Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ var DialTimeout = 10 * time.Second

func cmdZenoGetURL(urls []string) *cobra.Command {
cmd := cmd.Prepare()
args := append([]string{"get", "url", "--config-file", "config.toml", "--log-e2e", "--log-e2e-level", "debug", "--no-stdout-log", "--no-stderr-log"}, urls...)

// If config.toml exists in pwd, include it in args
var args []string
commonArgs := []string{"get", "url", "--log-e2e", "--log-e2e-level", "debug", "--no-stdout-log", "--no-stderr-log"}
if _, err := os.Stat("config.toml"); err == nil {
args = append(commonArgs,append([]string{"--config-file", "config.toml"}, urls...)...)
} else {
args = append(commonArgs, urls...)
}

fmt.Println("Command arguments:", args)
cmd.SetArgs(args)
return cmd
Expand Down
24 changes: 21 additions & 3 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func InitConfig() error {
config.SetContext(context.Background())

// Check if a config file is provided via flag
configFileProvided := viper.GetString("config-file") != ""
if configFile := viper.GetString("config-file"); configFile != "" {
viper.SetConfigFile(configFile)
} else {
Expand All @@ -230,13 +231,30 @@ func InitConfig() error {
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()

if err = viper.ReadInConfig(); err == nil {
err = viper.ReadInConfig()
if err != nil {
if configFileProvided {
// User explicitly provided a config file, any error should be reported
err = fmt.Errorf("error reading config file: %w", err)
return
} else {
// Using default config file location
// Only report errors for parsing issues, not for file not found
if _, isNotFoundError := err.(viper.ConfigFileNotFoundError); !isNotFoundError {
Comment thread
yzqzss marked this conversation as resolved.
// Config file exists but has errors (e.g., invalid YAML)
err = fmt.Errorf("error reading config file: %w", err)
return
}
// Config file doesn't exist at default location, which is OK
err = nil // Clear the error since file not found is OK for default config
}
} else {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}

if viper.GetBool("consul-config") && viper.GetString("consul-address") != "" {
var consulAddress *url.URL
consulAddress, err = url.Parse(viper.GetString("consul-address"))
consulAddress, err := url.Parse(viper.GetString("consul-address"))
if err != nil {
return
}
Expand All @@ -246,7 +264,7 @@ func InitConfig() error {
viper.SetConfigType(filepath.Ext(consulFile))
viper.SetConfigName(strings.TrimSuffix(consulFile, filepath.Ext(consulFile)))

if err = viper.ReadInConfig(); err == nil {
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
Expand Down
Loading