diff --git a/cmd/deploy.go b/cmd/deploy.go index c54e336..022d50a 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -2,13 +2,11 @@ package cmd import ( "bacon/pkg/collections" + "bacon/pkg/config" "bacon/pkg/dns" "fmt" - "io" - "os" "github.com/spf13/cobra" - "gopkg.in/yaml.v3" ) func newDeployCmd(app *App) *cobra.Command { @@ -33,7 +31,7 @@ creating new records.`, } func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) error { - config, err := readConfig(configFile) + config, err := config.ReadFile(configFile) if err != nil { return fmt.Errorf("reading config: %v", err) } @@ -90,28 +88,3 @@ func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) e } return nil } - -func readConfig(configFile string) (*dns.Config, error) { - file, err := os.Open(configFile) - if err != nil { - return nil, err - } - - raw, err := io.ReadAll(file) - if err != nil { - return nil, err - } - - err = file.Close() - if err != nil { - return nil, err - } - - config := dns.Config{} - err = yaml.Unmarshal(raw, &config) - if err != nil { - return nil, err - } - - return &config, nil -} diff --git a/cmd/print.go b/cmd/print.go index ee7ffdb..b0675e3 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -1,8 +1,9 @@ package cmd import ( - "bacon/pkg/dns" + "bacon/pkg/config" "fmt" + "strconv" "github.com/spf13/cobra" "gopkg.in/yaml.v3" @@ -26,12 +27,20 @@ func print(app *App, domain string) error { return err } - configRecords := make([]dns.ConfigRecord, len(records)) + configRecords := make([]config.Record, len(records)) for i, record := range records { - configRecords[i] = dns.ConfigFromRecord(record) + // Sets ttl to nil if Atoi fails + ttl, _ := strconv.Atoi(record.GetTtl()) + + configRecords[i] = config.Record{ + Name: record.GetName(), + Type: record.GetType(), + Ttl: &ttl, + Data: record.GetData(), + } } - config := dns.Config{ + config := config.Config{ Domain: domain, Records: configRecords, } diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..ea505c1 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,38 @@ +package config + +import ( + "io" + "os" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Domain string `yaml:"domain"` + Records []Record `yaml:"records"` +} + +func ReadFile(configFile string) (*Config, error) { + file, err := os.Open(configFile) + if err != nil { + return nil, err + } + + raw, err := io.ReadAll(file) + if err != nil { + return nil, err + } + + err = file.Close() + if err != nil { + return nil, err + } + + config := Config{} + err = yaml.Unmarshal(raw, &config) + if err != nil { + return nil, err + } + + return &config, nil +} diff --git a/pkg/config/record.go b/pkg/config/record.go new file mode 100644 index 0000000..781d1f2 --- /dev/null +++ b/pkg/config/record.go @@ -0,0 +1,35 @@ +package config + +import ( + "bacon/pkg/dns" + "fmt" +) + +type Record struct { + Name string `yaml:"host"` + Type string `yaml:"type"` + Ttl *int `yaml:"ttl"` + Data string `yaml:"content"` +} + +func (r Record) GetName() string { + return r.Name +} + +func (r Record) GetType() string { + return r.Type +} + +func (r Record) GetTtl() string { + if r.Ttl == nil { + return "" + } + + return fmt.Sprint(*r.Ttl) +} + +func (r Record) GetData() string { + return r.Data +} + +var _ dns.Record = Record{} diff --git a/pkg/dns/config.go b/pkg/dns/config.go deleted file mode 100644 index 1df35a6..0000000 --- a/pkg/dns/config.go +++ /dev/null @@ -1,51 +0,0 @@ -package dns - -import ( - "fmt" - "strconv" -) - -type Config struct { - Domain string `yaml:"domain"` - Records []ConfigRecord `yaml:"records"` -} - -type ConfigRecord struct { - Name string `yaml:"host"` - Type string `yaml:"type"` - Ttl *int `yaml:"ttl"` - Data string `yaml:"content"` -} - -func (r ConfigRecord) GetName() string { - return r.Name -} - -func (r ConfigRecord) GetType() string { - return r.Type -} - -func (r ConfigRecord) GetTtl() string { - if r.Ttl == nil { - return "" - } - - return fmt.Sprint(*r.Ttl) -} - -func (r ConfigRecord) GetData() string { - return r.Data -} - -var _ Record = ConfigRecord{} - -func ConfigFromRecord(r Record) ConfigRecord { - ttl, _ := strconv.Atoi(r.GetTtl()) - - return ConfigRecord{ - Name: r.GetName(), - Type: r.GetType(), - Ttl: &ttl, - Data: r.GetData(), - } -}