Skip to content

Commit

Permalink
add domain record create command
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanl committed Aug 23, 2015
1 parent 2c1c2b8 commit a1ab1db
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 64 deletions.
8 changes: 4 additions & 4 deletions commands/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewCmdActionList(out io.Writer) *cobra.Command {
Short: "action list",
Long: "list actions",
Run: func(cmd *cobra.Command, args []string) {
checkErr(RunActionList(out))
checkErr(RunActionList(out), cmd)
},
}
}
Expand Down Expand Up @@ -77,15 +77,15 @@ func NewCmdActionGet(out io.Writer) *cobra.Command {
Short: "action get",
Long: "get action",
Run: func(cmd *cobra.Command, args []string) {
checkErr(RunActionGet(out))
checkErr(RunActionGet(cmdNS(cmd), out), cmd)
},
}
}

// RunActionGet runs action get.
func RunActionGet(out io.Writer) error {
func RunActionGet(ns string, out io.Writer) error {
client := doit.VConfig.GetGodoClient()
id := doit.VConfig.GetInt(doit.ArgActionID)
id := doit.VConfig.GetInt(ns, doit.ArgActionID)
if id < 1 {
return errors.New("invalid action id")
}
Expand Down
7 changes: 4 additions & 3 deletions commands/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/bryanl/doit"
"github.com/digitalocean/godo"
"github.com/stretchr/testify/assert"
)

var (
Expand Down Expand Up @@ -55,9 +56,9 @@ func TestActionGet(t *testing.T) {
}

withTestClient(client, func(c doit.ViperConfig) {
c.Set("action-id", testAction.ID)
c.Set("test", "action-id", testAction.ID)

cmd := NewCmdActionGet(ioutil.Discard)
cmd.Run(cmd, []string{})
err := RunActionGet("test", ioutil.Discard)
assert.NoError(t, err)
})
}
32 changes: 28 additions & 4 deletions commands/doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/user"
"path/filepath"

"github.com/bryanl/doit"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -101,12 +102,35 @@ func configFilePath() (string, error) {
return dir, nil
}

func addStringFlag(cmd *cobra.Command, name, def, desc string) {
cmd.Flags().String(name, def, desc)
viper.BindPFlag(name, cmd.Flags().Lookup(name))
func addStringFlag(cmd *cobra.Command, name, dflt, desc string) {
parentName := doit.NSRoot
if cmd.Parent() != nil {
parentName = cmd.Parent().Name()
}

flagName := fmt.Sprintf("%s-%s-%s", parentName, cmd.Name(), name)

cmd.Flags().String(name, dflt, desc)
viper.BindPFlag(flagName, cmd.Flags().Lookup(name))
}

func addIntFlag(cmd *cobra.Command, name string, def int, desc string) {
parentName := doit.NSRoot
if cmd.Parent() != nil {
parentName = cmd.Parent().Name()
}

flagName := fmt.Sprintf("%s-%s-%s", parentName, cmd.Name(), name)

cmd.Flags().Int(name, def, desc)
viper.BindPFlag(name, cmd.Flags().Lookup(name))
viper.BindPFlag(flagName, cmd.Flags().Lookup(name))
}

func cmdNS(cmd *cobra.Command) string {
parentName := doit.NSRoot
if cmd.Parent() != nil {
parentName = cmd.Parent().Name()
}

return fmt.Sprintf("%s-%s", parentName, cmd.Name())
}
92 changes: 70 additions & 22 deletions commands/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,41 @@ func Domain() *cobra.Command {
}

cmdDomainCreate := NewCmdDomainCreate(os.Stdout)
cmd.AddCommand(cmdDomainCreate)
addStringFlag(cmdDomainCreate, doit.ArgDomainName, "", "Domain name")
addStringFlag(cmdDomainCreate, doit.ArgIPAddress, "", "IP address")
cmd.AddCommand(cmdDomainCreate)

cmdDomainList := NewCmdDomainList(os.Stdout)
cmd.AddCommand(cmdDomainList)

cmdDomainGet := NewCmdDomainGet(os.Stdout)
addStringFlag(cmdDomainGet, doit.ArgDomainName, "", "Domain name")
cmd.AddCommand(cmdDomainGet)
addStringFlag(cmdDomainGet, doit.ArgDomainName, "", "Domain name")

cmdDomainDelete := NewCmdDomainDelete(os.Stdout)
addStringFlag(cmdDomainDelete, doit.ArgDomainName, "", "Domain name")
cmd.AddCommand(cmdDomainDelete)
addStringFlag(cmdDomainDelete, doit.ArgDomainName, "", "Domain name")

recordCmd := &cobra.Command{
cmdRecord := &cobra.Command{
Use: "records",
Short: "domain record commands",
Long: "commands for interacting with an individual domain",
}
cmd.AddCommand(recordCmd)
cmd.AddCommand(cmdRecord)

cmdRecordList := NewCmdRecordList(os.Stdout)
cmdRecord.AddCommand(cmdRecordList)
addStringFlag(cmdRecordList, doit.ArgDomainName, "", "Domain name")
recordCmd.AddCommand(cmdRecordList)

cmdRecordCreate := NewCmdRecordCreate(os.Stdout)
cmdRecord.AddCommand(cmdRecordCreate)
addStringFlag(cmdRecordCreate, doit.ArgDomainName, "", "Domain name")
addStringFlag(cmdRecordCreate, doit.ArgRecordType, "", "Record type")
addStringFlag(cmdRecordCreate, doit.ArgRecordName, "", "Record name")
addStringFlag(cmdRecordCreate, doit.ArgRecordData, "", "Record data")
addIntFlag(cmdRecordCreate, doit.ArgRecordPriority, 0, "Record priority")
addIntFlag(cmdRecordCreate, doit.ArgRecordPort, 0, "Record port")
addIntFlag(cmdRecordCreate, doit.ArgRecordWeight, 0, "Record weight")

return cmd
}
Expand All @@ -55,17 +65,17 @@ func NewCmdDomainCreate(out io.Writer) *cobra.Command {
Short: "create domain",
Long: "create a domain",
Run: func(cmd *cobra.Command, args []string) {
checkErr(RunDomainCreate(out))
checkErr(RunDomainCreate(cmdNS(cmd), out), cmd)
},
}
}

// RunDomainCreate runs domain create.
func RunDomainCreate(out io.Writer) error {
func RunDomainCreate(ns string, out io.Writer) error {
client := doit.VConfig.GetGodoClient()
req := &godo.DomainCreateRequest{
Name: doit.VConfig.GetString("domain-name"),
IPAddress: doit.VConfig.GetString("ip-address"),
Name: doit.VConfig.GetString(ns, "domain-name"),
IPAddress: doit.VConfig.GetString(ns, "ip-address"),
}

d, _, err := client.Domains.Create(req)
Expand All @@ -83,13 +93,13 @@ func NewCmdDomainList(out io.Writer) *cobra.Command {
Short: "list domains",
Long: "list all domains",
Run: func(cmd *cobra.Command, args []string) {
checkErr(RunDomainList(out))
checkErr(RunDomainList(cmdNS(cmd), out), cmd)
},
}
}

// RunDomainList runs domain create.
func RunDomainList(out io.Writer) error {
func RunDomainList(cmdName string, out io.Writer) error {
client := doit.VConfig.GetGodoClient()

f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
Expand Down Expand Up @@ -126,15 +136,15 @@ func NewCmdDomainGet(out io.Writer) *cobra.Command {
Short: "get domain",
Long: "retrieve an individual domain",
Run: func(cmd *cobra.Command, args []string) {
checkErr(RunDomainGet(out))
checkErr(RunDomainGet(cmdNS(cmd), out), cmd)
},
}
}

// RunDomainGet retrieves a domain by name.
func RunDomainGet(out io.Writer) error {
func RunDomainGet(ns string, out io.Writer) error {
client := doit.VConfig.GetGodoClient()
id := doit.VConfig.GetString(doit.ArgDomainName)
id := doit.VConfig.GetString(ns, doit.ArgDomainName)

if len(id) < 1 {
return errors.New("invalid domain name")
Expand All @@ -155,15 +165,15 @@ func NewCmdDomainDelete(out io.Writer) *cobra.Command {
Short: "delete domain",
Long: "delete a domain an all associated records",
Run: func(cmd *cobra.Command, args []string) {
checkErr(RunDomainDelete(out))
checkErr(RunDomainDelete(cmdNS(cmd), out), cmd)
},
}
}

// RunDomainDelete deletes a domain by name.
func RunDomainDelete(out io.Writer) error {
func RunDomainDelete(ns string, out io.Writer) error {
client := doit.VConfig.GetGodoClient()
name := doit.VConfig.GetString(doit.ArgDomainName)
name := doit.VConfig.GetString(ns, doit.ArgDomainName)

if len(name) < 1 {
return errors.New("invalid domain name")
Expand All @@ -180,18 +190,18 @@ func NewCmdRecordList(out io.Writer) *cobra.Command {
Short: "list records",
Long: "list all records in a domain",
Run: func(cmd *cobra.Command, args []string) {
checkErr(RunRecordList(out))
checkErr(RunRecordList(cmdNS(cmd), out), cmd)
},
}
}

// RunRecordList list records for a domain.
func RunRecordList(out io.Writer) error {
func RunRecordList(ns string, out io.Writer) error {
client := doit.VConfig.GetGodoClient()
name := doit.VConfig.GetString(doit.ArgDomainName)
name := doit.VConfig.GetString(ns, doit.ArgDomainName)

if len(name) < 1 {
return errors.New("invalid domain name")
return errors.New("domain name is missing")
}

f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
Expand Down Expand Up @@ -220,3 +230,41 @@ func RunRecordList(out io.Writer) error {

return doit.DisplayOutput(list, out)
}

// NewCmdRecordCreate creates a record create command.
func NewCmdRecordCreate(out io.Writer) *cobra.Command {
return &cobra.Command{
Use: "create",
Short: "create record",
Long: "create record for a domain",
Run: func(cmd *cobra.Command, args []string) {
checkErr(RunRecordCreate(cmdNS(cmd), out), cmd)
},
}
}

// RunRecordCreate creates a domain record.
func RunRecordCreate(ns string, out io.Writer) error {
client := doit.VConfig.GetGodoClient()
name := doit.VConfig.GetString(ns, doit.ArgDomainName)

drcr := &godo.DomainRecordEditRequest{
Type: doit.VConfig.GetString(ns, doit.ArgRecordType),
Name: doit.VConfig.GetString(ns, doit.ArgRecordName),
Data: doit.VConfig.GetString(ns, doit.ArgRecordData),
Priority: doit.VConfig.GetInt(ns, doit.ArgRecordPriority),
Port: doit.VConfig.GetInt(ns, doit.ArgRecordPort),
Weight: doit.VConfig.GetInt(ns, doit.ArgRecordWeight),
}

if len(drcr.Type) == 0 {
return errors.New("record request is missing type")
}

r, _, err := client.Domains.CreateRecord(name, drcr)
if err != nil {
return err
}

return doit.DisplayOutput(r, out)
}
Loading

0 comments on commit a1ab1db

Please sign in to comment.