Beispiel #1
0
func main() {
	// Wont work if GOPATH contains multiple DIRs
	path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/adrianduke/configr/examples/simple-json/config.json")
	configr.AddSource(configr.NewFileSource(path))

	if err := configr.Parse(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fromAddress, err := configr.String(EmailFromAddressKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	emailSubject, err := configr.String(EmailSubjectKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	retryOnFail, err := configr.Bool(EmailRetryOnFailKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	maxRetries, err := configr.Int(EmailMaxRetriesKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	email := Email{
		FromAddress: fromAddress,
		Subject:     emailSubject,
		MaxRetries:  maxRetries,
		RetryOnFail: retryOnFail,
	}

	fmt.Printf("Gotten values:\n\t%#v\n\n", email)

	//
	// Generate blank config
	//
	f := configr.NewFileSource("")
	f.SetEncoding(configr.JSON)

	// f implements the encoder interface
	configBytes, err := configr.GenerateBlank(f)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("Blank config:\n%s", string(configBytes))
}
Beispiel #2
0
func main() {
	configr.AddSource(sources.NewEnvVars("configr"))

	if err := configr.Parse(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fromAddress, err := configr.String(EmailFromAddressKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	emailSubject, err := configr.String(EmailSubjectKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	retryOnFail, err := configr.Bool(EmailRetryOnFailKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	maxRetries, err := configr.Int(EmailMaxRetriesKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	email := Email{
		FromAddress: fromAddress,
		Subject:     emailSubject,
		MaxRetries:  maxRetries,
		RetryOnFail: retryOnFail,
	}

	fmt.Printf("Values:\n\t%#v\n\n", email)
}
Beispiel #3
0
func main() {
	// Wont work if GOPATH contains multiple DIRs
	path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/adrianduke/configr/examples/simple-json-inj/config.json")
	configr.AddSource(configr.NewFile(path))

	if err := configr.Parse(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	email := Email{}
	inj.Provide(&email)                     // Informs inj to perform DI on given instance
	inj.AddDatasource(configr.GetConfigr()) // Provides inj with a datasource to query

	if valid, errors := inj.Assert(); !valid { // Triggers the inj DI process
		fmt.Println(errors)
		os.Exit(1)
	}

	fmt.Println("> Email Address:", email.FromAddress)
	fmt.Println("> Subject:", email.Subject)
	fmt.Println("> Max Retries:", email.MaxRetries)
	fmt.Println("> Retry on Fail:", email.RetryOnFail)
}