Skip to content

Commit

Permalink
Merge pull request #37 from jungaretti/jpungaretti/new-config-package
Browse files Browse the repository at this point in the history
Move config code to new config package
  • Loading branch information
jungaretti authored Apr 27, 2024
2 parents ea9415d + 35e874c commit f726603
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 84 deletions.
31 changes: 2 additions & 29 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
17 changes: 13 additions & 4 deletions cmd/print.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cmd

import (
"bacon/pkg/dns"
"bacon/pkg/config"
"fmt"
"strconv"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
Expand All @@ -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,
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions pkg/config/record.go
Original file line number Diff line number Diff line change
@@ -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{}
51 changes: 0 additions & 51 deletions pkg/dns/config.go

This file was deleted.

0 comments on commit f726603

Please # to comment.