kirk-patton / cobra-precedence

Explore Cobra/Viper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cobra-precedence

Sample Cobra Project demonstrating cobra & viper precedence

This sample Cobra project demonstrates how to load values for multiple locations.

  • flag default value
  • config file
  • environment variable
  • explicit call to --flag

The project uses Viper as the source of truth for the values collected. It was written to better understand Cobra. The overall idea is that cobra/viper logic is separate from the core logic of whatever the client app is supposed to do. That logic should be contained in a separate file/library with its own unit tests. Cobra/viper just collects the settings to pass along. This test program does not do anything useful other that demonstrate precedence.

The tests start with no flags or config file and add them one test at a time.

Notes

When testing, flags/arguments are passed to the "rootCmd". The same holds true for calls to rootCmd.Execute. This is the same flow as a cli application would use, the difference being that we supply arguments using cmd.SetArgs([]string) which allows to control the argument during testing.

When setting up a cobra "command", we use RunE. It allows flexibility to return and check errors from the command.

The output of the RunE command is set to return an iowriter so that the output can be captured in a buffer for examination but the test program.

The "command" we have specified configures viper.

Reference

About

Explore Cobra/Viper


Languages

Language:Go 100.0%