All Projects → jamiealquiza → Envy

jamiealquiza / Envy

Licence: mit
Envy automatically exposes environment variables for all of your Go flags

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Envy

Transit Map
Generate a schematic map (“metro map”) for a given (transit) network graph using Mixed Integer Programming.
Stars: ✭ 98 (-34.67%)
Mutual labels:  cli, library
Best Of Python
🏆 A ranked list of awesome Python open-source libraries and tools. Updated weekly.
Stars: ✭ 1,869 (+1146%)
Mutual labels:  cli, library
Unix Permissions
Swiss Army knife for Unix permissions
Stars: ✭ 106 (-29.33%)
Mutual labels:  cli, library
Css Flags
A collection of pure CSS flags, all single divs.
Stars: ✭ 90 (-40%)
Mutual labels:  library, flags
Csv2ofx
A Python library and command line tool for converting csv to ofx and qif files
Stars: ✭ 133 (-11.33%)
Mutual labels:  cli, library
Angular Librarian
An Angular 2+ scaffolding setup for creating libraries
Stars: ✭ 92 (-38.67%)
Mutual labels:  cli, library
Termux Extra Packages
Stars: ✭ 110 (-26.67%)
Mutual labels:  cli, environment
Envinject Plugin
This plugin makes it possible to setup a custom environment for your jobs
Stars: ✭ 74 (-50.67%)
Mutual labels:  environment, environment-variables
Natrium
A pre-build (Swift) script to alter your Xcode project at pre-build-time per environment, build configuration and target.
Stars: ✭ 131 (-12.67%)
Mutual labels:  cli, environment
Typin
Declarative framework for interactive CLI applications
Stars: ✭ 126 (-16%)
Mutual labels:  cli, library
Spider
A small dart library to generate Assets dart code from assets folder.
Stars: ✭ 90 (-40%)
Mutual labels:  cli, library
Fig
A minimalist Go configuration library
Stars: ✭ 142 (-5.33%)
Mutual labels:  environment, environment-variables
Kit
Tools for developing, documenting, and testing React component libraries
Stars: ✭ 1,201 (+700.67%)
Mutual labels:  cli, library
Gowebdav
A golang WebDAV client library and command line tool.
Stars: ✭ 97 (-35.33%)
Mutual labels:  cli, library
Instascrape
🚀 A fast and lightweight utility and Python library for downloading posts, stories, and highlights from Instagram.
Stars: ✭ 76 (-49.33%)
Mutual labels:  cli, library
Py7zr
7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.
Stars: ✭ 110 (-26.67%)
Mutual labels:  cli, library
Conf
Go package for loading program configuration from multiple sources.
Stars: ✭ 70 (-53.33%)
Mutual labels:  environment, environment-variables
Env Providers
👷 Load Laravel service providers based on your application's environment.
Stars: ✭ 73 (-51.33%)
Mutual labels:  environment, environment-variables
Riko
A Python stream processing engine modeled after Yahoo! Pipes
Stars: ✭ 1,571 (+947.33%)
Mutual labels:  cli, library
Simplecli
Command Line Interface Library for Arduino
Stars: ✭ 135 (-10%)
Mutual labels:  cli, library

GoDoc

envy

Automatically exposes environment variables for all of your flags. It supports the standard flags package along with limited support for Cobra commands.

Envy takes a namespace prefix that will be used for environment variable lookups. Each flag registered in your app will be prefixed, uppercased, and hyphens exchanged for underscores; if a matching environment variable is found, it will set the respective flag value as long as the value is not otherwise explicitly set (see usage for precedence).

Example: flag

Code:

package main

import (
        "flag"
        "fmt"

        "github.com/jamiealquiza/envy"
)

func main() {
        var address = flag.String("address", "127.0.0.1", "Some random address")
        var port = flag.String("port", "8131", "Some random port")

        envy.Parse("MYAPP") // Expose environment variables.
        flag.Parse()

        fmt.Println(*address)
        fmt.Println(*port)
}

Output:

# Prints flag defaults
% ./example
127.0.0.1
8131

# Setting flags via env vars.
% MYAPP_ADDRESS="0.0.0.0" MYAPP_PORT="9080" ./example
0.0.0.0
9080

Example: Cobra

Code:


// Where to execute envy depends on the structure
// of your Cobra implementation. A common pattern
// is to define a root command and an 'Execute' function
// that's called from the application main. We can call
// envy ParseCobra here and configure it to recursively
// update all child commands. Alternatively, it can be
// scoped to child commands at some point in their
// initialization.

var rootCmd = &cobra.Command{
 Use: "myapp",
}

func Execute() {
  // Configure envy.
  cfg := CobraConfig{
    // The env var prefix.
    Prefix: "MYAPP",
    // Whether to parse persistent flags.
    Persistent: true,
    // Whether to recursively update child command FlagSets.
    Recursive: true,
  }

  // Apply.
  envy.ParseCobra(rootCmd, cfg)

  if err := rootCmd.Execute(); err != nil {
    fmt.Println(err)
    os.Exit(1)
  }
}

Output:

# Root command flags.
% myapp
Usage:
  myapp [command]

Available Commands:
  help        Help about any command
  doathing    This is a subcommand

Flags:
  -h, --help               help for myapp
      --some-config        A global config [MYAPP_SOME_CONFIG]

Use "myapp [command] --help" for more information about a command.

# Child command flags. Notice that the prefix
# has the subcommand name automatically appended
# while preserving global/parent env vars.
% myapp doathing
Usage:
  myapp doathing [flags]

Flags:
  -h, --help               help for myapp
      --subcmd-config      Another config [MYAPP_DOATHING_SUBCMD_CONFIG]

Global Flags:
      --some-flag          A global flag [MYAPP_SOME_FLAG]

Usage

Variable precedence:

Envy results in the following order of precedence, each item overwriting the previous: flag default -> Envy generated env var -> flag set at the CLI.

Results referencing the stdlib flag example code:

  • ./example will result in port being set to 8131
  • MYAPP_PORT=5678 ./example will result in port being set to 5678
  • MYAPP_PORT=5678 ./example -port=1234 will result in port being set to 1234

Env vars in help output:

Envy can update your app help output so that it includes the environment variable generated/referenced for each flag. This is done by calling envy.Parse() before flag.Parse().

The above example:

Usage of ./example:
  -address string
        Some random address [MYAPP_ADDRESS] (default "127.0.0.1")
  -port string
        Some random port [MYAPP_PORT] (default "8131")

If this isn't desired, simply call envy.Parse() after flag.Parse():

// ...
	flag.Parse()
        envy.Parse("MYAPP") // looks for MYAPP_ADDRESS & MYAPP_PORT
// ...
Usage of ./example:
  -address string
        Some random address (default "127.0.0.1")
  -port string
        Some random port (default "8131")

Satisfying types:

Environment variables should be defined using a type that satisfies the respective type in your Go application's flag. For example:

  • string -> APP_ASTRINGVAR="someString"
  • int -> APP_ANINTVAR=42
  • bool -> APP_ABOOLVAR=true

Side effects:

Setting a flag through an Envy generated environment variable will have the same effects on the default flag.CommandLine as if the flag were set via the command line. This only affect users that may rely on flag.CommandLine methods that make distinctions between set and to-be set flags (such as the Visit method).

Cobra compatibility:

The extensive types in Cobra's underlying pflag have not been tested, hence the "limited support" reference.

Also, keep in mind that Cobra can change in a way that breaks support with envy. Functionality was tested as of 2018-11-19.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].