-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (77 loc) · 2 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
)
var (
Version = "No version provided"
BuildDate = "Unknown build date"
Commit = ""
mainFlagSet = flag.NewFlagSet("ILC", flag.ExitOnError)
logger = log.New(io.Discard, "DEBUG: ", log.Lshortfile)
)
func main() {
mainFlagSet.Usage = func() {
u := NewUsage(mainFlagSet.Output())
u.Entrypoint = os.Args[0:1]
u.Print()
os.Exit(0)
}
mainFlagSet.BoolFunc("version", "Displays the version", func(_ string) error {
fmt.Printf("ILC - %s\nVersion: %s\n", BuildDate, Version)
if Commit != "" {
fmt.Printf("Commit: %s\n", Commit)
}
os.Exit(0)
return nil
})
debug := mainFlagSet.Bool("debug", false, "Print debug information")
nonInteractive := mainFlagSet.Bool("non-interactive", false, "Disable interactivity")
validateConfig := mainFlagSet.Bool("validate", false, "Validate configuration")
mainFlagSet.Parse(os.Args[1:])
args := mainFlagSet.Args()
if len(args) == 0 {
fmt.Fprintf(mainFlagSet.Output(), "configuration file not provided\n")
os.Exit(2)
}
if *debug {
logger.SetOutput(os.Stderr)
}
config, err := LoadConfig(args[0])
if err != nil {
fmt.Fprintf(mainFlagSet.Output(), "error loading configuration: %v\n", err)
os.Exit(2)
} else if *validateConfig {
fmt.Fprintf(mainFlagSet.Output(), "configuration is valid\n")
os.Exit(0)
}
runner := NewRunner(config, args[1:])
runner.NonInteractive = *nonInteractive
runner.Entrypoint = getEntrypoint(args[0])
err = runner.Run()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
}
func getEntrypoint(configPath string) []string {
underscore := ""
for _, item := range os.Environ() {
if strings.HasPrefix(item, "_=") {
underscore = strings.TrimPrefix(item, "_=")
break
}
}
// Check if config was invoked
if underscore == configPath {
logger.Println("Detected entrypoint to be config file")
return []string{underscore}
} else {
logger.Println("Assuming direct execution of binary")
return []string{os.Args[0], configPath}
}
}