flagsconfig

package module
v0.0.0-...-4bd20d5 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2016 License: MIT Imports: 4 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

flagsconfig package

Go Report Card

[GoDoc] (https://godoc.org/github.com/dns-gh/flagsconfig)

Improved configuration management using a configuration file for user defined flags and used flags at runtime.

Motivation

To get a more clean and simple way to play with configuration files.

Installation

  • It requires Go language of course. You can set it up by downloading it here: https://golang.org/dl/
  • Install it here C:/Go.
  • Set your GOPATH, GOROOT and PATH environment variables:
export GOROOT=C:/Go
export GOPATH=WORKING_DIR
export PATH=C:/Go/bin:${PATH}
  • Download and install the package:
@working_dir $ go get github.com/dns-gh/flagsconfig/...
@working_dir $ go install github.com/dns-gh/flagsconfig

Example

File: testConfig.go

package main

import (
	"flag"
	"github.com/dns-gh/flagsconfig"
	"fmt"
)

func main() {
	filename := "test.config"
	firstFlag := flag.String("first", "firstDefault", "first user defined flag")
	otherFlag := flag.String("other", "otherDefault", "another user defined flag")

	// Makes the configuration structure with one filtered flag being the config flag
	// Hence, it will not be saved in the configuration file.
	// And parses the user defined flags and the one used by the user at runtime.
	// If the 'first' flag was used at runtime, the value of this flag will be
	// the one saved into the config file instead of the default one.
	_, err := flagsconfig.NewConfig(filename, "other")
	if err != nil {
		panic(err)
	}

	fmt.Println("filename:", filename)
	fmt.Println("first:", *firstFlag)
	fmt.Println("other:", *otherFlag)
}

So with

$ bin/testConfig.exe

we get the following output

filename: test.config
first: firstDefault
other: otherDefault

and the configuration file looks like:

{
    "first": "firstDefault"
}

but with

$ bin/testConfig.exe -first "alice"

we get the following output

filename: test.config
first: alice
other: otherDefault

and the configuration file looks like:

{
    "first": "alice"
}

if we run one more time

$ bin/testConfig.exe

we get

filename: test.config
first: alice
other: otherDefault

and the configuration file looks like:

{
    "first": "alice"
}

since the 'first' flag was not defined, the previous value contained in the config file was used.

Tests

@gotools $ go test -v github.com/dns-gh/flagsconfig
=== RUN   TestFlagsConfig
--- PASS: TestFlagsConfig (0.00s)
=== RUN   TestFlagsConfigFiltered
--- PASS: TestFlagsConfigFiltered (0.00s)
PASS
ok      flagsconfig     0.058s

LICENSE

See included LICENSE file.

Documentation

Overview

Package flagsconfig enables to save user defined key-value pairs and used flags at runtime into a configuration file written in json format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config represents the configuration file overloaded with user defined flags. There can be no configuration file and just flags.

func NewConfig

func NewConfig(path string, filter ...string) (*Config, error)

NewConfig makes a Config given a default config file path and a list of filtered flags not to appear in the config file. Note that the 'config' flag is defined by this method and added to the list of filtered flags.

func (*Config) Get

func (c *Config) Get(key string) string

Get gets a flag from a given key or return 0 value for a string if none.

func (*Config) Parse

func (c *Config) Parse(path string) error

Parse parses the given configuration file and overloads it with the currently used flags.

func (*Config) Update

func (c *Config) Update(key, value string) error

Update updates a pair of key-value flags from the flags map of Config and update the configuration file if any.

Jump to

Keyboard shortcuts

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