-
Notifications
You must be signed in to change notification settings - Fork 8
/
args.go
69 lines (63 loc) · 1.01 KB
/
args.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
package main
import (
"os"
"path/filepath"
"strings"
)
type args struct {
command string
home string
output string
}
func parseArgs() *args {
args := &args{
home: findHome(),
}
osArgs := os.Args
if len(osArgs) < 2 {
return args
}
args.command = osArgs[1]
flag := ""
for _, arg := range osArgs {
if strings.HasPrefix(arg, "-") {
flag = arg
continue
}
switch flag {
case "-o", "-output":
args.output = arg
}
flag = ""
}
return args
}
func findHome() string {
dir, err := filepath.Abs(".")
if err != nil {
return ""
}
for {
path := filepath.Join(dir, "olca-schema")
if isDir(path) {
return path
}
parent := filepath.Dir(dir)
if parent == dir {
return ""
}
dir = parent
}
}
func (a *args) outputFileOrDefault(name string) (string, error) {
var path string
if a.output != "" {
path = a.output
} else {
path = filepath.Join(a.home, name)
}
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return "", err
}
return path, nil
}