Skip to content

Commit

Permalink
remove error on no config file, create file instead
Browse files Browse the repository at this point in the history
closes #42
  • Loading branch information
guyfedwards committed Nov 9, 2023
1 parent 552094a commit a9fc991
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions cmd/nom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 13 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
22 changes: 22 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"path/filepath"
"testing"

"gopkg.in/yaml.v3"
Expand All @@ -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{})
Expand Down Expand Up @@ -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()
}

0 comments on commit a9fc991

Please sign in to comment.