ashb commented on code in PR #50964: URL: https://github.com/apache/airflow/pull/50964#discussion_r2104663262
########## go-sdk/celery/cmd/root.go: ########## @@ -0,0 +1,158 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + "path" + "strings" + + cc "github.com/ivanpirog/coloredcobra" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/spf13/viper" + + "github.com/apache/airflow/go-sdk/pkg/sdkcontext" + "github.com/apache/airflow/go-sdk/worker" +) + +var cfgFile string + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "airflow-go-sdk", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application. + `, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + return initializeConfig(cmd) + }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute(worker worker.Worker) { + cc.Init(&cc.Config{ + RootCmd: rootCmd, + Headings: cc.Bold, + Commands: cc.Yellow + cc.Bold, + Example: cc.Italic, + ExecName: cc.HiMagenta + cc.Bold, + Flags: cc.Green, + FlagsDataType: cc.Italic + cc.White, + }) + // Store the worker in the context so we can pull it out later + ctx := context.WithValue(context.Background(), sdkcontext.WorkerContextKey, worker) + err := rootCmd.ExecuteContext(ctx) + if err != nil { + os.Exit(1) + } +} + +func init() { + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", + "config file (default is $HOME/airflow/go-sdk.yaml)") + rootCmd.AddCommand(runCmd) +} + +func setupViper() error { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + airflowHome := os.Getenv("AIRFLOW_HOME") + if airflowHome == "" { + home, err := os.UserHomeDir() + cobra.CheckErr(err) + airflowHome = path.Join(home, "airflow") + } + + // Search config in home directory with name ".go-sdk" (without extension). + viper.AddConfigPath(airflowHome) + viper.SetConfigType("yaml") + viper.SetConfigName("go-sdk.yaml") + } + + viper.SetOptions(viper.ExperimentalBindStruct()) + + // Attempt to read the config file, gracefully ignoring errors + // caused by a config file not being found. Return an error + // if we cannot parse the config file. + if err := viper.ReadInConfig(); err != nil { + // It's okay if there isn't a config file + if _, ok := err.(viper.ConfigFileNotFoundError); !ok { + return err + } + } + + // Make the prefix be AIRFLOW__ -- viper adds an extra `_` automatically + viper.SetEnvPrefix("AIRFLOW_") + // Set the key replacer to replace "__" with "." + replacer := strings.NewReplacer(".", "__", "-", "_") + viper.SetEnvKeyReplacer(replacer) + + viper.AutomaticEnv() // read in environment variables that match + return nil +} + +// initConfig reads in config file and ENV variables if set. +func initializeConfig(cmd *cobra.Command) error { + if err := setupViper(); err != nil { + return err + } + + // Set the key replacer to replace "__" with "." + replacer := strings.NewReplacer(".", "__", "-", "_") + // Bind the current command's flags to viper + bindFlags(cmd, replacer) + + return nil +} + +// Bind each cobra flag to its associated viper configuration (config file and environment variable) +// This approach cribbed from https://github.com/carolynvs/stingoftheviper/blob/19bd73117f0285436505ca17616cbc394d22e63d/main.go +func bindFlags(cmd *cobra.Command, replacer *strings.Replacer) { + cmd.Flags().VisitAll(func(f *pflag.Flag) { + // Determine the naming convention of the flags when represented in the config file + + // Since viper does case-insensitive comparisons, we don't need to bother fixing the case, and only need to remove the hyphens. + configName := replacer.Replace(f.Name) + + // Use cli flags for preference over env or config file! + if f.Changed { + // Apply the viper config value to the flag when the flag is not set and viper has a value + if slice, ok := f.Value.(pflag.SliceValue); ok { + viper.Set(configName, slice.GetSlice()) + } else { + viper.Set(configName, f.Value.String()) + } + } else if viper.IsSet(configName) { + // If we have a viper config but no flag, set the flag value. This lets `MarkRequiredFlag` work + val := viper.Get(configName) + fmt.Printf("%s %t %#v\n", configName, val, val) Review Comment: Yeah, this was debugging/behaviour testing left over. removing it -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
