-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfile.go
87 lines (74 loc) · 1.65 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package io
import (
"compress/gzip"
"fmt"
"github.com/lmorg/murex/lang/proc"
"github.com/lmorg/murex/lang/types"
"io"
"os"
"time"
)
func init() {
proc.GoFunctions["text"] = proc.GoFunction{Func: cmdText, TypeIn: types.Null, TypeOut: types.String}
proc.GoFunctions["open"] = proc.GoFunction{Func: cmdOpen, TypeIn: types.Null, TypeOut: types.Generic}
proc.GoFunctions["pt"] = proc.GoFunction{Func: cmdPipeTelemetry, TypeIn: types.Generic, TypeOut: types.Generic}
}
func cmdText(p *proc.Process) error {
for _, filename := range p.Parameters.StringArray() {
file, err := os.Open(filename)
if err != nil {
return err
}
if len(filename) > 3 && filename[len(filename)-3:] == ".gz" {
gz, err := gzip.NewReader(file)
if err != nil {
file.Close()
return err
}
_, err = io.Copy(p.Stdout, gz)
file.Close()
if err != nil {
return err
}
} else {
_, err = io.Copy(p.Stdout, file)
file.Close()
if err != nil {
return err
}
}
}
return nil
}
func cmdOpen(p *proc.Process) error {
for _, filename := range p.Parameters.StringArray() {
file, err := os.Open(filename)
if err != nil {
file.Close()
return err
}
_, err = io.Copy(p.Stdout, file)
if err != nil {
file.Close()
return err
}
file.Close()
}
return nil
}
func cmdPipeTelemetry(p *proc.Process) error {
quit := false
go func() {
for !quit {
time.Sleep(1 * time.Second)
if quit {
return
}
written, _ := p.Stdin.Stats()
_, read := p.Stdout.Stats()
os.Stderr.WriteString(fmt.Sprintf("Pipe telemetry: written %d bytes -> .pt() -> read %d bytes\n", written, read))
}
}()
io.Copy(p.Stdout, p.Stdin)
return nil
}