-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.go
70 lines (58 loc) · 1.59 KB
/
invoke.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
package system
import (
"fmt"
"io/fs"
"os/exec"
"runtime"
"strings"
)
type Command struct {
name string `json:name`
filename string `json:filename`
args []string `json:arguments`
}
const CMD_GO = "go"
const CMD_CLAMSCAN = "clamscan"
var validCommands map[string]*Command
func Init() {
validCommands = make(map[string]*Command, 10)
validCommands[CMD_GO] = &Command{name: CMD_GO, filename: "/go/bin/go", args: []string{"version"}}
validCommands[CMD_CLAMSCAN] = &Command{name: CMD_CLAMSCAN, filename: "/bin/clamdscan", args: []string{"-i", "--no-summary", "--move=/safe-import/quarantine"}}
}
func Run(command string, varargs ...string) (error, int, []byte) {
if validCommands == nil {
Init()
}
if cmd, ok := validCommands[command]; ok {
// Add user arguments to end of static command arguments
args := cmd.args
for _, n := range varargs {
args = append(args, n)
}
// Replace all / with \ in all arguments if running in Windows environment
if runtime.GOOS == "windows" {
cmd.filename = strings.ReplaceAll(cmd.filename, "/", "\\")
for i, v := range args {
args[i] = strings.ReplaceAll(v, "/", "\\")
}
}
fmt.Println("CLAMSCAN:", cmd.filename, args)
out, err := exec.Command(cmd.filename, args...).Output()
if err != nil {
switch e := err.(type) {
case *fs.PathError:
return err, 2, out
case *exec.ExitError:
return err, e.ExitCode(), out
default:
return err, 0, out
}
}
return err, 0, out
}
err := fmt.Errorf("Invalid command: %s\n", command)
return err, 126, []byte(command)
}
func Log(msg string) error {
return nil
}