github.com/vmware/govmomi@v0.37.1/govc/datastore/disk/info.go (about) 1 /* 2 Copyright (c) 2017 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 disk 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "strings" 25 "text/tabwriter" 26 27 "github.com/vmware/govmomi/govc/cli" 28 "github.com/vmware/govmomi/govc/flags" 29 "github.com/vmware/govmomi/object" 30 ) 31 32 type info struct { 33 *flags.DatastoreFlag 34 35 c bool 36 d bool 37 p bool 38 39 uuid bool 40 } 41 42 func init() { 43 cli.Register("datastore.disk.info", &info{}) 44 } 45 46 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 47 cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx) 48 cmd.DatastoreFlag.Register(ctx, f) 49 50 f.BoolVar(&cmd.c, "c", false, "Chain format") 51 f.BoolVar(&cmd.d, "d", false, "Include datastore in output") 52 f.BoolVar(&cmd.p, "p", true, "Include parents") 53 f.BoolVar(&cmd.uuid, "uuid", false, "Include disk UUID") 54 } 55 56 func (cmd *info) Process(ctx context.Context) error { 57 if err := cmd.DatastoreFlag.Process(ctx); err != nil { 58 return err 59 } 60 return nil 61 } 62 63 func (cmd *info) Usage() string { 64 return "VMDK" 65 } 66 67 func (cmd *info) Description() string { 68 return `Query VMDK info on DS. 69 70 Examples: 71 govc datastore.disk.info disks/disk1.vmdk` 72 } 73 74 func fullPath(s string) string { 75 return s 76 } 77 78 func dsPath(s string) string { 79 var p object.DatastorePath 80 81 if p.FromString(s) { 82 return p.Path 83 } 84 85 return s 86 } 87 88 var infoPath = dsPath 89 90 var queryUUID func(string) string 91 92 type infoResult []object.VirtualDiskInfo 93 94 func (r infoResult) Write(w io.Writer) error { 95 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 96 97 for _, info := range r { 98 fmt.Fprintf(tw, "Name:\t%s\n", infoPath(info.Name)) 99 if queryUUID != nil { 100 fmt.Fprintf(tw, " UUID:\t%s\n", queryUUID(info.Name)) 101 } 102 fmt.Fprintf(tw, " Type:\t%s\n", info.DiskType) 103 fmt.Fprintf(tw, " Parent:\t%s\n", infoPath(info.Parent)) 104 } 105 106 return tw.Flush() 107 } 108 109 type chainResult []object.VirtualDiskInfo 110 111 func (r chainResult) Write(w io.Writer) error { 112 for i, info := range r { 113 fmt.Fprint(w, strings.Repeat(" ", i*2)) 114 fmt.Fprintln(w, infoPath(info.Name)) 115 } 116 117 return nil 118 } 119 120 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 121 if f.NArg() != 1 { 122 return flag.ErrHelp 123 } 124 125 dc, err := cmd.Datacenter() 126 if err != nil { 127 return err 128 } 129 130 ds, err := cmd.Datastore() 131 if err != nil { 132 return err 133 } 134 135 m := object.NewVirtualDiskManager(ds.Client()) 136 137 if cmd.uuid { 138 queryUUID = func(name string) string { 139 id, _ := m.QueryVirtualDiskUuid(ctx, name, dc) 140 return id 141 } 142 } 143 144 info, err := m.QueryVirtualDiskInfo(ctx, ds.Path(f.Arg(0)), dc, cmd.p) 145 if err != nil { 146 return err 147 } 148 149 if cmd.d { 150 infoPath = fullPath 151 } 152 153 var r flags.OutputWriter = infoResult(info) 154 155 if cmd.c { 156 r = chainResult(info) 157 } 158 159 return cmd.WriteResult(r) 160 }