-
Notifications
You must be signed in to change notification settings - Fork 50
/
commands.go
391 lines (323 loc) · 6.8 KB
/
commands.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
//"errors"
"io/ioutil"
// "log"
"os"
"os/exec"
//"strconv"
//"bytes"
"fmt"
"github.com/opesun/copyrecur"
"path/filepath"
"strings"
)
//type params map[string]string
// Command represents an action on system.
type Command struct {
Name string `json:"name"`
Params map[string]string
ParamsList []string
Path string
run func(c *Command) int
Stdout string
Stderr string
status int
}
func (c *Command) Run() int {
return c.run(c)
}
// Returns a path ending with "/"
func (c *Command) GetPath() string {
return strings.TrimRight(c.Path, "/") + "/"
}
// GetPParam gets a path param, trimming, the first /
func (c *Command) GetPathParam(s string) string {
return strings.Trim(c.Params[s], "/")
}
func (c *Command) Status() int {
return c.status
}
// Returns a command to exec in a given path ( dir )
func GetCommand(cmd string, dir string) *Command {
if val, ok := commands[cmd]; ok {
val.Path = dir
return val
}
return nil
}
var commands = map[string]*Command{
"save": saveCommand,
"delete": deleteCommand,
"createFolder": createfolderCommand,
"rename": renameCommand,
"copy": copyCommand,
"compress": compressCommand,
"mv": mvCommand,
"syscmd": sysCommand,
}
var copyCommand = &Command{
Name: "Copy",
run: copy_file,
}
func copy_file(c *Command) int {
source := c.GetPath() + c.GetPathParam("source")
dest := c.GetPath() + c.GetPathParam("dest")
fi, err := os.Stat(source)
if err != nil {
c.Stderr = err.Error()
c.status = 1
return 1
}
if fi.IsDir() {
err := copyrecur.CopyDir(source, dest)
if err != nil {
c.Stderr = err.Error()
c.status = 1
return 1
}
return 0
} else {
err := copyrecur.CopyFile(source, dest)
if err != nil {
c.Stderr = err.Error()
c.status = 1
return 1
}
return 0
}
return 0
}
// rename command
var renameCommand = &Command{
Name: "Rename",
run: rename_file,
}
func rename_file(c *Command) int {
fo := c.GetPathParam("source")
fn := c.GetPathParam("dest")
fo = strings.Trim(fo, "../")
fn = strings.Trim(fn, "../")
err := os.Rename(c.GetPath()+fo, c.GetPath()+fn)
if err != nil {
c.Stderr = err.Error()
c.status = 1
return 1
}
return 0
}
// Create a dir
var createfolderCommand = &Command{
Name: "Create Folder",
run: create_folder,
}
func create_folder(c *Command) int {
folder := c.Params["source"]
file := c.GetPath() + strings.Trim(folder, "/")
err := os.Mkdir(file, 0777)
if err != nil {
c.Stderr = err.Error()
c.status = 1
return 1
}
return 0
}
// Save a File
var saveCommand = &Command{
Name: "Save File",
run: save_file,
}
func save_file(c *Command) int {
data := []byte(c.Params["content"])
file := c.GetPath() + c.GetPathParam("file")
//@todo parametrize mask
err := ioutil.WriteFile(file, data, 0644)
if err != nil {
c.status = 1
c.Stderr = err.Error()
return 1
}
return 0
}
// Delete a file, or list of files
var deleteCommand = &Command{
Name: "Delete File",
run: delete_file,
}
func delete_file(c *Command) int {
files := c.ParamsList
errs := make([]string, 0)
has_errors := false
for k := range files {
f := c.GetPath() + strings.Trim(files[k], "/")
err := os.Remove(f)
if err != nil {
errs = append(errs, err.Error())
has_errors = true
}
}
if has_errors == true {
c.status = 1
c.Stderr = strings.Join(errs, ",")
return 1
}
return 0
}
var sysCommand = &Command{
Name: "Exec command",
run: sys_command,
}
func sys_command(c *Command) int {
source := c.GetPath() + c.GetPathParam("source")
cm := c.Params["command"]
args := c.ParamsList
cmd := exec.Command(cm, args...)
cmd.Dir = source
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(err)
c.Stderr = string(out)
return 1
}
c.Stdout = string(out)
return 0
}
/*type flushWriter struct {
f http.Flusher
w io.Writer
}
func (fw *flushWriter) Write(p []byte) (n int, err error) {
n, err = fw.w.Write(p)
if fw.f != nil {
fw.f.Flush()
}
return
}
func HandlerStreamCommand(w http.ResponseWriter, wc *WebCommand) {
w.Header().Set("Content-Type", "application/octet-stream")
path := strings.TrimRight(dir, "/") + "/"
source := strings.Trim(wc.Params["source"], "/")
command := wc.Params["command"]
args := wc.ParamsList
fw := flushWriter{w: w}
if f, ok := w.(http.Flusher); ok {
fw.f = f
}
cmd := exec.Command(command, args...)
cmd.Dir = path + source
stdout, err := cmd.StderrPipe()
if err != nil {
fmt.Println(err)
}
/*stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println(err)
}*/
//cmd.Stdout = &fw
//cmd.Stderr = &fw
/* cmd.Start()
bufin := bufio.NewReader(stdout)
go func() {
i := 1
for {
b := make([]byte, 8)
_, err := bufin.Read(b)
if err != nil {
//fmt.Print()
break
}
fw.Write(b)
i++
//fmt.Printf("looping %d", i)
}
}()
//go func() {
//var buf bytes.Buffer
//go io.Copy(&fw, stdout)
//go io.Copy(&fw, stderr)
if err := cmd.Wait(); err != nil {
fmt.Print(err)
}
return
}
*/
var compressCommand = &Command{
Name: "Compress Tar/gz",
run: compress_file,
}
func compress_file(c *Command) int {
// source will be .. /abc/abc/abc/
// source will contain .. Absolute path to dir... /xxx/a/b/c
source := c.GetPath() + c.GetPathParam("source")
// base will contain c
base := filepath.Base(source)
dir := filepath.Dir(source)
fname := fmt.Sprintf("%s.tar.gz", base)
cmd := exec.Command("tar", "cvfz", fname, base)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
c.Stderr = string(out)
c.status = 1
return 1
}
c.Stdout = string(out)
return 0
}
var mvCommand = &Command{
Name: "Mv Command",
run: mv_file,
}
func mv_file(c *Command) int {
source := c.GetPath() + c.GetPathParam("source")
dest := c.GetPath() + c.GetPathParam("dest")
cmd := exec.Command("mv", source, dest)
out, err := cmd.CombinedOutput()
if err != nil {
c.Stderr = string(out)
c.status = 1
return 1
}
c.Stdout = string(out)
return 0
}
/*
POST...
{
'command': 'save'
'params': {
'file': 'xxx'
'content': 'xxxx'
}
'paramList': ['xxx', 'xxx']
'path'
}
command: command_name
params: hash
// AjaxApiHandler
type WebCommand struct {
command string
params map[string]string
parmList []string
pat string
}
type OutputCommand {
Out string
Err string
}
wc: = &WebCommand{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&wc)
command := GetCommand(wc)
if command == nil {
http.Error(w, "Command Not Found", http.StatusInternalServerError)
return
}
err = command.run()
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Sprint(w, Outputcommand)
*/