Skip to content

Commit

Permalink
Fix issue with generating config on first run
Browse files Browse the repository at this point in the history
  • Loading branch information
robgonnella committed Mar 20, 2024
1 parent d379850 commit 6b75b08
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
44 changes: 37 additions & 7 deletions internal/config/repo-json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Check failure on line 198 in internal/config/repo-json.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `r.createDefaultConfig` is not checked (errcheck)
}

file, err := os.Open(r.configPath)

if err != nil {
Expand All @@ -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{
Expand Down
23 changes: 13 additions & 10 deletions internal/config/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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",
Expand All @@ -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")

Expand Down
17 changes: 6 additions & 11 deletions internal/core/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 6b75b08

Please sign in to comment.