1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/config/source/cli
Vasiliy Tolstov 67acd9288b
config/source/cli: fix tests (#1179)
* config/source/cli: fix tests
* skip mdns test in travis

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-02-08 02:45:32 +03:00
..
cli_test.go config/source/cli: fix tests (#1179) 2020-02-08 02:45:32 +03:00
cli.go config/source/cli: fix default flag value loading (#1178) 2020-02-08 02:14:34 +03:00
options.go fix import paths for v2 release 2020-01-30 14:44:40 +03:00
README.md add config 2019-05-30 23:11:13 +01:00
util.go update micro/cli to urfave/cli/v2 and fix go-micro 2020-01-25 15:57:08 +03:00

cli Source

The cli source reads config from parsed flags via a cli.Context.

Format

We expect the use of the micro/cli package. Upper case flags will be lower cased. Dashes will be used as delimiters for nesting.

Example

micro.Flags(
    cli.StringFlag{
        Name: "database-address",
        Value: "127.0.0.1",
        Usage: "the db address",
    },
    cli.IntFlag{
        Name: "database-port",
        Value: 3306,
        Usage: "the db port",
    },
)

Becomes

{
    "database": {
        "address": "127.0.0.1",
        "port": 3306
    }
}

New and Load Source

Because a cli.Context is needed to retrieve the flags and their values, it is recommended to build your source from within a cli.Action.


func main() {
    // New Service
    service := micro.NewService(
        micro.Name("example"),
        micro.Flags(
            cli.StringFlag{
                Name: "database-address",
                Value: "127.0.0.1",
                Usage: "the db address",
            },
        ),
    )

    var clisrc source.Source

    service.Init(
        micro.Action(func(c *cli.Context) {
            clisrc = cli.NewSource(
                cli.Context(c),
	    )
            // Alternatively, just setup your config right here
        }),
    )
    
    // ... Load and use that source ...
    conf := config.NewConfig()
    conf.Load(clisrc)
}