/
main.go
101 lines (82 loc) · 2.59 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
package main
import (
"fmt"
"log"
"os"
homedir "github.com/mitchellh/go-homedir"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"github.com/kanopy-platform/ksec/internal/version"
"github.com/kanopy-platform/ksec/pkg/models"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var secretsClient *models.SecretsClient
func initRootCmd(rootCmd *cobra.Command) {
cobra.OnInitialize(initConfig)
// global options
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (Default: $HOME/.ksec.yaml)")
rootCmd.PersistentFlags().StringP("namespace", "n", "", "Operate in a specific NAMESPACE (Default: current kubeconfig namespace)")
// setup viper config
viper.BindPFlags(rootCmd.PersistentFlags())
// subcommands without extra options
rootCmd.AddCommand(createCmd)
rootCmd.AddCommand(pullCmd)
rootCmd.AddCommand(pushCmd)
rootCmd.AddCommand(setCmd)
rootCmd.AddCommand(unsetCmd)
// subcommands with extra options
rootCmd.AddCommand(getCmd)
getCmd.Flags().BoolP("verbose", "v", false, "Show extra metadata")
rootCmd.AddCommand(deleteCmd)
deleteCmd.Flags().BoolP("yes", "y", false, "do not ask for confirmation")
rootCmd.AddCommand(listCmd)
listCmd.Flags().BoolP("all", "a", false, "Show all secrets (Default: Opaque only)")
rootCmd.AddCommand(completionCmd)
completionCmd.AddCommand(bashCompletionCmd)
completionCmd.AddCommand(zshCompletionCmd)
}
func NewRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "ksec",
Short: "A tool for managing Kubernetes Secret data",
Version: version.Get().Version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var err error
secretsClient, err = models.NewSecretsClient(viper.GetString("namespace"))
if err != nil {
log.Fatal(err.Error())
}
},
}
initRootCmd(rootCmd)
return rootCmd
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Fatal(err.Error())
}
// Search config in home directory with name ".ksec" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".ksec")
}
viper.SetEnvPrefix("KSEC")
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
func main() {
if err := NewRootCmd().Execute(); err != nil {
os.Exit(1)
}
}