-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (95 loc) · 2.6 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 (
_ "embed"
"github.com/gabrieljuliao/godo/cmd/configuration"
"github.com/gabrieljuliao/godo/cmd/consts"
"github.com/gabrieljuliao/godo/cmd/env"
"github.com/gabrieljuliao/godo/cmd/info"
"github.com/gabrieljuliao/godo/cmd/os/exec"
"github.com/gabrieljuliao/godo/cmd/service"
"github.com/gabrieljuliao/godo/cmd/utils"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
//go:embed VERSION
var appVersion string
//go:embed resources/banner.txt
var appBanner string
//go:embed resources/usage.txt
var appUsageMsg string
//go:embed resources/config_file_example.yaml
var appConfigFileExample string
func main() {
info.NewAppInfo(appVersion, appUsageMsg, appBanner, appConfigFileExample)
info.PrintBanner()
env.InitEnv()
preConfigActions(filterGodoArgs(os.Args[1:]))
}
func filterGodoArgs(args []string) []string {
for i, arg := range args {
if strings.HasPrefix(arg, "--godo") {
if !utils.HasNext(args, i) {
log.Fatal("You must provide a value to godo options.")
}
noPrefixArg := strings.Replace(arg, "--godo-", "", -1)
switch noPrefixArg {
case "config-file":
path := args[i+1]
if filepath.IsAbs(path) {
env.Properties[consts.GodoConfigurationFilePath] = path
args = args[2:]
} else {
log.Fatalf("Value '%s' for option '%s' is not a valid path.", path, arg)
}
case "config-editor":
path := args[i+1]
if utils.IsBinaryOnPath(path) {
env.Properties[consts.GodoConfigurationEditor] = args[i+1]
args = args[2:]
} else {
log.Fatalf("Could not locate '%s' for option '%s'. Make sure executable is exported to path.", path, arg)
}
case "config-editor-args":
regex, _ := regexp.Compile("^(?:[^,\\s]+(?:,[^,\\s]+)*)?$\n")
editorArgs := args[i+1]
if regex.MatchString(editorArgs) {
env.Properties[consts.GodoConfigurationEditorArgs] = args[i+1]
args = args[2:]
} else {
log.Fatalf("Value '%s' for option '%s' does not match the pattern: --arg1,value1,arg-2.", editorArgs, arg)
}
default:
log.Fatalf("Option '%s' does not exist", arg)
}
}
}
return args
}
func preConfigActions(argv []string) {
env.FindConfigFilePath()
var action = ""
if len(argv) > 0 {
action = argv[0]
}
switch {
case action == "edit":
exec.OpenConfigurationEditor()
case action == "" || action == "-h" || action == "--help":
info.PrintUsage()
default:
postConfigActions(action, argv)
}
}
func postConfigActions(action string, argv []string) {
// load configuration
configuration.NewConfiguration()
switch action {
case "list":
configuration.ListConfiguration()
default:
service.ExecMacro(argv)
}
}