/
main.go
158 lines (140 loc) · 3.06 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/crackcomm/go-actions/action"
"github.com/crackcomm/go-actions/core"
"github.com/crackcomm/go-actions/encoding/yaml"
"github.com/crackcomm/go-actions/local"
"github.com/golang/glog"
_ "github.com/crackcomm/go-actions/source/file"
_ "github.com/crackcomm/go-actions/source/http"
_ "github.com/crackcomm/go-core"
)
var filename = "cmds.yaml" // cmds filename
func main() {
defer glog.Flush()
flag.Parse()
actions, err := fileToActions(filename)
if err != nil {
fmt.Println(err)
return
}
if len(os.Args) <= 1 {
fmt.Println("cmds needs at least one argument\n")
fmt.Println("available commands:\n")
for name := range actions {
fmt.Printf(" %s\n", name)
}
return
}
for name, a := range actions {
core.Action(name, a)
}
res, err := local.Run(&action.Action{Name: os.Args[1]})
if res != nil {
res = mapBytes(res)
printMap(res, 1)
fmt.Printf("\n")
}
if err != nil {
fmt.Printf("ERROR %v\n", err)
} else {
fmt.Println("OK")
}
}
// return n * 2 spaces
func spaces(n int) string {
return strings.Repeat(" ", n*2)
}
// print with spaces
func prints(n int, f string, a ...interface{}) {
fmt.Printf("%s%s", spaces(n), fmt.Sprintf(f, a...))
}
func printValue(value interface{}, n int) {
switch value.(type) {
case action.Map:
print("\n")
printMap(value.(action.Map), n+1)
case []interface{}:
print("\n")
for _, v := range value.([]interface{}) {
prints(n+1, "-")
printValue(v, n+1)
}
case []string:
print("\n")
for _, v := range value.([]string) {
prints(n+1, "- %v\n", v)
}
case string:
lines := strings.Split(value.(string), "\n")
if len(lines) <= 1 {
print(value.(string))
print("\n")
} else {
print("\n")
for _, ln := range lines {
prints(n+1, "%s\n", ln)
}
}
default:
f := action.Format{value}
if v, ok := f.String(); ok {
prints(0, "%#v\n", v)
} else {
prints(0, "%#v\n", value)
}
}
}
func printKeyValue(key string, value interface{}, n int) {
prints(n, "%s: ", key)
printValue(value, n)
}
func printMap(m action.Map, n int) {
for key, value := range m {
printKeyValue(key, value, n)
}
}
// mapBytes - Iterates through a result map and tranforms all byte arrays into strings.
func mapBytes(m action.Map) action.Map {
// Iterate map
for k, v := range m {
// Pass if value is nil
if v == nil {
continue
}
// If it's a byte array or a reader we are gonna make it a string
if rc, ok := v.(io.ReadCloser); ok {
arr, _ := ioutil.ReadAll(rc)
rc.Close()
m[k] = string(arr)
} else if arr, ok := v.([]byte); ok {
m[k] = string(arr)
}
}
return m
}
func indent(body string) string {
res := ""
lines := strings.Split(body, "\n")
for _, line := range lines {
line = " " + line + "\n"
res = res + line
}
return res
}
func fileToActions(filename string) (map[string]*action.Action, error) {
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return yaml.UnmarshalMany(body)
}
func init() {
flag.StringVar(&filename, "file", filename, "Cmds file")
}