1package commands 2 3import ( 4 "fmt" 5 6 "strings" 7 8 "errors" 9 10 "github.com/docker/machine/libmachine" 11 "github.com/docker/machine/libmachine/log" 12) 13 14func cmdRm(c CommandLine, api libmachine.API) error { 15 if len(c.Args()) == 0 { 16 c.ShowHelp() 17 return ErrNoMachineSpecified 18 } 19 20 log.Info(fmt.Sprintf("About to remove %s", strings.Join(c.Args(), ", "))) 21 log.Warn("WARNING: This action will delete both local reference and remote instance.") 22 23 force := c.Bool("force") 24 confirm := c.Bool("y") 25 var errorOccurred []string 26 27 if !userConfirm(confirm, force) { 28 return nil 29 } 30 31 for _, hostName := range c.Args() { 32 err := removeRemoteMachine(hostName, api) 33 if err != nil { 34 errorOccurred = collectError(fmt.Sprintf("Error removing host %q: %s", hostName, err), force, errorOccurred) 35 } 36 37 if err == nil || force { 38 removeErr := removeLocalMachine(hostName, api) 39 if removeErr != nil { 40 errorOccurred = collectError(fmt.Sprintf("Can't remove \"%s\"", hostName), force, errorOccurred) 41 } else { 42 log.Infof("Successfully removed %s", hostName) 43 } 44 } 45 } 46 47 if len(errorOccurred) > 0 && !force { 48 return errors.New(strings.Join(errorOccurred, "\n")) 49 } 50 51 return nil 52} 53 54func userConfirm(confirm bool, force bool) bool { 55 if confirm || force { 56 return true 57 } 58 59 sure, err := confirmInput(fmt.Sprintf("Are you sure?")) 60 if err != nil { 61 return false 62 } 63 64 return sure 65} 66 67func removeRemoteMachine(hostName string, api libmachine.API) error { 68 currentHost, loaderr := api.Load(hostName) 69 if loaderr != nil { 70 return loaderr 71 } 72 73 return currentHost.Driver.Remove() 74} 75 76func removeLocalMachine(hostName string, api libmachine.API) error { 77 exist, _ := api.Exists(hostName) 78 if !exist { 79 return errors.New(hostName + " does not exist.") 80 } 81 return api.Remove(hostName) 82} 83 84func collectError(message string, force bool, errorOccurred []string) []string { 85 if force { 86 log.Error(message) 87 } 88 return append(errorOccurred, message) 89} 90