-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathroot.go
125 lines (106 loc) · 3.53 KB
/
root.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package cmd contains the CLI commands for Zarf.
package cmd
import (
"fmt"
"os"
"strings"
"github.com/defenseunicorns/zarf/src/cmd/tools"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
logLevel string
// Default global config for the CLI
pkgConfig = types.PackagerConfig{}
// Viper instance used by the cmd package
v *viper.Viper
)
var rootCmd = &cobra.Command{
Use: "zarf COMMAND",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Skip for vendor-only commands
if tools.CheckVendorOnlyFromPath(cmd) {
return
}
// Don't add the logo to the help command
if cmd.Parent() == nil {
config.SkipLogFile = true
}
cliSetup()
},
Short: lang.RootCmdShort,
Long: lang.RootCmdLong,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
zarfLogo := message.GetLogo()
_, _ = fmt.Fprintln(os.Stderr, zarfLogo)
cmd.Help()
if len(args) > 0 {
if strings.Contains(args[0], config.ZarfPackagePrefix) || strings.Contains(args[0], "zarf-init") {
pterm.FgYellow.Printfln("\n"+lang.RootCmdDeprecatedDeploy, args[0])
}
if args[0] == config.ZarfYAML {
pterm.FgYellow.Printfln("\n" + lang.RootCmdDeprecatedCreate)
}
}
},
}
// Execute is the entrypoint for the CLI.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
// Add the tools commands
tools.Include(rootCmd)
// Skip for vendor-only commands
if tools.CheckVendorOnlyFromArgs() {
return
}
initViper()
v.SetDefault(V_LOG_LEVEL, "info")
v.SetDefault(V_ARCHITECTURE, "")
v.SetDefault(V_NO_LOG_FILE, false)
v.SetDefault(V_NO_PROGRESS, false)
v.SetDefault(V_INSECURE, false)
v.SetDefault(V_ZARF_CACHE, config.ZarfDefaultCachePath)
v.SetDefault(V_TMP_DIR, "")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", v.GetString(V_LOG_LEVEL), lang.RootCmdFlagLogLevel)
rootCmd.PersistentFlags().StringVarP(&config.CLIArch, "architecture", "a", v.GetString(V_ARCHITECTURE), lang.RootCmdFlagArch)
rootCmd.PersistentFlags().BoolVar(&config.SkipLogFile, "no-log-file", v.GetBool(V_NO_LOG_FILE), lang.RootCmdFlagSkipLogFile)
rootCmd.PersistentFlags().BoolVar(&message.NoProgress, "no-progress", v.GetBool(V_NO_PROGRESS), lang.RootCmdFlagNoProgress)
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.CachePath, "zarf-cache", v.GetString(V_ZARF_CACHE), lang.RootCmdFlagCachePath)
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.TempDirectory, "tmpdir", v.GetString(V_TMP_DIR), lang.RootCmdFlagTempDir)
rootCmd.PersistentFlags().BoolVar(&config.CommonOptions.Insecure, "insecure", v.GetBool(V_INSECURE), lang.RootCmdFlagInsecure)
}
func cliSetup() {
match := map[string]message.LogLevel{
"warn": message.WarnLevel,
"info": message.InfoLevel,
"debug": message.DebugLevel,
"trace": message.TraceLevel,
}
// No log level set, so use the default
if logLevel != "" {
if lvl, ok := match[logLevel]; ok {
message.SetLogLevel(lvl)
message.Debug("Log level set to " + logLevel)
} else {
message.Warn(lang.RootCmdErrInvalidLogLevel)
}
}
// Disable progress bars for CI envs
if os.Getenv("CI") == "true" {
message.Debug("CI environment detected, disabling progress bars")
message.NoProgress = true
}
if !config.SkipLogFile {
message.UseLogFile()
}
}