Skip to content

Commit f726603

Browse files
authored
Merge pull request #37 from jungaretti/jpungaretti/new-config-package
Move config code to new config package
2 parents ea9415d + 35e874c commit f726603

File tree

5 files changed

+88
-84
lines changed

5 files changed

+88
-84
lines changed

cmd/deploy.go

+2-29
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package cmd
22

33
import (
44
"bacon/pkg/collections"
5+
"bacon/pkg/config"
56
"bacon/pkg/dns"
67
"fmt"
7-
"io"
8-
"os"
98

109
"github.com/spf13/cobra"
11-
"gopkg.in/yaml.v3"
1210
)
1311

1412
func newDeployCmd(app *App) *cobra.Command {
@@ -33,7 +31,7 @@ creating new records.`,
3331
}
3432

3533
func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) error {
36-
config, err := readConfig(configFile)
34+
config, err := config.ReadFile(configFile)
3735
if err != nil {
3836
return fmt.Errorf("reading config: %v", err)
3937
}
@@ -90,28 +88,3 @@ func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) e
9088
}
9189
return nil
9290
}
93-
94-
func readConfig(configFile string) (*dns.Config, error) {
95-
file, err := os.Open(configFile)
96-
if err != nil {
97-
return nil, err
98-
}
99-
100-
raw, err := io.ReadAll(file)
101-
if err != nil {
102-
return nil, err
103-
}
104-
105-
err = file.Close()
106-
if err != nil {
107-
return nil, err
108-
}
109-
110-
config := dns.Config{}
111-
err = yaml.Unmarshal(raw, &config)
112-
if err != nil {
113-
return nil, err
114-
}
115-
116-
return &config, nil
117-
}

cmd/print.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package cmd
22

33
import (
4-
"bacon/pkg/dns"
4+
"bacon/pkg/config"
55
"fmt"
6+
"strconv"
67

78
"github.com/spf13/cobra"
89
"gopkg.in/yaml.v3"
@@ -26,12 +27,20 @@ func print(app *App, domain string) error {
2627
return err
2728
}
2829

29-
configRecords := make([]dns.ConfigRecord, len(records))
30+
configRecords := make([]config.Record, len(records))
3031
for i, record := range records {
31-
configRecords[i] = dns.ConfigFromRecord(record)
32+
// Sets ttl to nil if Atoi fails
33+
ttl, _ := strconv.Atoi(record.GetTtl())
34+
35+
configRecords[i] = config.Record{
36+
Name: record.GetName(),
37+
Type: record.GetType(),
38+
Ttl: &ttl,
39+
Data: record.GetData(),
40+
}
3241
}
3342

34-
config := dns.Config{
43+
config := config.Config{
3544
Domain: domain,
3645
Records: configRecords,
3746
}

pkg/config/config.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package config
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"gopkg.in/yaml.v3"
8+
)
9+
10+
type Config struct {
11+
Domain string `yaml:"domain"`
12+
Records []Record `yaml:"records"`
13+
}
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+
}

pkg/config/record.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package config
2+
3+
import (
4+
"bacon/pkg/dns"
5+
"fmt"
6+
)
7+
8+
type Record struct {
9+
Name string `yaml:"host"`
10+
Type string `yaml:"type"`
11+
Ttl *int `yaml:"ttl"`
12+
Data string `yaml:"content"`
13+
}
14+
15+
func (r Record) GetName() string {
16+
return r.Name
17+
}
18+
19+
func (r Record) GetType() string {
20+
return r.Type
21+
}
22+
23+
func (r Record) GetTtl() string {
24+
if r.Ttl == nil {
25+
return ""
26+
}
27+
28+
return fmt.Sprint(*r.Ttl)
29+
}
30+
31+
func (r Record) GetData() string {
32+
return r.Data
33+
}
34+
35+
var _ dns.Record = Record{}

pkg/dns/config.go

-51
This file was deleted.

0 commit comments

Comments
 (0)