Skip to content

Commit

Permalink
feat: reading config from dot directory (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
CallumKerson authored Apr 4, 2023
1 parent 29fcc37 commit 3e64bfb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
3 changes: 1 addition & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ brews:
test: |
system "#{bin}/athenaeum --version"
service: |
run [opt_bin/"athenaeum", "run" ,"--config", etc/"athenaeum.yaml"]
environment_variables ATHENAEUM_DB_ROOT: var/"athenaeum"
run [opt_bin/"athenaeum", "run"]
keep_alive true
error_log_path var/"log/athenaeum.log"
log_path var/"log/athenaeum.log"
Expand Down
28 changes: 22 additions & 6 deletions cmd/athenaeum/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/spf13/viper"
)

const (
defaultPort = 8080
defaultPort = 8080
defaultConfigDir = ".athenaeum"
)

type Config struct {
Expand Down Expand Up @@ -66,7 +69,6 @@ func InitConfig(cfg *Config, pathToConfigFile string, out io.Writer) error {
viper.SetDefault("Podcast.PreUnixEpoch.Handle", true)
viper.SetDefault("Media.HostPath", "/media")
viper.SetDefault("Media.Root", "/srv/media")
viper.SetDefault("DB.Root", "/usr/local/athenaeum")
viper.SetDefault("ThirdParty.UpdateOvercast", false)
viper.SetDefault("ThirdParty.NotifyOvercast", false)
viper.SetDefault("Port", defaultPort)
Expand Down Expand Up @@ -96,14 +98,28 @@ func InitConfig(cfg *Config, pathToConfigFile string, out io.Writer) error {
}

if pathToConfigFile == "" {
fmt.Fprintln(out, "No valid config path provided by flag or found from environment variable ATHENAEUM_CONFIG_PATH,",
"reading config from environment variables only")
home, err := os.UserHomeDir()
if err != nil {
return err
}
viper.AddConfigPath(filepath.Join(home, defaultConfigDir))
viper.SetConfigType("yaml")
viper.SetConfigName("config")
} else {
viper.SetConfigFile(pathToConfigFile)
err := viper.ReadInConfig()
}
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(out, "Using config file:", viper.ConfigFileUsed())
} else {
fmt.Fprintln(out, "No valid config path provided by flag or found from environment variable ATHENAEUM_CONFIG_PATH,",
"reading config from environment variables only")
}
if viper.GetString("DB.Root") == "" {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("config error: %w", err)
return err
}
viper.SetDefault("DB.Root", filepath.Join(home, defaultConfigDir, "data"))
}

err := viper.Unmarshal(cfg)
Expand Down
6 changes: 1 addition & 5 deletions cmd/athenaeum/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"io/fs"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -138,10 +137,7 @@ func TestConfig_BadFile(t *testing.T) {
err := InitConfig(&config, "", &bytes.Buffer{})

// then
if assert.Error(t, err) {
var expectedErr *fs.PathError
assert.ErrorAs(t, err, &expectedErr)
}
assert.NoError(t, err)
}

func envVarSetter(t *testing.T, envs map[string]string) (closer func()) {
Expand Down

0 comments on commit 3e64bfb

Please sign in to comment.