github.com/vmware/govmomi@v0.51.0/cli/library/subscriber/info.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package subscriber 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 "io" 12 "text/tabwriter" 13 14 "github.com/vmware/govmomi/cli" 15 "github.com/vmware/govmomi/cli/flags" 16 "github.com/vmware/govmomi/find" 17 "github.com/vmware/govmomi/vapi/library" 18 "github.com/vmware/govmomi/vim25/types" 19 ) 20 21 type info struct { 22 *flags.ClientFlag 23 *flags.OutputFlag 24 } 25 26 func init() { 27 cli.Register("library.subscriber.info", &info{}) 28 } 29 30 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 31 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 32 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 33 cmd.ClientFlag.Register(ctx, f) 34 cmd.OutputFlag.Register(ctx, f) 35 } 36 37 func (cmd *info) Process(ctx context.Context) error { 38 if err := cmd.ClientFlag.Process(ctx); err != nil { 39 return err 40 } 41 return cmd.OutputFlag.Process(ctx) 42 } 43 44 func (cmd *info) Usage() string { 45 return "PUBLISHED-LIBRARY SUBSCRIPTION-ID" 46 } 47 48 func (cmd *info) Description() string { 49 return `Library subscriber info. 50 51 Examples: 52 id=$(govc library.subscriber.ls | grep my-library-name | awk '{print $1}') 53 govc library.subscriber.info published-library-name $id` 54 } 55 56 // path returns the inventory path for id, if possible 57 func (cmd *info) path(kind, id string) string { 58 if id == "" { 59 return "" 60 } 61 ref := types.ManagedObjectReference{Type: kind, Value: id} 62 c, err := cmd.Client() 63 if err == nil { 64 ctx := context.Background() 65 e, err := find.NewFinder(c, false).Element(ctx, ref) 66 if err == nil { 67 return e.Path 68 } 69 } 70 return id 71 } 72 73 type infoResultsWriter struct { 74 *library.Subscriber 75 cmd *info 76 } 77 78 func (r infoResultsWriter) Write(w io.Writer) error { 79 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 80 81 _, _ = fmt.Fprintf(tw, "Name:\t%s\n", r.LibraryName) 82 _, _ = fmt.Fprintf(tw, " ID:\t%s\n", r.LibraryID) 83 _, _ = fmt.Fprintf(tw, " Location:\t%s\n", r.LibraryLocation) 84 85 p := r.cmd.path 86 87 if r.Vcenter != nil { 88 p = func(kind, id string) string { return id } 89 port := "" 90 if r.Vcenter.Port != 0 { 91 port = fmt.Sprintf(":%d", r.Vcenter.Port) 92 } 93 _, _ = fmt.Fprintf(tw, " vCenter:\t\n") 94 _, _ = fmt.Fprintf(tw, " URL:\thttps://%s%s\n", r.Vcenter.Hostname, port) 95 _, _ = fmt.Fprintf(tw, " GUID:\t%s\n", r.Vcenter.ServerGUID) 96 } 97 98 _, _ = fmt.Fprintf(tw, " Placement:\t\n") 99 _, _ = fmt.Fprintf(tw, " Folder:\t%s\n", p("Folder", r.Placement.Folder)) 100 _, _ = fmt.Fprintf(tw, " Cluster:\t%s\n", p("ClusterComputeResource", r.Placement.Cluster)) 101 _, _ = fmt.Fprintf(tw, " Pool:\t%s\n", p("ResourcePool", r.Placement.ResourcePool)) 102 _, _ = fmt.Fprintf(tw, " Network:\t%s\n", p("Network", r.Placement.Network)) 103 104 return tw.Flush() 105 } 106 107 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 108 c, err := cmd.RestClient() 109 if err != nil { 110 return err 111 } 112 113 lib, err := flags.ContentLibrary(ctx, c, f.Arg(0)) 114 if err != nil { 115 return err 116 } 117 m := library.NewManager(c) 118 119 s, err := m.GetSubscriber(ctx, lib, f.Arg(1)) 120 if err != nil { 121 return err 122 } 123 124 return cmd.WriteResult(infoResultsWriter{s, cmd}) 125 }