cmd

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "wfxctl",
	Short: "wfxctl is a command-line tool to interact with the wfx REST API",
	Long: `wfxctl can be used for management or diagnostic purposes.

To see raw HTTP requests and responses, export the environment variable DEBUG=true.

Tip: Shell completion is available for Bash, Fish and Zsh. See wfxctl completion --help for more information.
`,
	SilenceUsage:     true,
	TraverseChildren: true,
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		f := cmd.Flags()
		if level, err := f.GetString(logLevelFlag); err == nil {
			if lvl, err := zerolog.ParseLevel(level); err == nil {
				zerolog.SetGlobalLevel(lvl)
			}
		}

		configFiles, _ := f.GetStringSlice(flags.ConfigFlag)
		log.Debug().Strs("configFiles", configFiles).Msg("Checking config files")
		for _, fname := range configFiles {
			if _, err := os.Stat(fname); err == nil {
				log.Debug().Str("fname", fname).Msg("Loading config file")
				prov := file.Provider(fname)
				if err := flags.Koanf.Load(prov, yaml.Parser()); err != nil {
					log.Fatal().Err(err).Msg("Failed to parse config file")
				}
			}
		}

		if err := flags.Koanf.Load(env.Provider("WFX_", ".", func(s string) string {
			result := strings.ReplaceAll(
				strings.ToLower(strings.TrimPrefix(s, "WFX_")), "_", "-")
			return result
		}), nil); err != nil {
			log.Err(err).Msg("Failed to env variables")
		}

		if err := flags.Koanf.Load(posflag.Provider(cmd.Flags(), ".", flags.Koanf), nil); err != nil {
			log.Fatal().Err(err).Msg("Failed to load pflags")
		}

		log.Logger = zerolog.New(zerolog.ConsoleWriter{
			Out:        os.Stderr,
			TimeFormat: time.Stamp,
		}).With().Timestamp().Logger()
		if lvl, err := zerolog.ParseLevel(flags.Koanf.String(logLevelFlag)); err == nil {
			zerolog.SetGlobalLevel(lvl)
		}

		log.Debug().
			Str("version", metadata.Version).
			Str("date", metadata.Date).
			Str("commit", metadata.Commit).
			Msg("wfxctl")

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL