|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/viper" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + fileOption = "file" |
| 14 | + configDirOption = "config-dir" |
| 15 | + outputDirOption = "output-dir" |
| 16 | + templatesDirOption = "templates" |
| 17 | + showDefinitionsOption = "show-definitions" |
| 18 | +) |
| 19 | + |
| 20 | +// RootCmd defines the root cli command |
| 21 | +func RootCmd() *cobra.Command { |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "kubernetes-api-reference", |
| 24 | + Short: "K8s API documentation tools", |
| 25 | + Long: `Tool to build documentation from OpenAPI specification of the Kubernetes API`, |
| 26 | + SilenceErrors: true, |
| 27 | + SilenceUsage: true, |
| 28 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 29 | + viper.BindPFlags(cmd.Flags()) |
| 30 | + }, |
| 31 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 32 | + return nil |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + cmd.PersistentFlags().StringP(fileOption, "f", "", "OpenAPI spec file") |
| 37 | + cmd.MarkFlagRequired(fileOption) |
| 38 | + |
| 39 | + subcommands := []func() *cobra.Command{ |
| 40 | + ResourceslistCmd, ShowTOCCmd, GVKeysMap, KWebsite, |
| 41 | + } |
| 42 | + for _, subcommand := range subcommands { |
| 43 | + cmd.AddCommand(subcommand()) |
| 44 | + } |
| 45 | + |
| 46 | + cobra.OnInitialize(initConfig) |
| 47 | + |
| 48 | + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) |
| 49 | + return cmd |
| 50 | +} |
| 51 | + |
| 52 | +// Run the cli |
| 53 | +func Run() { |
| 54 | + if err := RootCmd().Execute(); err != nil { |
| 55 | + fmt.Println(err) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func initConfig() { |
| 61 | + viper.AutomaticEnv() |
| 62 | +} |
0 commit comments