|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "code.cloudfoundry.org/cli/cf/flags" |
| 5 | + "code.cloudfoundry.org/cli/plugin" |
| 6 | + "fmt" |
| 7 | + "gopkg.in/yaml.v2" |
| 8 | +) |
| 9 | + |
| 10 | +type cfDeleteWrapper struct{} |
| 11 | + |
| 12 | +// Flags that is used by the cf delete wrapper |
| 13 | +type WrapperFlags struct { |
| 14 | + AppName string |
| 15 | + Force bool |
| 16 | +} |
| 17 | + |
| 18 | +// Manifest struct, we only care about the app name |
| 19 | +type Manifest struct { |
| 20 | + Applications []struct { |
| 21 | + Name string `yaml:"name"` |
| 22 | + } `yaml:"applications"` |
| 23 | +} |
| 24 | + |
| 25 | +// Subcommand names |
| 26 | +const ( |
| 27 | + multiAppDeleteCmd = "delete-multi-apps" |
| 28 | + deleteAppUsingManifestcmd = "delete-app-using-manifest" |
| 29 | +) |
| 30 | + |
| 31 | +// Initialize a new flags wrapper |
| 32 | +var cmdOptions = new(WrapperFlags) |
| 33 | + |
| 34 | +// Run is what is executed by the Cloud Foundry CLI |
| 35 | +func (c *cfDeleteWrapper) Run(cliConnection plugin.CliConnection, args []string) { |
| 36 | + switch args[0] { |
| 37 | + case multiAppDeleteCmd: |
| 38 | + c.buildMultiAppDeleteArguments(args, true) |
| 39 | + c.MultiAppDelete(cliConnection) |
| 40 | + case deleteAppUsingManifestcmd: |
| 41 | + c.buildMultiAppDeleteArguments(args, false) |
| 42 | + c.DeleteAppUsingManifest(cliConnection) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// GetMetadata provides the Cloud Foundry CLI with metadata on how to use this plugin |
| 47 | +func (c *cfDeleteWrapper) GetMetadata() plugin.PluginMetadata { |
| 48 | + return plugin.PluginMetadata{ |
| 49 | + Name: "CfDeleteWrapper", |
| 50 | + Version: plugin.VersionType{ |
| 51 | + Major: 0, |
| 52 | + Minor: 1, |
| 53 | + Build: 0, |
| 54 | + }, |
| 55 | + MinCliVersion: plugin.VersionType{ |
| 56 | + Major: 6, |
| 57 | + Minor: 7, |
| 58 | + Build: 0, |
| 59 | + }, |
| 60 | + Commands: []plugin.Command{ |
| 61 | + { |
| 62 | + Name: multiAppDeleteCmd, |
| 63 | + Alias: "dma", |
| 64 | + HelpText: "Delete multiple apps via a single command", |
| 65 | + UsageDetails: plugin.Usage{ |
| 66 | + Usage: fmt.Sprintf("cf %s <APP1>,<APP2>,....,<APPn>", multiAppDeleteCmd), |
| 67 | + Options: map[string]string{ |
| 68 | + "app": "-a, list of apps to be deleted", |
| 69 | + "force": "-f, no need to prompt for confirmation", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + Name: deleteAppUsingManifestcmd, |
| 75 | + Alias: "daum", |
| 76 | + HelpText: "Detect the apps name from manifest and delete it", |
| 77 | + UsageDetails: plugin.Usage{ |
| 78 | + Usage: fmt.Sprintf("cf %s", deleteAppUsingManifestcmd), |
| 79 | + Options: map[string]string{ |
| 80 | + "force": "-f, no need to prompt for confirmation", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Build a list of argument flags to be used by this wrapper |
| 89 | +func (c *cfDeleteWrapper) buildMultiAppDeleteArguments(args []string, mandatory bool) { |
| 90 | + fc := flags.New() |
| 91 | + fc.NewBoolFlag("force", "f", "don't prompt for confirmation") |
| 92 | + fc.NewStringFlag("app", "a", "list of apps to delete") |
| 93 | + err := fc.Parse(args[1:]...) |
| 94 | + if err != nil { |
| 95 | + handleError(fmt.Sprintf("%v", err), true) |
| 96 | + } |
| 97 | + if fc.IsSet("f") { |
| 98 | + cmdOptions.Force = true |
| 99 | + } |
| 100 | + if fc.IsSet("app") { |
| 101 | + cmdOptions.AppName = fc.String("app") |
| 102 | + } |
| 103 | + if cmdOptions.AppName == "" && mandatory { |
| 104 | + handleError("Mandatory argument app name is missing", true) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Program that helps to delete multiple apps via single command |
| 109 | +func (c *cfDeleteWrapper) MultiAppDelete(cli plugin.CliConnection) { |
| 110 | + |
| 111 | + // Get the list of apps to be delete |
| 112 | + apps := spiltString(cmdOptions.AppName) |
| 113 | + |
| 114 | + // Check with user if they want to continue |
| 115 | + if !cmdOptions.Force { |
| 116 | + yesOrNoConfirmation(cmdOptions.AppName) |
| 117 | + } |
| 118 | + |
| 119 | + // Get the app and delete the app |
| 120 | + for _, app := range apps { |
| 121 | + checkDeleteApp(cli, app, false) |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +// Reads the manifest and delete the app |
| 127 | +func (c *cfDeleteWrapper) DeleteAppUsingManifest(cli plugin.CliConnection) { |
| 128 | + |
| 129 | + // Manifest |
| 130 | + m := new(Manifest) |
| 131 | + |
| 132 | + // Check if the manifest file exists and get the content |
| 133 | + output := readManifest() |
| 134 | + |
| 135 | + // Get the app name from the manifest |
| 136 | + err := yaml.Unmarshal(output, &m) |
| 137 | + if err != nil { |
| 138 | + handleError(fmt.Sprintf("Encounteed error when unmarshaling the manifest ym, err: %v", err), true) |
| 139 | + } |
| 140 | + |
| 141 | + // Check & Delete the app name |
| 142 | + if len(m.Applications) > 0 && m.Applications[0].Name != "" { |
| 143 | + appName := m.Applications[0].Name |
| 144 | + if !cmdOptions.Force { |
| 145 | + yesOrNoConfirmation(appName) |
| 146 | + } |
| 147 | + checkDeleteApp(cli, appName, true) |
| 148 | + } else { |
| 149 | + handleError("Unable to find any information from manifest", true) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// Check & Delete the app |
| 154 | +func checkDeleteApp(cli plugin.CliConnection, app string, exit bool) { |
| 155 | + appInfo, _ := cli.GetApp(app) |
| 156 | + |
| 157 | + // App not found |
| 158 | + if appInfo.Guid == "" { |
| 159 | + handleError(fmt.Sprintf("ERROR: App \"%s\" not found on the current org & space, continuing with remaining apps...", app), exit) |
| 160 | + } else { |
| 161 | + // Run the curl command to delete the app |
| 162 | + output, err := cli.CliCommandWithoutTerminalOutput("curl", "-X", "DELETE", fmt.Sprintf("/v2/apps/%s", appInfo.Guid)) |
| 163 | + if err != nil { |
| 164 | + handleError(fmt.Sprintf("ERROR: Received error when deleting the app \"%s\", err: %v", appInfo.Name, err), exit) |
| 165 | + fmt.Println("continuing with other apps, if there is any...") |
| 166 | + } |
| 167 | + |
| 168 | + // If we found any message, that means the CLI wants to tell us something is wrong |
| 169 | + if len(output) > 1 { |
| 170 | + handleError(fmt.Sprintf("ERROR: Something went wrong when deleting the app \"%s\", message: \n%v", appInfo.Name, output), exit) |
| 171 | + fmt.Println("continuing with other apps, if there is any...") |
| 172 | + } else { |
| 173 | + fmt.Println("\nSuccessfully deleted the app \"" + appInfo.Name + "\"") |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +// Initialize and start the plugin |
| 179 | +func main() { |
| 180 | + plugin.Start(new(cfDeleteWrapper)) |
| 181 | +} |
0 commit comments