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

feat: specify config file as cmd argument #8

Merged
merged 1 commit into from
Sep 22, 2024
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
6 changes: 5 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ func PrintDefaults() error {
}

// Load and parse config, overlaying `Default` values.
func Load(path string) (*Config, error) {
func Load(path string, mustExist bool) (*Config, error) {
conf := Default()

b, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if mustExist {
return nil, err
}

slog.Warn("Config file not found, using defaults", "path", path)
return &conf, nil
}
Expand Down
42 changes: 28 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (

func main() {
var (
help = flag.Bool("help", false, "Print argument help message")
defaults = flag.Bool("defaults", false, "Prints default configuration to stdout and exits")
logPath = flag.String("log", "", "File to write debug logs to")
help = flag.Bool("help", false, "Print argument help message")
configPathOpt = flag.String("config", "", "Path to TOML config file")
defaults = flag.Bool("defaults", false, "Prints default configuration to stdout and exits")
logPath = flag.String("log", "", "File to write debug logs to")
)
flag.Parse()

Expand Down Expand Up @@ -50,20 +51,19 @@ func main() {
}

// Load config file if present.
configRoot := os.Getenv("XDG_CONFIG_HOME")
if configRoot == "" {
home := os.Getenv("HOME")
if home == "" {
fmt.Fprintln(os.Stderr, "Neither $XDG_CONFIG_HOME or $HOME available")
os.Exit(1)
}
configRoot = filepath.Join(home, ".config")
var configPath string
var configMustExist bool

if *configPathOpt == "" {
configPath = defaultConfigPath()
} else {
configPath = *configPathOpt
configMustExist = true
}

configPath := filepath.Join(configRoot, "labcoat", "config.toml")
conf, err := config.Load(configPath)
conf, err := config.Load(configPath, configMustExist)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read config %q: %v\n", configPath, err)
fmt.Fprintf(os.Stderr, "Failed to read config: %v\n", err)
os.Exit(1)
}

Expand Down Expand Up @@ -97,3 +97,17 @@ func main() {
os.Exit(1)
}
}

func defaultConfigPath() string {
configRoot := os.Getenv("XDG_CONFIG_HOME")
if configRoot == "" {
home := os.Getenv("HOME")
if home == "" {
fmt.Fprintln(os.Stderr, "Neither $XDG_CONFIG_HOME or $HOME available")
os.Exit(1)
}
configRoot = filepath.Join(home, ".config")
}

return filepath.Join(configRoot, "labcoat", "config.toml")
}