Giter Site home page Giter Site logo

kunmingliu / stingoftheviper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from carolynvs/stingoftheviper

0.0 0.0 0.0 45 KB

Share configuration between spf13/cobra and spf13/viper

Home Page: https://carolynvanslyck.com/blog/2020/08/sting-of-the-viper/

License: MIT License

Go 100.00%

stingoftheviper's Introduction

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! ๐ŸŽฉโœจ

stingoftheviper's People

Contributors

carolynvs avatar ezk84 avatar vkumbhar94 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.