|
| 1 | +// Copyright 2017 CoreOS, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package aws |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + |
| 21 | + "github.com/spf13/cobra" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + cmdDeleteImage = &cobra.Command{ |
| 26 | + Use: "delete-image --ami <ami_id> --snapshot <snapshot_id> ...", |
| 27 | + Short: "Delete AMI and/or snapshot", |
| 28 | + Run: runDeleteImage, |
| 29 | + } |
| 30 | + amiID string |
| 31 | + snapshotID string |
| 32 | + allowMissing bool |
| 33 | +) |
| 34 | + |
| 35 | +func init() { |
| 36 | + // Initialize the command and its flags |
| 37 | + AWS.AddCommand(cmdDeleteImage) |
| 38 | + cmdDeleteImage.Flags().StringVar(&amiID, "ami", "", "AWS ami tag") |
| 39 | + cmdDeleteImage.Flags().StringVar(&snapshotID, "snapshot", "", "AWS snapshot tag") |
| 40 | + cmdDeleteImage.Flags().BoolVar(&allowMissing, "allow-missing", false, "Do not error out on the resource not existing") |
| 41 | +} |
| 42 | + |
| 43 | +func runDeleteImage(cmd *cobra.Command, args []string) { |
| 44 | + // Check if either amiID or snapshotID is provided |
| 45 | + if amiID == "" && snapshotID == "" { |
| 46 | + fmt.Fprintf(os.Stderr, "Provide --ami or --snapshot to delete\n") |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + // Remove resources based on provided flags |
| 51 | + if amiID != "" { |
| 52 | + err := API.RemoveByAmiTag(amiID, allowMissing) |
| 53 | + if err != nil { |
| 54 | + fmt.Fprintf(os.Stderr, "Could not delete %v: %v\n", amiID, err) |
| 55 | + os.Exit(1) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if snapshotID != "" { |
| 60 | + err := API.RemoveBySnapshotTag(snapshotID, allowMissing) |
| 61 | + if err != nil { |
| 62 | + fmt.Fprintf(os.Stderr, "Could not delete %v: %v\n", snapshotID, err) |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + os.Exit(0) |
| 68 | +} |
0 commit comments