Skip to content

bad values in configfile or envVar silently ignored #21

@woodsaj

Description

@woodsaj

If the value of an option is set in the configfile or environment var with an invalid value that can't be parsed, it is just silently ignored and the 0 value for the data type is used (even if a "default" is defined)

This can be highlighted with the following example code.

package main

import (
    "log"
    "flag"
    "github.com/rakyll/globalconf"
)

var myInt = flag.Int("int", 1,"integer config item")

func main() {
    conf, _ := globalconf.NewWithOptions(&globalconf.Options{
        EnvPrefix: "T_",
    })
    conf.ParseAll()
    log.Printf("myInt is set to %d\n", *myInt)
}

use default value

$ go run /tmp/bad-conf.go
2017/05/23 15:52:16 myInt is set to 1

use non-integer value as command-line arg. An error is correctly raised.

$ go run /tmp/bad-conf.go -int foo
invalid value "foo" for flag -int: strconv.ParseInt: parsing "foo": invalid syntax
Usage of /tmp/go-build084013186/command-line-arguments/_obj/exe/bad-conf:
  -int int
    	integer config item (default 1)
exit status 2

use a non-integer value as an environment var. No error is raised and the flag is reset to the 0 value.

$ T_INT=foo go run /tmp/bad-conf.go
2017/05/23 15:54:51 myInt is set to 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions