func config(context *cli.Context) factory.Config { configPath := context.GlobalString("config") var config factory.Config var err error if configPath == "" { config, err = factory.Parse([]byte("{}")) } else { config, err = factory.LoadFromFile(configPath) } if err != nil { cli.ShowAppHelp(context) panic(err) } schemeType := context.GlobalString("scheme") if schemeType != "" { config.SchemeType = schemeType } writerType := context.GlobalString("writer") if writerType != "" { config.WriterType = writerType } readerType := context.GlobalString("reader") if readerType != "" { config.ReaderType = readerType } for _, assignment := range context.GlobalStringSlice("config-string") { keyValue, err := parseAssignment(assignment) if err != nil { panic(err) } config.Additional.SetString(keyValue[0], keyValue[1]) } for _, assignment := range context.GlobalStringSlice("config-int") { keyValue, err := parseAssignment(assignment) if err != nil { panic(err) } value, err := strconv.Atoi(keyValue[1]) if err != nil { panic(err) } config.Additional.SetInt(keyValue[0], value) } return config }
package factory_test import ( "github.com/myshkin5/netspel/factory" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Config", func() { It("loads config from a file", func() { config, err := factory.LoadFromFile("./simple.json") Expect(err).NotTo(HaveOccurred()) Expect(config.WriterType).To(Equal("udp")) Expect(config.ReaderType).To(Equal("udp")) Expect(config.SchemeType).To(Equal("simple")) }) It("returns an error when attempting to load a non-existent file", func() { _, err := factory.LoadFromFile("./not-there.json") Expect(err).To(HaveOccurred()) }) It("parses a JSON object and stores the results", func() { config, err := factory.Parse([]byte(`{ "writer-type": "SomeNeatWriter", "reader-type": "SomeNeatReader", "scheme-type": "SomeNeatScheme", "additional": {