|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + homedir "github.com/mitchellh/go-homedir" |
| 6 | + |
| 7 | + "github.com/christopherhein/aws-operator/pkg/config" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/spf13/viper" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + // cfgFile, kubeConfig, awsRegion all help support passed in flags into the server |
| 17 | + cfgFile, kubeconfig, awsRegion, logLevel, logFile, resources, clusterName, bucket string |
| 18 | + |
| 19 | + // rootCmd represents the base command when called without any subcommands |
| 20 | + rootCmd = &cobra.Command{ |
| 21 | + Use: "aws-operator", |
| 22 | + Short: "AWS Operator manages your AWS Infrastructure using CRDs and Operators", |
| 23 | + Long: `TODO WRITE THIS`, |
| 24 | + Run: func(c *cobra.Command, _ []string) { |
| 25 | + c.Help() |
| 26 | + }, |
| 27 | + } |
| 28 | +) |
| 29 | + |
| 30 | +func main() { |
| 31 | + cobra.OnInitialize(initConfig) |
| 32 | + |
| 33 | + if err := rootCmd.Execute(); err != nil { |
| 34 | + fmt.Println(err) |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func init() { |
| 40 | + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "f", "Config file (default is $HOME/.aws-operator.yaml)") |
| 41 | + rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to local kubeconfig file (mainly used for development)") |
| 42 | + rootCmd.PersistentFlags().StringVarP(&awsRegion, "region", "r", "us-west-2", "AWS Region for resources to be created in") |
| 43 | + rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "Info", "Log level for the CLI") |
| 44 | + rootCmd.PersistentFlags().StringVarP(&logFile, "logfile", "", "", "Log level for the CLI") |
| 45 | + rootCmd.PersistentFlags().StringVarP(&resources, "resources", "", "s3bucket,dynamodb", "Comma delimited list of CRDs to deploy") |
| 46 | + rootCmd.PersistentFlags().StringVarP(&clusterName, "cluster-name", "i", "aws-operator", "Cluster name for the Application to run as, used to label the Cloudformation templated to avoid conflict") |
| 47 | + rootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "aws-operator", "To configure the operator you need a base bucket to contain the resources") |
| 48 | + |
| 49 | + viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) |
| 50 | + viper.BindPFlag("kubeconfig", rootCmd.PersistentFlags().Lookup("kubeconfig")) |
| 51 | + viper.BindPFlag("region", rootCmd.PersistentFlags().Lookup("region")) |
| 52 | + viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel")) |
| 53 | + viper.BindPFlag("logfile", rootCmd.PersistentFlags().Lookup("logfile")) |
| 54 | + viper.BindPFlag("resources", rootCmd.PersistentFlags().Lookup("resources")) |
| 55 | + viper.BindPFlag("clustername", rootCmd.PersistentFlags().Lookup("cluster-name")) |
| 56 | + viper.BindPFlag("bucket", rootCmd.PersistentFlags().Lookup("bucket")) |
| 57 | +} |
| 58 | + |
| 59 | +// initConfig reads in config file and ENV variables if set. |
| 60 | +func initConfig() { |
| 61 | + if cfgFile != "" { |
| 62 | + viper.SetConfigFile(cfgFile) |
| 63 | + } else { |
| 64 | + home, err := homedir.Dir() |
| 65 | + if err != nil { |
| 66 | + fmt.Println(err) |
| 67 | + os.Exit(1) |
| 68 | + } |
| 69 | + |
| 70 | + viper.AddConfigPath(home) |
| 71 | + viper.SetConfigName(".aws-operator") |
| 72 | + } |
| 73 | + |
| 74 | + replacer := strings.NewReplacer(".", "_") |
| 75 | + viper.SetEnvKeyReplacer(replacer) |
| 76 | + viper.AutomaticEnv() |
| 77 | + |
| 78 | + if err := viper.ReadInConfig(); err == nil { |
| 79 | + fmt.Println("Using config file:", viper.ConfigFileUsed()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func getConfig() (*config.Config, error) { |
| 84 | + resourcesList := strings.Split(resources, ",") |
| 85 | + config := &config.Config{ |
| 86 | + Region: awsRegion, |
| 87 | + Kubeconfig: kubeconfig, |
| 88 | + LoggingConfig: &config.LoggingConfig{ |
| 89 | + File: logFile, |
| 90 | + Level: logLevel, |
| 91 | + FullTimestamps: true, |
| 92 | + DisableTimestamps: false, |
| 93 | + }, |
| 94 | + Resources: resourcesList, |
| 95 | + ClusterName: clusterName, |
| 96 | + Bucket: bucket, |
| 97 | + } |
| 98 | + |
| 99 | + return config, nil |
| 100 | +} |
0 commit comments