Skip to content

Commit

Permalink
Merge pull request #162 from moul/force-rm
Browse files Browse the repository at this point in the history
Support of 'scw rm -f' option (Fix #158)
  • Loading branch information
moul committed Sep 7, 2015
2 parents b3d3a3f + aaea5d8 commit 0b0f6d8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,14 @@ Remove one or more servers.

Options:

-f, --force=false Force the removal of a server
-h, --help=false Print usage

Examples:

$ scw rm my-stopped-server my-second-stopped-server
$ scw rm myserver
$ scw rm -f myserver
$ scw rm my-stopped-server my-second-stopped-serv
$ scw rm $(scw ps -q)
$ scw rm $(scw ps | grep mysql | awk '{print $1}')
```
Expand Down Expand Up @@ -1129,6 +1132,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

* `scw info` now prints user/organization info from the API ([#142](https://github.com/scaleway/scaleway-cli/issues/130)
* Added helpers to manipulate new `user_data` API ([#150](https://github.com/scaleway/scaleway-cli/issues/150))
* Support of `scw rm -f/--force` option ([#158](https://github.com/scaleway/scaleway-cli/issues/158))

#### Fixes

Expand Down
4 changes: 3 additions & 1 deletion pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ func StartServerOnce(api *ScalewayAPI, needle string, wait bool, successChan cha
// DeleteServerSafe tries to delete a server using multiple ways
func (a *ScalewayAPI) DeleteServerSafe(serverID string) error {
// FIXME: also delete attached volumes and ip address
// FIXME: call delete and stop -t in parallel to speed up process
err := a.DeleteServer(serverID)
if err == nil {
logrus.Infof("Server '%s' successfuly deleted", serverID)
Expand All @@ -527,7 +528,8 @@ func (a *ScalewayAPI) DeleteServerSafe(serverID string) error {
return nil
}

// FIXME: retry in a loop until timeout or Control+C
logrus.Errorf("Failed to delete server %s", serverID)
logrus.Errorf("Try to run 'scw rm %s' later", serverID)
logrus.Errorf("Try to run 'scw rm -f %s' later", serverID)
return err
}
7 changes: 6 additions & 1 deletion pkg/cli/cmd_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var cmdRm = &Command{
Description: "Remove one or more servers",
Help: "Remove one or more servers.",
Examples: `
$ scw rm myserver
$ scw rm -f myserver
$ scw rm my-stopped-server my-second-stopped-server
$ scw rm $(scw ps -q)
$ scw rm $(scw ps | grep mysql | awk '{print $1}')
Expand All @@ -20,10 +22,12 @@ var cmdRm = &Command{

func init() {
cmdRm.Flag.BoolVar(&rmHelp, []string{"h", "-help"}, false, "Print usage")
cmdRm.Flag.BoolVar(&rmForce, []string{"f", "-force"}, false, "Force the removal of a server")
}

// Flags
var rmHelp bool // -h, --help flag
var rmHelp bool // -h, --help flag
var rmForce bool // -f, --force flag

func runRm(cmd *Command, rawArgs []string) error {
if rmHelp {
Expand All @@ -35,6 +39,7 @@ func runRm(cmd *Command, rawArgs []string) error {

args := commands.RmArgs{
Servers: rawArgs,
Force: rmForce,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunRm(ctx, args)
Expand Down
8 changes: 7 additions & 1 deletion pkg/commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ import (
// RmArgs are flags for the `RunRm` function
type RmArgs struct {
Servers []string
Force bool
}

// RunRm is the handler for 'scw rm'
func RunRm(ctx CommandContext, args RmArgs) error {
hasError := false
for _, needle := range args.Servers {
server := ctx.API.GetServerID(needle)
err := ctx.API.DeleteServer(server)
var err error
if args.Force {
err = ctx.API.DeleteServerSafe(server)
} else {
err = ctx.API.DeleteServer(server)
}
if err != nil {
logrus.Errorf("failed to delete server %s: %s", server, err)
hasError = true
Expand Down

0 comments on commit 0b0f6d8

Please sign in to comment.