-
Notifications
You must be signed in to change notification settings - Fork 24
/
root.go
128 lines (113 loc) · 4.45 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
126
127
128
package cmd
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/autoscale"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/blueprints"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/build"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/cloud"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/decisions"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/discovery"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/dynamicconfig"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/flowcontrol"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/globalcache"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/installation"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/policy"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/resultcache"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/status"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/utils"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/info"
"github.com/fluxninja/aperture/v2/pkg/log"
)
var (
version = info.Version
verbose bool
controller utils.ControllerConn
)
func init() {
RootCmd.AddCommand(cloud.CloudCmd)
RootCmd.AddCommand(blueprints.BlueprintsCmd)
RootCmd.AddCommand(compileCmd)
RootCmd.AddCommand(installation.InstallCmd)
RootCmd.AddCommand(installation.UnInstallCmd)
RootCmd.AddCommand(flowcontrol.FlowControlCmd)
RootCmd.AddCommand(autoscale.AutoScaleCmd)
RootCmd.AddCommand(discovery.DiscoveryCmd)
RootCmd.AddCommand(build.BuildCmd)
RootCmd.AddCommand(agentsCmd)
RootCmd.AddCommand(decisions.DecisionsCmd)
RootCmd.AddCommand(status.StatusCmd)
RootCmd.AddCommand(dynamicconfig.DynamicConfigCmd)
RootCmd.AddCommand(policy.PolicyCmd)
RootCmd.AddCommand(dashboardCmd)
RootCmd.AddCommand(resultcache.CacheCmd)
RootCmd.AddCommand(globalcache.CacheCmd)
RootCmd.InitDefaultCompletionCmd()
RootCmd.SilenceUsage = true
}
// RootCmd is the root command for aperturectl.
var RootCmd = &cobra.Command{
Use: "aperturectl",
Short: "aperturectl - CLI tool to interact with Aperture",
DisableAutoGenTag: true,
DisableSuggestions: false,
Long: `aperturectl is a CLI tool to interact with Aperture.`,
Version: version,
}
// Execute is the entry point for the CLI. It is called from the main package.
func Execute() {
// Process the verbose and allowDeprecated flags before executing the command.
// Fun fact: PersistentPreRunE does not allow chaining.
// set flags manually using pflag and parse them
pflag.BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
pflag.BoolVar(&utils.AllowDeprecated, "allow-deprecated", false, "Allow deprecated fields in the configuration")
// Set help flag to false by default to print help for aperturectl command instead of pflag
pflag.BoolP("help", "h", false, "Display help for aperturectl command")
// configure pflag to ignore unknown flags
pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
pflag.Parse()
pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = false
level := "info"
if verbose {
level = "debug"
}
logger := log.NewLogger(log.GetPrettyConsoleWriter(), level)
log.SetGlobalLogger(logger)
err := config.RegisterDeprecatedValidator(utils.AllowDeprecated)
if err != nil {
log.Error().Err(err).Msg("Error registering deprecated validator")
os.Exit(1)
}
newer, err := utils.IsCurrentVersionNewer(info.Version)
if err != nil {
log.Error().Err(err).Msg("Failed to check if current version is newer")
}
if newer {
var userHomeDir string
userHomeDir, err = os.UserHomeDir()
if err == nil {
aperturectlRootDir := filepath.Join(userHomeDir, utils.AperturectlRootDir)
aperturectlBlueprintsCacheRoot := filepath.Join(aperturectlRootDir, utils.BlueprintsCacheRoot)
err = os.RemoveAll(aperturectlBlueprintsCacheRoot)
if err != nil {
log.Error().Err(err).Msg("Failed to remove latest blueprints")
}
aperturectlBuilderCacheRoot := filepath.Join(aperturectlRootDir, utils.BuilderCacheRoot)
err = os.RemoveAll(aperturectlBuilderCacheRoot)
if err != nil {
log.Error().Err(err).Msg("Failed to remove builder cache")
}
}
err = utils.UpdateVersionFile(info.Version)
if err != nil {
log.Error().Err(err).Msg("Failed to update version file")
}
}
if err = RootCmd.Execute(); err != nil {
log.Error().Err(err).Msg("Error executing aperturectl")
os.Exit(1)
}
}