Skip to content

Commit

Permalink
Merge pull request #224 from xmudrii/delete-consistency
Browse files Browse the repository at this point in the history
consistency changes: ask for confirm added to all delete actions
  • Loading branch information
mauricio authored May 17, 2017
2 parents b994643 + 1981ec1 commit 92fd1a6
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 58 deletions.
4 changes: 2 additions & 2 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,6 @@ const (
// ArgForwardingRules is a list of forwarding rules for the load balancer.
ArgForwardingRules = "forwarding-rules"

// ArgDeleteForce forces deletion actions
ArgDeleteForce = "force"
// ArgForce forces confirmation on actions
ArgForce = "force"
)
4 changes: 2 additions & 2 deletions args_short.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ limitations under the License.
package doctl

const (
// ArgShortDeleteForce forces deletion actions
ArgShortDeleteForce = "f"
// ArgShortForce forces confirmation on actions
ArgShortForce = "f"
)
4 changes: 2 additions & 2 deletions commands/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Certificate() *Command {
CmdBuilder(cmd, RunCertificateList, "list", "list certificates", Writer, aliasOpt("ls"))

cmdCertificateDelete := CmdBuilder(cmd, RunCertificateDelete, "delete <id>", "delete certificate", Writer, aliasOpt("d", "rm"))
AddBoolFlag(cmdCertificateDelete, doctl.ArgDeleteForce, doctl.ArgShortDeleteForce, false, "Force certificate delete")
AddBoolFlag(cmdCertificateDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force certificate delete")

return cmd
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func RunCertificateDelete(c *CmdConfig) error {
}
cID := c.Args[0]

force, err := c.Doit.GetBool(c.NS, doctl.ArgDeleteForce)
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestCertificateDelete(t *testing.T) {
tm.certificates.On("Delete", cID).Return(nil)

config.Args = append(config.Args, cID)
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunCertificateDelete(config)
assert.NoError(t, err)
Expand Down
61 changes: 42 additions & 19 deletions commands/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func Domain() *Command {
CmdBuilder(cmd, RunDomainGet, "get <domain>", "get domain", Writer,
aliasOpt("g"), displayerType(&domain{}), docCategories("domain"))

CmdBuilder(cmd, RunDomainDelete, "delete <domain>", "delete domain", Writer, aliasOpt("g"))
cmdRunDomainDelete := CmdBuilder(cmd, RunDomainDelete, "delete <domain>", "delete domain", Writer, aliasOpt("g"))
AddBoolFlag(cmdRunDomainDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force domain delete")

cmdRecord := &Command{
Command: &cobra.Command{
Expand All @@ -71,8 +72,9 @@ func Domain() *Command {
AddIntFlag(cmdRecordCreate, doctl.ArgRecordTTL, "", 1800, "Record TTL")
AddIntFlag(cmdRecordCreate, doctl.ArgRecordWeight, "", 0, "Record weight")

CmdBuilder(cmdRecord, RunRecordDelete, "delete <domain> <record id...>", "delete record", Writer,
cmdRunRecordDelete := CmdBuilder(cmdRecord, RunRecordDelete, "delete <domain> <record id...>", "delete record", Writer,
aliasOpt("d"), docCategories("domain"))
AddBoolFlag(cmdRunRecordDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force record delete")

cmdRecordUpdate := CmdBuilder(cmdRecord, RunRecordUpdate, "update <domain>", "update record", Writer,
aliasOpt("u"), displayerType(&domainRecord{}), docCategories("domain"))
Expand Down Expand Up @@ -158,14 +160,25 @@ func RunDomainDelete(c *CmdConfig) error {
}
name := c.Args[0]

ds := c.Domains()
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}

if len(name) < 1 {
return errors.New("invalid domain name")
if force || AskForConfirm("delete domain") == nil {
ds := c.Domains()

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

err := ds.Delete(name)
return err
} else {
return fmt.Errorf("operation aborted")
}

err := ds.Delete(name)
return err
return nil
}

// RunRecordList list records for a domain.
Expand Down Expand Up @@ -265,26 +278,36 @@ func RunRecordDelete(c *CmdConfig) error {
return doctl.NewMissingArgsErr(c.NS)
}

domainName, ids := c.Args[0], c.Args[1:]
if len(ids) < 1 {
return doctl.NewMissingArgsErr(c.NS)
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}

ds := c.Domains()

for _, i := range ids {
id, err := strconv.Atoi(i)
if err != nil {
return fmt.Errorf("invalid record id %q", i)
if force || AskForConfirm("delete record(s)") == nil {
domainName, ids := c.Args[0], c.Args[1:]
if len(ids) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}

err = ds.DeleteRecord(domainName, id)
if err != nil {
return err
ds := c.Domains()

for _, i := range ids {
id, err := strconv.Atoi(i)
if err != nil {
return fmt.Errorf("invalid record id %q", i)
}

err = ds.DeleteRecord(domainName, id)
if err != nil {
return err
}
}
} else {
return fmt.Errorf("opertaion aborted")
}

return nil

}

// RunRecordUpdate updates a domain record.
Expand Down
4 changes: 4 additions & 0 deletions commands/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func TestDomainsDelete(t *testing.T) {

config.Args = append(config.Args, testDomain.Name)

config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunDomainDelete(config)
assert.NoError(t, err)
})
Expand Down Expand Up @@ -140,6 +142,8 @@ func TestRecordsDelete(t *testing.T) {

config.Args = append(config.Args, "example.com", "1")

config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunRecordDelete(config)
assert.NoError(t, err)
})
Expand Down
4 changes: 2 additions & 2 deletions commands/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Droplet() *Command {

cmdRunDropletDelete := CmdBuilder(cmd, RunDropletDelete, "delete <droplet-id|droplet-name> [droplet-id|droplet-name ...]", "Delete droplet by id or name", Writer,
aliasOpt("d", "del", "rm"), docCategories("droplet"))
AddBoolFlag(cmdRunDropletDelete, doctl.ArgDeleteForce, doctl.ArgShortDeleteForce, false, "Force droplet delete")
AddBoolFlag(cmdRunDropletDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force droplet delete")

cmdRunDropletGet := CmdBuilder(cmd, RunDropletGet, "get <droplet-id>", "get droplet", Writer,
aliasOpt("g"), displayerType(&droplet{}), docCategories("droplet"))
Expand Down Expand Up @@ -452,7 +452,7 @@ func allInt(in []string) ([]int, error) {
func RunDropletDelete(c *CmdConfig) error {
ds := c.Droplets()

force, err := c.Doit.GetBool(c.NS, doctl.ArgDeleteForce)
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions commands/droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestDropletDelete(t *testing.T) {
tm.droplets.On("Delete", 1).Return(nil)

config.Args = append(config.Args, strconv.Itoa(testDroplet.ID))
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunDropletDelete(config)
assert.NoError(t, err)
Expand All @@ -163,7 +163,7 @@ func TestDropletDeleteByTag(t *testing.T) {
tm.droplets.On("DeleteByTag", "my-tag").Return(nil)

config.Doit.Set(config.NS, doctl.ArgTagName, "my-tag")
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunDropletDelete(config)
assert.NoError(t, err)
Expand All @@ -177,7 +177,7 @@ func TestDropletDeleteRepeatedID(t *testing.T) {

id := strconv.Itoa(testDroplet.ID)
config.Args = append(config.Args, id, id)
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunDropletDelete(config)
assert.NoError(t, err)
Expand All @@ -190,7 +190,7 @@ func TestDropletDeleteByName(t *testing.T) {
tm.droplets.On("Delete", 1).Return(nil)

config.Args = append(config.Args, testDroplet.Name)
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunDropletDelete(config)
assert.NoError(t, err)
Expand All @@ -203,7 +203,7 @@ func TestDropletDeleteByName_Ambiguous(t *testing.T) {
tm.droplets.On("List").Return(list, nil)

config.Args = append(config.Args, testDroplet.Name)
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunDropletDelete(config)
t.Log(err)
Expand All @@ -218,7 +218,7 @@ func TestDropletDelete_MixedNameAndType(t *testing.T) {

id := strconv.Itoa(testDroplet.ID)
config.Args = append(config.Args, id, testDroplet.Name)
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunDropletDelete(config)
assert.NoError(t, err)
Expand Down
16 changes: 13 additions & 3 deletions commands/floating_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func FloatingIP() *Command {
CmdBuilder(cmd, RunFloatingIPGet, "get <floating-ip>", "get the details of a floating IP", Writer,
aliasOpt("g"), displayerType(&floatingIP{}), docCategories("floatingip"))

CmdBuilder(cmd, RunFloatingIPDelete, "delete <floating-ip>", "delete a floating IP address", Writer, aliasOpt("d"))
cmdRunFloatingIPDelete := CmdBuilder(cmd, RunFloatingIPDelete, "delete <floating-ip>", "delete a floating IP address", Writer, aliasOpt("d"))
AddBoolFlag(cmdRunFloatingIPDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force floating IP delete")

cmdFloatingIPList := CmdBuilder(cmd, RunFloatingIPList, "list", "list all floating IP addresses", Writer,
aliasOpt("ls"), displayerType(&floatingIP{}), docCategories("floatingip"))
Expand Down Expand Up @@ -119,9 +120,18 @@ func RunFloatingIPDelete(c *CmdConfig) error {
return doctl.NewMissingArgsErr(c.NS)
}

ip := c.Args[0]
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}

return fis.Delete(ip)
if force || AskForConfirm("delete floating IP") == nil {
ip := c.Args[0]
return fis.Delete(ip)
} else {
return fmt.Errorf("operation aborted")
}
return nil
}

// RunFloatingIPList runs floating IP create.
Expand Down
2 changes: 2 additions & 0 deletions commands/floating_ips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func TestFloatingIPsDelete(t *testing.T) {

config.Args = append(config.Args, "127.0.0.1")

config.Doit.Set(config.NS, doctl.ArgForce, true)

RunFloatingIPDelete(config)
})
}
4 changes: 2 additions & 2 deletions commands/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Images() *Command {

cmdRunImagesDelete := CmdBuilder(cmd, RunImagesDelete, "delete <image-id>", "Delete image", Writer,
docCategories("image"))
AddBoolFlag(cmdRunImagesDelete, doctl.ArgDeleteForce, doctl.ArgShortDeleteForce, false, "Force image delete")
AddBoolFlag(cmdRunImagesDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force image delete")

return cmd
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func RunImagesDelete(c *CmdConfig) error {
return doctl.NewMissingArgsErr(c.NS)
}

force, err := c.Doit.GetBool(c.NS, doctl.ArgDeleteForce)
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions commands/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestImagesDelete(t *testing.T) {
tm.images.On("Delete", testImage.ID).Return(nil)

config.Args = append(config.Args, strconv.Itoa(testImage.ID))
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunImagesDelete(config)
assert.NoError(t, err)
Expand All @@ -122,7 +122,7 @@ func TestImagesDeleteMultiple(t *testing.T) {
tm.images.On("Delete", testImageSecondary.ID).Return(nil)

config.Args = append(config.Args, strconv.Itoa(testImage.ID), strconv.Itoa(testImageSecondary.ID))
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunImagesDelete(config)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions commands/load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func LoadBalancer() *Command {
CmdBuilder(cmd, RunLoadBalancerList, "list", "list load balancers", Writer, aliasOpt("ls"))

cmdRunRecordDelete := CmdBuilder(cmd, RunLoadBalancerDelete, "delete <id>", "delete load balancer", Writer, aliasOpt("d", "rm"))
AddBoolFlag(cmdRunRecordDelete, doctl.ArgDeleteForce, doctl.ArgShortDeleteForce, false, "Force load balancer delete")
AddBoolFlag(cmdRunRecordDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force load balancer delete")

cmdAddDroplets := CmdBuilder(cmd, RunLoadBalancerAddDroplets, "add-droplets <id>", "add droplets to the load balancer", Writer)
AddStringSliceFlag(cmdAddDroplets, doctl.ArgDropletIDs, "", []string{}, "comma-separated list of droplet IDs, example valus: 12,33")
Expand Down Expand Up @@ -155,7 +155,7 @@ func RunLoadBalancerDelete(c *CmdConfig) error {
}
lbID := c.Args[0]

force, err := c.Doit.GetBool(c.NS, doctl.ArgDeleteForce)
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/load_balancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestLoadBalancerDelete(t *testing.T) {
tm.loadBalancers.On("Delete", lbID).Return(nil)

config.Args = append(config.Args, lbID)
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunLoadBalancerDelete(config)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions commands/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Snapshot() *Command {

cmdRunSnapshotDelete := CmdBuilder(cmd, RunSnapshotDelete, "delete <snapshot-id> [snapshot-id ...]", "delete snapshot", Writer,
aliasOpt("d"), displayerType(&droplet{}), docCategories("snapshot"))
AddBoolFlag(cmdRunSnapshotDelete, doctl.ArgDeleteForce, doctl.ArgShortDeleteForce, false, "Force snapshot delete")
AddBoolFlag(cmdRunSnapshotDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force snapshot delete")

return cmd
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func RunSnapshotDelete(c *CmdConfig) error {
return doctl.NewMissingArgsErr(c.NS)
}

force, err := c.Doit.GetBool(c.NS, doctl.ArgDeleteForce)
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestSnapshotDelete(t *testing.T) {
tm.snapshots.On("Delete", testSnapshot.ID).Return(nil)

config.Args = append(config.Args, testSnapshot.ID)
config.Doit.Set(config.NS, doctl.ArgDeleteForce, true)
config.Doit.Set(config.NS, doctl.ArgForce, true)

err := RunSnapshotDelete(config)
assert.NoError(t, err)
Expand Down
Loading

0 comments on commit 92fd1a6

Please sign in to comment.