generated from ivanklee86/template_go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (107 loc) · 3.42 KB
/
main.go
File metadata and controls
129 lines (107 loc) · 3.42 KB
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
129
package main
import (
"fmt"
"github.com/spf13/pflag"
"os"
"strings"
"github.com/ivanklee86/argonap/pkg/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
// Build information (injected by goreleaser).
version = "dev"
)
const (
defaultConfigFilename = "argonap"
envPrefix = "ARGONAP"
)
// main function.
func main() {
command := NewRootCommand()
if err := command.Execute(); err != nil {
os.Exit(1)
}
}
func NewRootCommand() *cobra.Command {
argonap := cli.New()
cmd := &cobra.Command{
Use: "argonap",
Short: "Give ArgoCD a lil' nap.",
Long: "A CLI to provision sync windows at scale.",
Version: version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
argonap.Out = cmd.OutOrStdout()
argonap.Err = cmd.ErrOrStderr()
return initializeConfig(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprint(argonap.Out, cmd.UsageString())
},
}
cmd.PersistentFlags().StringVar(&argonap.Config.ServerAddr, "server-address", "", "ArgoCD server address")
cmd.PersistentFlags().BoolVar(&argonap.Config.Insecure, "insecure", false, "Don't validate SSL certificate on client request")
cmd.PersistentFlags().StringVar(&argonap.Config.AuthToken, "auth-token", "", "JWT Authentication Token")
cmd.PersistentFlags().StringSliceVar(&argonap.Config.ProjectNames, "name", []string{}, "Project names to update. If specified, label filtering will not apply. Can be used multiple times.")
cmd.PersistentFlags().StringSliceVar(&argonap.Config.LabelsAsStrings, "label", []string{}, "Labels to filter projects on in format 'key=value'. Can be used multiple times.")
cmd.PersistentFlags().IntVar(&argonap.Config.Timeout, "timeout", 240, "Context timeout in seconds.")
cmd.PersistentFlags().IntVar(&argonap.Config.Workers, "workers", 4, "# of parallel workers.")
cmd.AddCommand(NewClearCommand(argonap))
cmd.AddCommand(NewSetCommand(argonap))
return cmd
}
func NewClearCommand(argonap *cli.Argonap) *cobra.Command {
cmd := &cobra.Command{
Use: "clear",
Short: "Clear SyncWindows.",
Long: "Clear SyncWindows on all AppProjects.",
Run: func(cmd *cobra.Command, args []string) {
argonap.Connect()
argonap.ClearSyncWindows()
},
}
return cmd
}
func NewSetCommand(argonap *cli.Argonap) *cobra.Command {
cmd := &cobra.Command{
Use: "set",
Short: "Set SyncWindows.",
Long: "Set SyncWindows from file",
Run: func(cmd *cobra.Command, args []string) {
argonap.Connect()
argonap.SetSyncWindows()
},
}
cmd.PersistentFlags().StringVar(&argonap.Config.SyncWindowsFile, "file", "", "Path to file with SyncWindows to configure")
return cmd
}
func initializeConfig(cmd *cobra.Command) error {
v := viper.New()
v.SetConfigName(defaultConfigFilename)
v.AddConfigPath(".")
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return err
}
}
v.SetEnvPrefix(envPrefix)
v.AutomaticEnv()
bindFlags(cmd, v)
return nil
}
func bindFlags(cmd *cobra.Command, v *viper.Viper) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if strings.Contains(f.Name, "-") {
envVarSuffix := strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))
if err := v.BindEnv(f.Name, fmt.Sprintf("%s_%s", envPrefix, envVarSuffix)); err != nil {
os.Exit(1)
}
}
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil {
os.Exit(1)
}
}
})
}