• Stars
    star
    121
  • Rank 292,864 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Share configuration between spf13/cobra and spf13/viper

Sting of the Viper

This demonstrates how to integrate spf13/cobra with spf13/viper such command-line flags have the highest precedence, then environment variables, then config file values, and then defaults set on command-line flags.

πŸ“¬ Read the accompanying blog post that explains this example code. Sting of the Viper: Getting Cobra and Viper to work together

It also handles binding command-line flags that have dashes properly to environment variables with underscores. For example, --favorite-color is set with the environment variable FAVORITE_COLOR.

Try it out

First grab the source code with go get or by cloning it. Change into the directory of this repository.

go get github.com/carolynvs/stingoftheviper
# or
git clone https://github.com/carolynvs/stingoftheviper.git
cd stingoftheviper/

Now let's build the CLI (stingoftheviper) and make sure everything is still working:

go build .
go test ./...

We are now ready to try out a few scenarios to test out the precedence order. First let's run it with no flags or environment variables.

$ ./stingoftheviper
Your favorite color is: blue
The magic number is: 7

If you take a peek at the config file, you will see that only the favorite-color was set there. So favorite-color got its value from the config file, while magic number got its value from the flag's default value set in main.go. So the lowest precedence is the flag default, followed by the config file.

Let's try setting an environment variable.

$ STING_FAVORITE_COLOR=purple ./stingoftheviper
Your favorite color is: purple
The magic number is: 7

There's two interesting things going on here. One is that the environment variable has higher precedence than the config file value obviously. The other is that not only was the environment variable automatically bound to the flag, but we handled swapping the dashes for underscores in the binding (which isn't done for us in the library, you have to do that yourself).

To finish things off, let's actually use a flag.

$ ./stingoftheviper --number 2
Your favorite color is: blue
The magic number is: 2

MAGIC! 🎩✨

More Repositories

1

emote

Go
32
star
2

magex

Helper methods for Magefiles
Go
28
star
3

handbrk8s

Handbrake on Kubernetes
Go
21
star
4

howtowhale

howtowhale.com
HTML
20
star
5

cfp

Scratch pad for my conference proposals and talks
11
star
6

nutritionix

This is a .NET client for the Nutritionix API
C#
8
star
7

kubectl-kill-plugin

Example kubectl plugin that force deletes pods
Go
8
star
8

vscode-dep

dep extension for Visual Studio Code
Makefile
7
star
9

rollersk8s

Run your pony cluster on roller sk8s!
Shell
7
star
10

telephone

Telephone Gophers Workshop
Go
6
star
11

kubectl-flags-plugin

Example kubectl plugin that dumps all flags passed in
Makefile
6
star
12

az-cli

unofficial helper binary for uploading to azure storage
Go
5
star
13

whalesayd

The whales have taken over your webserver 🐳
Go
5
star
14

kubectl-wait-plugin

kubectl plugin that waits for a kubernetes resource to be ready
Go
3
star
15

handbrakecli

Docker container for the HandBrake CLI
Makefile
3
star
16

wheatley

The smartest robot in the world
CoffeeScript
2
star
17

go-docker

Makefile
2
star
18

magic

A magical app which demonstrates cross-platform tips and tricks for golang
PowerShell
2
star
19

deptest-importers

Go
2
star
20

cloudkinds

Prototype for portable service definitions KEP
Go
2
star
21

assign-myself

GitHub app that lets contributors assign themselves to issues
TypeScript
1
star
22

whaleinabox-kit

Your very own howtowhale in a box!
PowerShell
1
star
23

mage-example

Example magefile
Go
1
star
24

github-release-proxy

Python
1
star
25

wwg

Ruby
1
star
26

cloudkinds-servicecatalog

Go
1
star
27

chocolatey-packages

Packages for Chocolatey
PowerShell
1
star
28

porter-cowsay

moar cows
Go
1
star
29

bamboo-gitversion

GitVersion for Bamboo
Java
1
star
30

contributor-strategy-talks

Slides for CNCF SIG Contributor Strategy talks
HTML
1
star
31

install-tools-with-gomod

Demonstrates how to install tools with go.mod + Makefile
Go
1
star
32

cli-tutorial

Tutorial repository for building a great cli in Go
1
star
33

whaleinabox

Your very own howtowhale in a box!
Shell
1
star