github.com/vmware/govmomi@v0.43.0/govc/vm/guest/df.go (about) 1 /* 2 Copyright (c) 2019 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 guest 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/units" 29 "github.com/vmware/govmomi/vim25/mo" 30 "github.com/vmware/govmomi/vim25/types" 31 ) 32 33 type df struct { 34 *flags.VirtualMachineFlag 35 } 36 37 func init() { 38 cli.Register("guest.df", &df{}) 39 } 40 41 func (cmd *df) Register(ctx context.Context, f *flag.FlagSet) { 42 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 43 cmd.VirtualMachineFlag.Register(ctx, f) 44 } 45 46 func (cmd *df) Description() string { 47 return `Report file system disk space usage. 48 49 Examples: 50 govc guest.df -vm $name` 51 } 52 53 type dfResult []types.GuestDiskInfo 54 55 func (r dfResult) Write(w io.Writer) error { 56 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 57 _, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\n", "Filesystem", "Size", "Used", "Avail", "Use%") 58 for _, disk := range r { 59 used := disk.Capacity - disk.FreeSpace 60 use := 100.0 * float32(used) / float32(disk.Capacity) 61 _, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%.0f%%\n", disk.DiskPath, 62 units.ByteSize(disk.Capacity), units.ByteSize(used), units.ByteSize(disk.FreeSpace), use) 63 } 64 return tw.Flush() 65 } 66 67 func (cmd *df) Run(ctx context.Context, f *flag.FlagSet) error { 68 obj, err := cmd.VirtualMachine() 69 if err != nil { 70 return err 71 } 72 73 var vm mo.VirtualMachine 74 err = obj.Properties(ctx, obj.Reference(), []string{"guest.disk"}, &vm) 75 if err != nil { 76 return err 77 } 78 79 return cmd.WriteResult(dfResult(vm.Guest.Disk)) 80 }