Skip to content

Commit f101d65

Browse files
committed
Fix issue with generating config on first run
1 parent d379850 commit f101d65

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

internal/config/repo-json.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ import (
1515

1616
// JSONRepo is our repo implementation for json
1717
type JSONRepo struct {
18-
configPath string
19-
configs []*Config
20-
mux sync.Mutex
18+
configPath string
19+
configs []*Config
20+
defaultConfig Config
21+
mux sync.Mutex
2122
}
2223

2324
// NewJSONRepo returns a new ops repo for flat yaml file
24-
func NewJSONRepo(configPath string) (*JSONRepo, error) {
25+
func NewJSONRepo(configPath string, defaultConfig Config) (*JSONRepo, error) {
2526
repo := &JSONRepo{
26-
configPath: configPath,
27-
configs: []*Config{},
28-
mux: sync.Mutex{},
27+
configPath: configPath,
28+
configs: []*Config{},
29+
defaultConfig: defaultConfig,
30+
mux: sync.Mutex{},
2931
}
3032

3133
if err := repo.load(); err != nil {
@@ -192,6 +194,12 @@ func (r *JSONRepo) write() error {
192194
}
193195

194196
func (r *JSONRepo) load() error {
197+
if _, err := os.Stat(r.configPath); errors.Is(err, os.ErrNotExist) {
198+
if err := r.createDefaultConfig(); err != nil {
199+
return err
200+
}
201+
}
202+
195203
file, err := os.Open(r.configPath)
196204

197205
if err != nil {
@@ -217,6 +225,30 @@ func (r *JSONRepo) load() error {
217225
return nil
218226
}
219227

228+
func (r *JSONRepo) createDefaultConfig() error {
229+
configs := Configs{
230+
Configs: []*Config{&r.defaultConfig},
231+
}
232+
233+
file, err := os.OpenFile(r.configPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
234+
235+
if err != nil {
236+
return err
237+
}
238+
239+
defer file.Close()
240+
241+
data, err := json.MarshalIndent(&configs, "", "\t")
242+
243+
if err != nil {
244+
return err
245+
}
246+
247+
_, err = file.Write(data)
248+
249+
return err
250+
}
251+
220252
// helpers
221253
func copyConfig(c *Config) *Config {
222254
return &Config{

internal/config/repo_test.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func assertEqualConf(t *testing.T, expected, actual *config.Config) {
2222
}
2323
}
2424

25-
func TestConfigYamlRepo(t *testing.T) {
25+
func TestConfigJsonRepo(t *testing.T) {
2626
testConfigFile := "config.json"
2727
file, err := os.Create(testConfigFile)
2828

@@ -32,6 +32,17 @@ func TestConfigYamlRepo(t *testing.T) {
3232
os.RemoveAll(testConfigFile)
3333
}()
3434

35+
defaultConf := config.Config{
36+
ID: "0",
37+
Name: "default",
38+
SSH: config.SSHConfig{
39+
User: "user",
40+
Identity: "./id_rsa",
41+
Port: "22",
42+
Overrides: []config.SSHOverride{},
43+
},
44+
}
45+
3546
conf := config.Config{
3647
ID: "1",
3748
Name: "myConfig",
@@ -52,18 +63,10 @@ func TestConfigYamlRepo(t *testing.T) {
5263

5364
assert.NoError(t, err)
5465

55-
repo, err := config.NewJSONRepo(testConfigFile)
66+
repo, err := config.NewJSONRepo(testConfigFile, defaultConf)
5667

5768
assert.NoError(t, err)
5869

59-
t.Run("returns error if cannot instantiate instance", func(st *testing.T) {
60-
repo, err := config.NewJSONRepo("nope.json")
61-
62-
assert.Nil(st, repo)
63-
assert.Error(t, err)
64-
65-
})
66-
6770
t.Run("returns record not found error", func(st *testing.T) {
6871
_, err := repo.Get("10")
6972

internal/core/create.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ import (
1717
"github.com/spf13/viper"
1818
)
1919

20-
// getDefaultConfig creates and returns a default configuration
21-
func getDefaultConfig(networkInfo network.Network) *config.Config {
20+
// CreateNewAppCore creates and returns a new instance of *core.Core
21+
func CreateNewAppCore(networkInfo network.Network, eventManager event.Manager, debug bool) (*Core, error) {
22+
configPath := viper.Get("config-path").(string)
2223
user := viper.Get("user").(string)
2324
identity := viper.Get("default-ssh-identity").(string)
2425
seed := time.Now().UTC().UnixNano()
2526
nameGenerator := namegenerator.NewNameGenerator(seed)
2627

27-
return &config.Config{
28+
defaultConf := config.Config{
2829
Name: nameGenerator.Generate(),
2930
SSH: config.SSHConfig{
3031
User: user,
@@ -34,13 +35,8 @@ func getDefaultConfig(networkInfo network.Network) *config.Config {
3435
},
3536
Interface: networkInfo.Interface().Name,
3637
}
37-
}
38-
39-
// CreateNewAppCore creates and returns a new instance of *core.Core
40-
func CreateNewAppCore(networkInfo network.Network, eventManager event.Manager, debug bool) (*Core, error) {
41-
configPath := viper.Get("config-path").(string)
4238

43-
configRepo, err := config.NewJSONRepo(configPath)
39+
configRepo, err := config.NewJSONRepo(configPath, defaultConf)
4440

4541
if err != nil {
4642
return nil, err
@@ -52,8 +48,7 @@ func CreateNewAppCore(networkInfo network.Network, eventManager event.Manager, d
5248

5349
if err != nil {
5450
if errors.Is(err, exception.ErrRecordNotFound) {
55-
conf = getDefaultConfig(networkInfo)
56-
conf, err = configService.Create(conf)
51+
conf, err = configService.Create(&defaultConf)
5752
if err != nil {
5853
return nil, err
5954
}

0 commit comments

Comments
 (0)