-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
109 lines (94 loc) · 2.09 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
const (
containerOutDir = "/out"
containerInDir = "/in"
dockerImage = "gonowa/wasm-opt"
)
func main() {
mArgs, srcDir, dstDir := buildArgs(os.Args[1:])
var args = []string{"run", "--rm"}
if srcDir != "" {
args = append(args, "-v", strings.Join([]string{srcDir, containerInDir}, ":"))
//most likely
if srcDir == dstDir {
args = append(args, "-v", strings.Join([]string{dstDir, containerOutDir}, ":"))
} else {
if dstDir != "" {
args = append(args, "-v", strings.Join([]string{dstDir, containerOutDir}, ":"))
}
}
}
args = append(args, "-i", dockerImage)
args = append(args, mArgs...)
cmd := exec.Command("docker", args...)
if Debug(args) {
fmt.Fprintf(os.Stdout, "runing - %s\n", strings.Join(cmd.Args, " "))
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
os.Exit(1)
}
}
func buildArgs(args []string) (mArgs []string, srcDir, dstDir string) {
for i := 0; i < len(args); i++ {
if isFlag(args[i]) {
if isOutput(args[i]) {
mArgs = append(mArgs, args[i])
//next arg is the output file
//todo guard this
if len(args) < i+1 {
log.Fatalln("you must specify output file")
}
output := args[i+1]
//get ouput path dir
dstDir = filepath.Dir(AbsPath(output))
mArgs = append(mArgs, path.Join(containerOutDir, filepath.Base(args[i+1])))
i++
} else {
mArgs = append(mArgs, args[i])
}
} else { //input file
srcDir = filepath.Dir(AbsPath(args[i]))
mArgs = append(mArgs, path.Join(containerInDir, filepath.Base(args[i])))
}
}
return
}
func isFlag(str string) bool {
if strings.HasPrefix(str, "-") {
return true
}
return false
}
func AbsPath(str string) string {
abspath, err := filepath.Abs(str)
if err != nil {
panic(err)
}
return abspath
}
func isOutput(str string) bool {
if str == "-o" || str == "--output" {
return true
}
return false
}
func Debug(args []string) bool {
for i := range args {
if args[i] == "-d" || args[i] == "--debug" {
return true
}
}
return false
}