/
main.go
109 lines (97 loc) · 3.09 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 (
"fmt"
"os"
"github.com/nicjohnson145/godot/internal/lib"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
// Version info set by goreleaser
var (
version = "development"
date = "unknown"
)
func main() {
if err := buildCommand().Execute(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func initLogger(verbose bool, debug bool) zerolog.Logger {
if verbose {
return lib.LoggerWithLevel(zerolog.InfoLevel)
}
if debug {
return lib.LoggerWithLevel(zerolog.DebugLevel)
}
return lib.LoggerWithLevel(zerolog.WarnLevel)
}
func buildCommand() *cobra.Command {
syncOpts := lib.SyncOpts{}
var debug bool
var verbose bool
var updateIgnoreVault bool
rootCmd := &cobra.Command{
Use: "godot",
Short: "A dotfiles manager",
Long: "A staticly linked dotfiles manager written in Go",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// So we don't print usage messages on execution errors
cmd.SilenceUsage = true
// So we dont double report errors
cmd.SilenceErrors = true
},
}
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug logging")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
syncCmd := &cobra.Command{
Use: "sync",
Short: "Sync configuration",
Long: "Sync local filesystem with configuration",
RunE: func(cmd *cobra.Command, args []string) error {
return lib.Sync(syncOpts, initLogger(verbose, debug))
},
}
syncCmd.Flags().BoolVarP(&syncOpts.Quick, "quick", "q", false, "Run a quick sync, skipping some stages")
syncCmd.Flags().StringSliceVarP(&syncOpts.Ignore, "ignore", "i", []string{}, "Ignore these configs")
syncCmd.Flags().BoolVar(&syncOpts.NoVault, "no-vault", false, "Ignore vault lookup directives in templates")
syncCmd.Flags().StringSliceVarP(&syncOpts.Executors, "executors", "e", []string{}, fmt.Sprintf("Limit run to only these executor types (valid values: %v)", lib.ExecutorTypeNames()))
rootCmd.AddCommand(syncCmd)
validateCmd := &cobra.Command{
Use: "validate <path-to-config>",
Args: cobra.ExactArgs(1),
Short: "Validate configuration",
Long: "Validate a configuration file on disk",
RunE: func(cmd *cobra.Command, args []string) error {
return lib.Validate(args[0])
},
}
rootCmd.AddCommand(validateCmd)
versionCmd := &cobra.Command{
Use: "version",
Short: "Show version info",
Args: cobra.NoArgs,
Long: "Show version info and exit",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Build Version: ", version)
fmt.Println("Build Time: ", date)
},
}
rootCmd.AddCommand(versionCmd)
updateCmd := &cobra.Command{
Use: "update",
Short: "Self update",
Args: cobra.NoArgs,
Long: "Check for newer version and install if found",
RunE: func(cmd *cobra.Command, args []string) error {
return lib.SelfUpdate(lib.SelfUpdateOpts{
Logger: initLogger(true, false),
CurrentVersion: version,
IgnoreVault: updateIgnoreVault,
})
},
}
updateCmd.Flags().BoolVar(&updateIgnoreVault, "no-vault", false, "Ignore vault integrations")
rootCmd.AddCommand(updateCmd)
return rootCmd
}