github.com/vmware/govmomi@v0.43.0/govc/tags/info.go (about) 1 /* 2 Copyright (c) 2018 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tags 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "text/tabwriter" 25 26 "github.com/vmware/govmomi/govc/cli" 27 "github.com/vmware/govmomi/govc/flags" 28 "github.com/vmware/govmomi/vapi/tags" 29 ) 30 31 type info struct { 32 *flags.ClientFlag 33 *flags.OutputFlag 34 c string 35 C bool 36 } 37 38 func init() { 39 cli.Register("tags.info", &info{}) 40 } 41 42 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 43 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 44 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 45 cmd.ClientFlag.Register(ctx, f) 46 cmd.OutputFlag.Register(ctx, f) 47 f.StringVar(&cmd.c, "c", "", "Category name") 48 f.BoolVar(&cmd.C, "C", true, "Display category name instead of ID") 49 } 50 51 func (cmd *info) Process(ctx context.Context) error { 52 if err := cmd.ClientFlag.Process(ctx); err != nil { 53 return err 54 } 55 return cmd.OutputFlag.Process(ctx) 56 } 57 58 func (cmd *info) Usage() string { 59 return "NAME" 60 } 61 62 func (cmd *info) Description() string { 63 return `Display tags info. 64 65 If NAME is provided, display info for only that tag. Otherwise display info for all tags. 66 67 Examples: 68 govc tags.info 69 govc tags.info k8s-zone-us-ca1 70 govc tags.info -c k8s-zone` 71 } 72 73 type infoResult []tags.Tag 74 75 func (t infoResult) Write(w io.Writer) error { 76 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 77 78 for _, item := range t { 79 fmt.Fprintf(tw, "Name:\t%s\n", item.Name) 80 fmt.Fprintf(tw, " ID:\t%s\n", item.ID) 81 fmt.Fprintf(tw, " Description:\t%s\n", item.Description) 82 fmt.Fprintf(tw, " Category:\t%s\n", item.CategoryID) 83 fmt.Fprintf(tw, " UsedBy: %s\n", item.UsedBy) 84 } 85 86 return tw.Flush() 87 } 88 89 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 90 c, err := cmd.RestClient() 91 if err != nil { 92 return err 93 } 94 95 m := tags.NewManager(c) 96 var res lsResult 97 98 if cmd.c == "" { 99 res, err = m.GetTags(ctx) 100 } else { 101 res, err = m.GetTagsForCategory(ctx, cmd.c) 102 } 103 if err != nil { 104 return err 105 } 106 107 if f.NArg() == 1 { 108 arg := f.Arg(0) 109 src := res 110 res = nil 111 for i := range src { 112 if src[i].Name == arg || src[i].ID == arg { 113 res = append(res, src[i]) 114 } 115 } 116 if len(res) == 0 { 117 return fmt.Errorf("tag %q not found", arg) 118 } 119 } 120 121 if cmd.C { 122 categories, err := m.GetCategories(ctx) 123 if err != nil { 124 return err 125 } 126 m := make(map[string]tags.Category) 127 for _, category := range categories { 128 m[category.ID] = category 129 } 130 for i := range res { 131 res[i].CategoryID = m[res[i].CategoryID].Name 132 } 133 } 134 135 return cmd.WriteResult(infoResult(res)) 136 }