github.com/landoop/schema-registry@v0.0.0-20190327143759-50a5701c1891/schema-registry-cli/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  
     9  	"github.com/fatih/color"
    10  	schemaregistry "github.com/landoop/schema-registry"
    11  
    12  	"github.com/spf13/cobra"
    13  	"github.com/spf13/viper"
    14  )
    15  
    16  var (
    17  	cfgFile     string
    18  	registryURL string
    19  	verbose     bool
    20  	nocolor     bool
    21  )
    22  
    23  // RootCmd represents the base command when called without any subcommands
    24  var RootCmd = &cobra.Command{
    25  	Use:   "schema-registry-cli",
    26  	Short: "A command line interface for the Confluent schema registry",
    27  	Long:  `A command line interface for the Confluent schema registry`,
    28  	PersistentPreRun: func(cmd *cobra.Command, args []string) {
    29  		if !verbose {
    30  			log.SetOutput(ioutil.Discard)
    31  		}
    32  		if nocolor {
    33  			color.NoColor = true
    34  		}
    35  		log.Printf("schema registry url: %s\n", viper.Get("url"))
    36  	},
    37  }
    38  
    39  // Execute adds all child commands to the root command sets flags appropriately.
    40  // This is called by main.main(). It only needs to happen once to the rootCmd.
    41  func Execute() {
    42  	if err := RootCmd.Execute(); err != nil {
    43  		fmt.Println(err)
    44  		os.Exit(-1)
    45  	}
    46  }
    47  
    48  func init() {
    49  	RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "be verbose")
    50  	RootCmd.PersistentFlags().BoolVarP(&nocolor, "no-color", "n", false, "dont color output")
    51  	RootCmd.PersistentFlags().StringVarP(&registryURL, "url", "e", schemaregistry.DefaultURL, "schema registry url, overrides SCHEMA_REGISTRY_URL")
    52  	viper.SetEnvPrefix("schema_registry")
    53  	viper.BindPFlag("url", RootCmd.PersistentFlags().Lookup("url"))
    54  	viper.BindEnv("url")
    55  }