Skip to content

Commit 35e874c

Browse files
committed
Move config reading helper to config
1 parent aeee15e commit 35e874c

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

cmd/deploy.go

+1-29
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ import (
55
"bacon/pkg/config"
66
"bacon/pkg/dns"
77
"fmt"
8-
"io"
9-
"os"
108

119
"github.com/spf13/cobra"
12-
"gopkg.in/yaml.v3"
1310
)
1411

1512
func newDeployCmd(app *App) *cobra.Command {
@@ -34,7 +31,7 @@ creating new records.`,
3431
}
3532

3633
func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) error {
37-
config, err := readConfig(configFile)
34+
config, err := config.ReadFile(configFile)
3835
if err != nil {
3936
return fmt.Errorf("reading config: %v", err)
4037
}
@@ -91,28 +88,3 @@ func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) e
9188
}
9289
return nil
9390
}
94-
95-
func readConfig(configFile string) (*config.Config, error) {
96-
file, err := os.Open(configFile)
97-
if err != nil {
98-
return nil, err
99-
}
100-
101-
raw, err := io.ReadAll(file)
102-
if err != nil {
103-
return nil, err
104-
}
105-
106-
err = file.Close()
107-
if err != nil {
108-
return nil, err
109-
}
110-
111-
config := config.Config{}
112-
err = yaml.Unmarshal(raw, &config)
113-
if err != nil {
114-
return nil, err
115-
}
116-
117-
return &config, nil
118-
}

pkg/config/config.go

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
package config
22

3+
import (
4+
"io"
5+
"os"
6+
7+
"gopkg.in/yaml.v3"
8+
)
9+
310
type Config struct {
411
Domain string `yaml:"domain"`
512
Records []Record `yaml:"records"`
613
}
14+
15+
func ReadFile(configFile string) (*Config, error) {
16+
file, err := os.Open(configFile)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
raw, err := io.ReadAll(file)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
err = file.Close()
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
config := Config{}
32+
err = yaml.Unmarshal(raw, &config)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return &config, nil
38+
}

0 commit comments

Comments
 (0)