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

fix: ensure config file is found #23

Merged
merged 1 commit into from
Oct 10, 2024
Merged
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
18 changes: 17 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -60,14 +63,26 @@ type Serve struct {
}

func NewConfig() (*Config, error) {
userDir, err := os.UserHomeDir()
if err != nil {
log.Warn().Msgf("Could not get user home directory: %v", err)
}

execFile, err := os.Executable()
if err != nil {
log.Warn().Msgf("Could not get file path of executable: %v", err)
}
execDir := filepath.Dir(execFile)

v := viper.New()

v.SetEnvPrefix("HCTL")
v.AutomaticEnv()
v.SetConfigType("yaml")
v.SetConfigName("hctl")
v.AddConfigPath("$HOME/.config/hctl")
v.AddConfigPath(".")
v.AddConfigPath(path.Join(userDir, ".config/hctl"))
v.AddConfigPath(execDir)

// create empty config and set defaults
cfg := &Config{}
Expand Down Expand Up @@ -105,6 +120,7 @@ func NewConfig() (*Config, error) {
zerolog.SetGlobalLevel(lvl)
}

log.Info().Msgf("Config file in use: %s", v.ConfigFileUsed())
log.Debug().Msgf("Running with the following config: %+v", cfg)

cfg.Viper = v
Expand Down