Skip to content

Instantly share code, notes, and snippets.

@losinggeneration
Last active April 21, 2018 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save losinggeneration/4b4fe7d5c16cdcbefb7e0f2244ce5378 to your computer and use it in GitHub Desktop.
Save losinggeneration/4b4fe7d5c16cdcbefb7e0f2244ce5378 to your computer and use it in GitHub Desktop.
Viper PFlag nested binding
  • go run main.go -c app.yml works as expected and unmarshalls Remote.Host="a good host"
  • go run main.go -c app.yml -n "new host" seems to have the correct structure when calling AllSettings, but unmarshal doesn't get set from the command line
  • go run main.go -c no-app.yml panics
  • go run main.go -c no-app.yml -n "new host" panics
  • go run main.go -n "new host" panics
  • go run main.go panics

panic

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xd0 pc=0x750e25]

goroutine 1 [running]:
viper_test/vendor/github.com/spf13/viper.(*Viper).AllKeys(0x0, 0x30, 0x804c60, 0xc420175e01)
        viper_test/vendor/github.com/spf13/viper/viper.go:1582 +0x45
viper_test/vendor/github.com/spf13/viper.(*Viper).AllSettings(0x0, 0x7492d1)
        viper_test/vendor/github.com/spf13/viper/viper.go:1662 +0x51
viper_test/vendor/github.com/spf13/viper.(*Viper).Unmarshal(0x0, 0x781220, 0xc42009b060, 0x0, 0x0)
        viper_test/vendor/github.com/spf13/viper/viper.go:759 +0x2f
main.main()
        viper_test/main.go:36 +0x1bd
exit status 2
app:
remote:
host: "a good host"
[[projects]]
name = "github.com/fsnotify/fsnotify"
packages = ["."]
revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9"
version = "v1.4.7"
[[projects]]
branch = "master"
name = "github.com/hashicorp/hcl"
packages = [".","hcl/ast","hcl/parser","hcl/printer","hcl/scanner","hcl/strconv","hcl/token","json/parser","json/scanner","json/token"]
revision = "ef8a98b0bbce4a65b5aa4c368430a80ddc533168"
[[projects]]
name = "github.com/magiconair/properties"
packages = ["."]
revision = "c3beff4c2358b44d0493c7dda585e7db7ff28ae6"
version = "v1.7.6"
[[projects]]
branch = "master"
name = "github.com/mitchellh/mapstructure"
packages = ["."]
revision = "00c29f56e2386353d58c599509e8dc3801b0d716"
[[projects]]
name = "github.com/pelletier/go-toml"
packages = ["."]
revision = "acdc4509485b587f5e675510c4f2c63e90ff68a8"
version = "v1.1.0"
[[projects]]
name = "github.com/spf13/afero"
packages = [".","mem"]
revision = "63644898a8da0bc22138abf860edaf5277b6102e"
version = "v1.1.0"
[[projects]]
name = "github.com/spf13/cast"
packages = ["."]
revision = "8965335b8c7107321228e3e3702cab9832751bac"
version = "v1.2.0"
[[projects]]
branch = "master"
name = "github.com/spf13/jwalterweatherman"
packages = ["."]
revision = "7c0cea34c8ece3fbeb2b27ab9b59511d360fb394"
[[projects]]
name = "github.com/spf13/pflag"
packages = ["."]
revision = "583c0c0531f06d5278b7d917446061adc344b5cd"
version = "v1.0.1"
[[projects]]
name = "github.com/spf13/viper"
packages = ["."]
revision = "b5e8006cbee93ec955a89ab31e0e3ce3204f3736"
version = "v1.0.2"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
packages = ["unix"]
revision = "79b0c6888797020a994db17c8510466c72fe75d9"
[[projects]]
name = "golang.org/x/text"
packages = ["internal/gen","internal/triegen","internal/ucd","transform","unicode/cldr","unicode/norm"]
revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
version = "v0.3.0"
[[projects]]
name = "gopkg.in/yaml.v2"
packages = ["."]
revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183"
version = "v2.2.1"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "687b252626f2925730598945dd483d5b8e544e202167f2ecff05239289073e91"
solver-name = "gps-cdcl"
solver-version = 1
[[constraint]]
name = "github.com/spf13/pflag"
version = "1.0.1"
[[constraint]]
name = "github.com/spf13/viper"
version = "1.0.2"
package main
import (
"log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type opts struct {
Remote struct {
Host string
}
}
func main() {
failOnError := func(err error) {
if err != nil {
log.Fatal(err)
}
}
cfg := pflag.StringP("config", "c", "", "config to load from")
pflag.StringP("host", "n", "", "a hostname")
failOnError(viper.BindPFlag("app.remote.host", pflag.Lookup("host")))
pflag.Parse()
if *cfg != "" {
viper.SetConfigFile(*cfg)
failOnError(viper.ReadInConfig())
}
var o opts
failOnError(viper.Sub("app").Unmarshal(&o))
log.Printf("%#v", viper.AllSettings())
log.Printf("%#v", o)
}
remote:
host: "a good host"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment