Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
		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": {
				"this": "that"
			}
		}`))

		Expect(err).NotTo(HaveOccurred())
		Expect(config.WriterType).To(Equal("SomeNeatWriter"))
		Expect(config.ReaderType).To(Equal("SomeNeatReader"))
		Expect(config.SchemeType).To(Equal("SomeNeatScheme"))

		this, ok := config.Additional["this"]
		Expect(ok).To(BeTrue())
		value := this.(string)
		Expect(value).To(Equal("that"))
	})
})