|
| 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 aliyun |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + cmdTagImage = &cobra.Command{ |
| 27 | + Use: "tag-image --id <id> --tags foo=bar ...", |
| 28 | + Short: "Tag an image", |
| 29 | + Run: runTagImage, |
| 30 | + } |
| 31 | + id string |
| 32 | + tags []string |
| 33 | + region string |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + // Initialize the command and its flags |
| 38 | + Aliyun.AddCommand(cmdTagImage) |
| 39 | + cmdTagImage.Flags().StringVar(&id, "id", "", "Aliyun Image ID") |
| 40 | + cmdTagImage.Flags().StringVar(®ion, "region", "", "Region") |
| 41 | + cmdTagImage.Flags().StringSliceVar(&tags, "tags", []string{}, "list of key=value tags to attach to the Aliyun image") |
| 42 | +} |
| 43 | + |
| 44 | +func runTagImage(cmd *cobra.Command, args []string) { |
| 45 | + if id == "" { |
| 46 | + fmt.Fprintf(os.Stderr, "Provide --id to tag\n") |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + if region == "" { |
| 51 | + fmt.Fprintf(os.Stderr, "Provide --region\n") |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + |
| 55 | + tagMap := make(map[string]string) |
| 56 | + for _, tag := range tags { |
| 57 | + splitTag := strings.SplitN(tag, "=", 2) |
| 58 | + if len(splitTag) != 2 { |
| 59 | + fmt.Fprintf(os.Stderr, "invalid tag format; should be key=value, not %v\n", tag) |
| 60 | + os.Exit(1) |
| 61 | + } |
| 62 | + key, value := splitTag[0], splitTag[1] |
| 63 | + tagMap[key] = value |
| 64 | + } |
| 65 | + |
| 66 | + err := API.CreateTags(id, tagMap, region) |
| 67 | + if err != nil { |
| 68 | + fmt.Fprintf(os.Stderr, "couldn't create image tags: %v", err) |
| 69 | + os.Exit(1) |
| 70 | + } |
| 71 | +} |
0 commit comments