-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (48 loc) · 1.11 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
package main
import (
"os"
"github.com/timandy/routinex/inject"
"github.com/timandy/routinex/tools/exec"
"github.com/timandy/routinex/tools/flag"
"github.com/timandy/routinex/tools/log"
"github.com/timandy/routinex/tools/opt"
"github.com/timandy/routinex/tools/slices"
)
func main() {
// parse app options
args := slices.Filter(os.Args, func(s string) bool { return s != "" })
appOpt := &opt.AppOptions{}
flagSet := flag.ParseStruct(appOpt, args[0], args[1:])
// print entry args
if appOpt.Debug {
log.PrintArgs("entry", args)
}
// print usage
if appOpt.Help {
flagSet.SortFlags = false
flag.PrintUsage(flagSet)
return
}
// print before args
remainArgs := flagSet.Args()
if appOpt.Debug {
log.PrintArgs("before", remainArgs)
}
// return when no remain args
if len(remainArgs) == 0 {
if appOpt.Debug {
log.Info("no remain args and exit")
}
return
}
// exists remained args, run the cmd finally
defer func() {
// print after args
if appOpt.Debug {
log.PrintArgs("after", remainArgs)
}
exec.RunCmd(remainArgs)
}()
// exec inject
remainArgs = inject.Execute(remainArgs, appOpt)
}