shell/shell.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package shell import ( "context" "fmt" "log" "os" "os/exec" "path/filepath" "strings" "j3s.sh/zoa/env" "j3s.sh/zoa/utils" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" ) // this is used to detect when the script // name changes from run to run, which allows // us to prettily-print // var lastScriptPath string var r *interp.Runner func init() { var err error envs, err := env.GenerateEnv() if err != nil { log.Fatal(err) } r, err = interp.New( interp.StdIO(nil, os.Stdout, os.Stdout), interp.CallHandler(CallHandler), interp.Env(envs), ) if err != nil { log.Fatal(err) } } func CallHandler(ctx context.Context, args []string) ([]string, error) { // hc := interp.HandlerCtx(ctx) if args[0] == "zoa-script" { subScript := filepath.Join(utils.ZoaRoot, "scripts", args[1]) // TODO: figure out how to get scripts to echo their names // when we resume their execution utils.BluePrintln("-> " + subScript) runScriptInSubshell(ctx, subScript) // args has to have something in it - true is a pretty safe bet args = []string{"true", "true"} } if args[0] == "zoa-file" { // fmt.Fprintln(hc.Stdout, "") // return nil // args = "zoa-file nginx /etc/nginx/conf.d/jesse.conf systemctl nginx reload" if len(args) <= 2 { log.Fatal("zoa-file requires 2+ arguments") } src := args[1] dst := args[2] optionalCmd := []string{} if len(args) >= 4 { optionalCmd = args[3:] } fmt.Printf("$ zoa-file %s %s\n", src, dst) filePath := filepath.Join(utils.ZoaRoot, "files", src) dstChanged, err := zoaCopy(filePath, dst) if err != nil { log.Fatal(err) } if len(optionalCmd) >= 1 && dstChanged { args = optionalCmd } else { args = []string{"true", "true"} } } // this should really just say "if zoa-file or zoa-script ran" if len(args) == 2 && args[0] == "true" { // no printy } else { fmt.Printf("$ %s\n", strings.Join(args, " ")) } return args, nil // return interp.DefaultCallHandler(2*time.Second)(ctx, args) } func runScriptInSubshell(ctx context.Context, script string) error { r := r.Subshell() f, err := parseFile(script) if err != nil { return err } r.Run(ctx, f) return nil } func RunScript(s string) { utils.BluePrintln("(✿◠‿◠) zoa") script, err := parseFile(s) if err != nil { fmt.Printf("error in %s: %s\n", s, err) os.Exit(1) } err = r.Run(context.TODO(), script) if err != nil { fmt.Println(err) os.Exit(1) } } func parseFile(filename string) (*syntax.File, error) { var result = &syntax.File{} f, err := os.Open(filename) if err != nil { return result, err } defer f.Close() result, err = syntax.NewParser().Parse(f, "") return result, err } // zoaCopy copies a file from zoaRoot/scripts/src to dst. // if the dst was changed, zoaCopy will return true, // otherwise it will return false // zoaCopy defaults to 0666 for permissions (before umask) func zoaCopy(src string, dst string) (bool, error) { srcChk, err := utils.ChecksumFile(src) if err != nil { // source file should always exist, return error return false, err } dstChk, err := utils.ChecksumFile(dst) if err != nil { // dstfile may not exist for a million // reasons, set checksum to blank // to force a mismatch dstChk = "" } if srcChk == dstChk { fmt.Println("file unchanged") return false, nil } // TODO: pass the file through a templating engine // - expand vars // - loop support? err = utils.Copy(src, dst) if err != nil { return false, err } return true, nil } func SSH(command string, hostlist []string) error { _, err := exec.LookPath("ssh") if err != nil { log.Fatal(err) } for _, server := range hostlist { cmd := exec.Command("ssh", server, command) cmd.Stdout = os.Stdout err := cmd.Run() return err } return nil } func SCP(file string, hostlist []string) error { _, err := exec.LookPath("scp") if err != nil { log.Fatal(err) } for _, server := range hostlist { cmd := exec.Command("scp", file, server+":") cmd.Stdout = os.Stdout err := cmd.Run() return err } return nil } func SCPDir(dir string, hostlist []string) error { _, err := exec.LookPath("scp") if err != nil { log.Fatal(err) } for _, server := range hostlist { cmd := exec.Command("scp", "-r", dir, server+":"+dir) cmd.Stdout = os.Stdout err := cmd.Run() return err } return nil }