github.com/vmware/govmomi@v0.37.1/govc/extension/info.go (about) 1 /* 2 Copyright (c) 2015-2024 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 extension 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/object" 29 "github.com/vmware/govmomi/vim25/types" 30 ) 31 32 type info struct { 33 *flags.ClientFlag 34 *flags.OutputFlag 35 } 36 37 func init() { 38 cli.Register("extension.info", &info{}) 39 } 40 41 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 42 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 43 cmd.ClientFlag.Register(ctx, f) 44 45 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 46 cmd.OutputFlag.Register(ctx, f) 47 } 48 49 func (cmd *info) Process(ctx context.Context) error { 50 if err := cmd.ClientFlag.Process(ctx); err != nil { 51 return err 52 } 53 if err := cmd.OutputFlag.Process(ctx); err != nil { 54 return err 55 } 56 return nil 57 } 58 59 func (cmd *info) Usage() string { 60 return "[KEY]..." 61 } 62 63 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 64 c, err := cmd.Client() 65 if err != nil { 66 return err 67 } 68 69 m, err := object.GetExtensionManager(c) 70 if err != nil { 71 return err 72 } 73 74 var res infoResult 75 exts := make(map[string]types.Extension) 76 77 if f.NArg() == 1 { 78 e, err := m.Find(ctx, f.Arg(0)) 79 if err != nil { 80 return err 81 } 82 if e != nil { 83 exts[f.Arg(0)] = *e 84 } 85 } else { 86 list, err := m.List(ctx) 87 if err != nil { 88 return err 89 } 90 if f.NArg() == 0 { 91 res.Extensions = list 92 } else { 93 for _, e := range list { 94 exts[e.Key] = e 95 } 96 } 97 } 98 99 for _, key := range f.Args() { 100 if e, ok := exts[key]; ok { 101 res.Extensions = append(res.Extensions, e) 102 } else { 103 return fmt.Errorf("extension %s not found", key) 104 } 105 } 106 107 return cmd.WriteResult(&res) 108 } 109 110 type infoResult struct { 111 Extensions []types.Extension `json:"extensions"` 112 } 113 114 func (r *infoResult) Write(w io.Writer) error { 115 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 116 117 for _, e := range r.Extensions { 118 fmt.Fprintf(tw, "Name:\t%s\n", e.Key) 119 fmt.Fprintf(tw, " Version:\t%s\n", e.Version) 120 fmt.Fprintf(tw, " Description:\t%s\n", e.Description.GetDescription().Summary) 121 fmt.Fprintf(tw, " Company:\t%s\n", e.Company) 122 fmt.Fprintf(tw, " Last heartbeat time:\t%s\n", e.LastHeartbeatTime) 123 fmt.Fprintf(tw, " Subject name:\t%s\n", e.SubjectName) 124 fmt.Fprintf(tw, " Type:\t%s\n", e.Type) 125 } 126 127 return tw.Flush() 128 }