Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if file parse err no config read at all #79

Open
nevian427 opened this issue Aug 9, 2021 · 2 comments
Open

if file parse err no config read at all #79

nevian427 opened this issue Aug 9, 2021 · 2 comments

Comments

@nevian427
Copy link

Hi,

If file doesn't exists or corrupted - nothing got at all. TOML/ENV can be empty (null) to be ok all others - fail.
Expected to get params from env and/or default env

package main

import (
	"github.com/ilyakaznacheev/cleanenv"
	"github.com/spf13/pflag"
	"github.com/davecgh/go-spew/spew"
)

type Config struct {
	BindAddr   string `toml:"bindaddr" env:"YAACC_BINDADDR" env-default:":5013"`
	DBPort     int    `toml:"dbport" env:"YAACC_DBPORT" env-default:"5432"`
	DBHost     string `toml:"dbhost" env:"YAACC_DBHOST" env-default:"localhost"`
	DBName     string `toml:"dbname" env:"YAACC_DBNAME" env-default:"postgres"`
	DBUser     string `toml:"dbuser" env:"YAACC_DBUSER" env-default:"cdr"`
	DBPassword string `toml:"dbpassword" env:"YAACC_DBPASSWORD"`
	Logfile    string `toml:"logfile"`
}

func main() {
	cfgfile := pflag.StringP("config", "c", "./yaacc.toml", "File to read config params from")
	logfile := pflag.StringP("logfile", "l", "", "File to write exec time messages.")
	pflag.Lookup("logfile").NoOptDefVal = "./yaacc.log"
	pflag.Parse()

	jww.SetPrefix("yaacc")
	jww.SetLogThreshold(jww.LevelTrace)
	jww.SetStdoutThreshold(jww.LevelInfo)

	err := cleanenv.ReadConfig(*cfgfile, &cfg)
	if err != nil {
		jww.WARN.Printf("Error loading config (%s): %q", *cfgfile, err)
	}
	spew.Dump(cfg)
}
@mbauroth01
Copy link

Agree. Would be great to get this working even in absence of a yaml file!

@cloud-66
Copy link

cloud-66 commented Feb 19, 2023

I have that problem. I thought when there is no yaml config file it would get values from env or env-default, but it is not.
@ilyakaznacheev #94 Could you please review pr for this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants