-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutdf.go
50 lines (46 loc) · 1.07 KB
/
utdf.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
package main
import (
"flag"
"fmt"
"log"
"os"
"reflect"
"github.com/attron/utdfgo"
)
func main() {
// setup cli flags
fname := flag.String("filename", "", "file that contains utdf data")
rcall := flag.String("run", "ToString", "run specific command")
ocall := flag.String("output", "", "save output of cli to file")
flag.Parse()
if *fname == "" {
flag.PrintDefaults()
os.Exit(0)
}
// run commands and output to file if requested
utdf, err := utdfgo.Run(*fname)
if err != nil {
log.Fatal("Unable to process:", err)
}
for i, p := range utdf {
cmd := reflect.ValueOf(p).MethodByName(*rcall)
output := cmd.Call([]reflect.Value{})
fmt.Printf("%v\n", output[0])
if *ocall != "" {
if i == 0 {
_, err := os.Create(*ocall)
if err != nil {
log.Fatal("Cannot create file", *ocall)
}
}
f, err := os.OpenFile(*ocall, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
fmt.Fprintln(f, output[0])
}
}
// useful for debugging
// fmt.Printf("fname: %s, rcall: %s, ocall: %s", *fname, *rcall, *ocall)
}