From a9fc991dbbed9dc24c2a8d3ebe71ae7cae471314 Mon Sep 17 00:00:00 2001 From: guyfedwards Date: Thu, 9 Nov 2023 10:11:01 +0000 Subject: [PATCH] remove error on no config file, create file instead closes #42 --- .github/workflows/release.yml | 2 +- cmd/nom/main.go | 4 ---- internal/config/config.go | 22 +++++++++++++--------- internal/config/config_test.go | 22 ++++++++++++++++++++++ 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ab571c7..84b1898 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,7 +19,7 @@ jobs: name: Set up Go uses: actions/setup-go@v3 with: - go-version: '>=1.19.0' + go-version: '>=1.21.3' - name: Run GoReleaser uses: goreleaser/goreleaser-action@v4 diff --git a/cmd/nom/main.go b/cmd/nom/main.go index 6e99393..bffa700 100644 --- a/cmd/nom/main.go +++ b/cmd/nom/main.go @@ -77,10 +77,6 @@ func main() { fmt.Printf("%v\n", err) } - if errors.Is(err, config.ErrMissingConfig) { - fmt.Printf("Missing config file. \nAdd $XDG_CONFIG_HOME/nom/config.yml. See docs for config options.\n\n") - } - parser.WriteHelp(os.Stderr) os.Exit(1) } diff --git a/internal/config/config.go b/internal/config/config.go index fe91de5..4640efc 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,8 +10,6 @@ import ( "gopkg.in/yaml.v3" ) -var ErrMissingConfig = errors.New("missing config.yml") - type Feed struct { URL string `yaml:"url"` Name string `yaml:"name,omitempty"` @@ -35,7 +33,7 @@ type Backends struct { type Config struct { configPath string - ConfigDir string + ConfigDir string `yaml:"-"` Pager string `yaml:"pager,omitempty"` Feeds []Feed `yaml:"feeds"` // Preview feeds are distinguished from Feeds because we don't want to inadvertenly write those into the config file. @@ -93,7 +91,7 @@ func (c *Config) Load() error { rawData, err := os.ReadFile(c.configPath) if err != nil { - return fmt.Errorf("config.Load: %w", ErrMissingConfig) + return fmt.Errorf("config.Load: %w", err) } // manually set config values from fileconfig, messy solve for config priority @@ -182,20 +180,26 @@ func (c *Config) GetFeeds() []Feed { } func setupConfigDir(configDir string) error { - // if configpath already exists, exit early - if _, err := os.Stat(configDir); !errors.Is(err, os.ErrNotExist) { + configFile := filepath.Join(configDir, "/config.yml") + + _, err := os.Stat(configFile) + + // if configFile exists, do nothing + if !errors.Is(err, os.ErrNotExist) { return nil } - err := os.MkdirAll(configDir, 0755) + // if not, create directory. noop if directory exists + err = os.MkdirAll(configDir, 0755) if err != nil { return fmt.Errorf("setupConfigDir: %w", err) } - _, err = os.Create(configDir) + // then create the file + _, err = os.Create(configFile) if err != nil { return fmt.Errorf("setupConfigDir: %w", err) } - return nil + return err } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index ed664aa..949d2ba 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -3,6 +3,7 @@ package config import ( "fmt" "os" + "path/filepath" "testing" "gopkg.in/yaml.v3" @@ -12,6 +13,12 @@ import ( const configFixturePath = "../test/data/config_fixture.yml" const configFixtureWritePath = "../test/data/config_fixture_write.yml" +const configDir = "../test/data/nom" +const configPath = "../test/data/nom/config.yml" + +func cleanup() { + os.RemoveAll(configDir) +} func TestNewDefault(t *testing.T) { c, _ := New("", "", []string{}) @@ -107,3 +114,18 @@ func TestConfigAddFeed(t *testing.T) { t.Fatalf("did not write feed correctly") } } + +func TestConfigSetupDir(t *testing.T) { + err := setupConfigDir(configDir) + if err != nil { + t.Fail() + return + } + + _, err = os.Stat(filepath.Join(configDir, "config.yml")) + if err != nil { + t.Fail() + } + + cleanup() +}