From 6b75b08c4cc21fdc2e858afad1f647548e8178e1 Mon Sep 17 00:00:00 2001 From: Rob Gonnella Date: Wed, 20 Mar 2024 12:44:38 -0400 Subject: [PATCH] Fix issue with generating config on first run --- internal/config/repo-json.go | 44 ++++++++++++++++++++++++++++++------ internal/config/repo_test.go | 23 +++++++++++-------- internal/core/create.go | 17 +++++--------- 3 files changed, 56 insertions(+), 28 deletions(-) diff --git a/internal/config/repo-json.go b/internal/config/repo-json.go index e4a1edc..7412c11 100644 --- a/internal/config/repo-json.go +++ b/internal/config/repo-json.go @@ -15,17 +15,19 @@ import ( // JSONRepo is our repo implementation for json type JSONRepo struct { - configPath string - configs []*Config - mux sync.Mutex + configPath string + configs []*Config + defaultConfig Config + mux sync.Mutex } // NewJSONRepo returns a new ops repo for flat yaml file -func NewJSONRepo(configPath string) (*JSONRepo, error) { +func NewJSONRepo(configPath string, defaultConfig Config) (*JSONRepo, error) { repo := &JSONRepo{ - configPath: configPath, - configs: []*Config{}, - mux: sync.Mutex{}, + configPath: configPath, + configs: []*Config{}, + defaultConfig: defaultConfig, + mux: sync.Mutex{}, } if err := repo.load(); err != nil { @@ -192,6 +194,10 @@ func (r *JSONRepo) write() error { } func (r *JSONRepo) load() error { + if _, err := os.Stat(r.configPath); errors.Is(err, os.ErrNotExist) { + r.createDefaultConfig() + } + file, err := os.Open(r.configPath) if err != nil { @@ -217,6 +223,30 @@ func (r *JSONRepo) load() error { return nil } +func (r *JSONRepo) createDefaultConfig() error { + configs := Configs{ + Configs: []*Config{&r.defaultConfig}, + } + + file, err := os.OpenFile(r.configPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + + if err != nil { + return err + } + + defer file.Close() + + data, err := json.MarshalIndent(&configs, "", "\t") + + if err != nil { + return err + } + + _, err = file.Write(data) + + return err +} + // helpers func copyConfig(c *Config) *Config { return &Config{ diff --git a/internal/config/repo_test.go b/internal/config/repo_test.go index 515b682..3b15b23 100644 --- a/internal/config/repo_test.go +++ b/internal/config/repo_test.go @@ -22,7 +22,7 @@ func assertEqualConf(t *testing.T, expected, actual *config.Config) { } } -func TestConfigYamlRepo(t *testing.T) { +func TestConfigJsonRepo(t *testing.T) { testConfigFile := "config.json" file, err := os.Create(testConfigFile) @@ -32,6 +32,17 @@ func TestConfigYamlRepo(t *testing.T) { os.RemoveAll(testConfigFile) }() + defaultConf := config.Config{ + ID: "0", + Name: "default", + SSH: config.SSHConfig{ + User: "user", + Identity: "./id_rsa", + Port: "22", + Overrides: []config.SSHOverride{}, + }, + } + conf := config.Config{ ID: "1", Name: "myConfig", @@ -52,18 +63,10 @@ func TestConfigYamlRepo(t *testing.T) { assert.NoError(t, err) - repo, err := config.NewJSONRepo(testConfigFile) + repo, err := config.NewJSONRepo(testConfigFile, defaultConf) assert.NoError(t, err) - t.Run("returns error if cannot instantiate instance", func(st *testing.T) { - repo, err := config.NewJSONRepo("nope.json") - - assert.Nil(st, repo) - assert.Error(t, err) - - }) - t.Run("returns record not found error", func(st *testing.T) { _, err := repo.Get("10") diff --git a/internal/core/create.go b/internal/core/create.go index 98fe6e8..a733ba8 100644 --- a/internal/core/create.go +++ b/internal/core/create.go @@ -17,14 +17,15 @@ import ( "github.com/spf13/viper" ) -// getDefaultConfig creates and returns a default configuration -func getDefaultConfig(networkInfo network.Network) *config.Config { +// CreateNewAppCore creates and returns a new instance of *core.Core +func CreateNewAppCore(networkInfo network.Network, eventManager event.Manager, debug bool) (*Core, error) { + configPath := viper.Get("config-path").(string) user := viper.Get("user").(string) identity := viper.Get("default-ssh-identity").(string) seed := time.Now().UTC().UnixNano() nameGenerator := namegenerator.NewNameGenerator(seed) - return &config.Config{ + defaultConf := config.Config{ Name: nameGenerator.Generate(), SSH: config.SSHConfig{ User: user, @@ -34,13 +35,8 @@ func getDefaultConfig(networkInfo network.Network) *config.Config { }, Interface: networkInfo.Interface().Name, } -} - -// CreateNewAppCore creates and returns a new instance of *core.Core -func CreateNewAppCore(networkInfo network.Network, eventManager event.Manager, debug bool) (*Core, error) { - configPath := viper.Get("config-path").(string) - configRepo, err := config.NewJSONRepo(configPath) + configRepo, err := config.NewJSONRepo(configPath, defaultConf) if err != nil { return nil, err @@ -52,8 +48,7 @@ func CreateNewAppCore(networkInfo network.Network, eventManager event.Manager, d if err != nil { if errors.Is(err, exception.ErrRecordNotFound) { - conf = getDefaultConfig(networkInfo) - conf, err = configService.Create(conf) + conf, err = configService.Create(&defaultConf) if err != nil { return nil, err }