-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (66 loc) · 1.95 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
package main
import (
"fmt"
"log"
"os"
"github.com/alexflint/go-arg"
"github.com/femnad/fup/base"
"github.com/femnad/fup/internal"
"github.com/femnad/fup/provision"
)
const (
version = "0.37.0"
)
type args struct {
DebugToStderr bool `arg:"-b,--debug-to-stderr" help:"Write logs to stderr"`
File string `arg:"-f,--file,env:FUP_CONFIG" default:"~/.config/fup/fup.yml" help:"Config file path"`
LogFile string `arg:"--logfile" default:"~/.local/share/fup/fup.log" help:"Log file path"`
LogLevel int `arg:"-l,--loglevel" default:"4" help:"Log level as integer, 0 least, 5 most"`
NoLogs bool `arg:"-n,--nologs" help:"Don't write logs to a file"`
PrintConfig bool `arg:"-r,--print-config" help:"Print final config and exit"`
Provisioners []string `arg:"-p,--provisioners" help:"List of provisioners to run"`
ValidateConfig bool `arg:"-c,--validate-config" help:"Validate config and exit"`
}
func (args) Version() string {
return fmt.Sprintf("fup v%s", version)
}
func printConfig(configFile string) {
out, err := base.FinalizeConfig(configFile)
if err != nil {
internal.Log.Errorf("error printing config from %s: %v", configFile, err)
os.Exit(1)
}
fmt.Println(out)
os.Exit(0)
}
func main() {
var parsed args
arg.MustParse(&parsed)
logFile := internal.ExpandUser(parsed.LogFile)
if parsed.NoLogs {
logFile = ""
}
internal.InitLogging(parsed.LogLevel, logFile, parsed.DebugToStderr)
cfg := parsed.File
internal.Log.Debugf("Reading config file %s", cfg)
if parsed.PrintConfig {
printConfig(cfg)
}
config, err := base.ReadConfig(cfg)
if err != nil {
log.Fatalf("%v\n", err)
}
if parsed.ValidateConfig {
os.Exit(0)
}
p, err := provision.NewProvisioner(config, parsed.Provisioners)
if err != nil {
internal.Log.Errorf("error building provisioner: %v", err)
return
}
err = p.Apply()
if err != nil {
fmt.Printf("Some provisioners had errors:\n%v\n", err)
os.Exit(1)
}
}