github.com/vmware/govmomi@v0.43.0/govc/vm/guest/ls.go (about) 1 /* 2 Copyright (c) 2014-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 guest 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "os" 24 "text/tabwriter" 25 26 "github.com/vmware/govmomi/govc/cli" 27 "github.com/vmware/govmomi/units" 28 "github.com/vmware/govmomi/vim25/types" 29 ) 30 31 type ls struct { 32 *GuestFlag 33 34 simple bool 35 } 36 37 func init() { 38 cli.Register("guest.ls", &ls{}) 39 } 40 41 func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { 42 cmd.GuestFlag, ctx = newGuestFlag(ctx) 43 cmd.GuestFlag.Register(ctx, f) 44 45 f.BoolVar(&cmd.simple, "s", false, "Simple path only listing") // sadly we used '-l' for guest login 46 } 47 48 func (cmd *ls) Process(ctx context.Context) error { 49 if err := cmd.GuestFlag.Process(ctx); err != nil { 50 return err 51 } 52 return nil 53 } 54 55 func (cmd *ls) Usage() string { 56 return "PATH" 57 } 58 59 func (cmd *ls) Description() string { 60 return `List PATH files in VM. 61 62 Examples: 63 govc guest.ls -vm $name /tmp` 64 } 65 66 func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { 67 m, err := cmd.FileManager() 68 if err != nil { 69 return err 70 } 71 72 var offset int32 73 tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) 74 75 for { 76 info, err := m.ListFiles(ctx, cmd.Auth(), f.Arg(0), offset, 0, f.Arg(1)) 77 if err != nil { 78 return err 79 } 80 81 for _, f := range info.Files { 82 if cmd.simple { 83 fmt.Fprintln(tw, f.Path) 84 continue 85 } 86 87 var kind byte 88 89 switch types.GuestFileType(f.Type) { 90 case types.GuestFileTypeDirectory: 91 kind = 'd' 92 if f.Size == 0 { 93 f.Size = 4092 94 } 95 case types.GuestFileTypeSymlink: 96 kind = 'l' 97 case types.GuestFileTypeFile: 98 kind = '-' 99 } 100 101 switch x := f.Attributes.(type) { 102 case *types.GuestPosixFileAttributes: 103 perm := os.FileMode(x.Permissions).Perm().String()[1:] 104 fmt.Fprintf(tw, "%c%s\t%d\t%d\t", kind, perm, *x.OwnerId, *x.GroupId) 105 } 106 107 attr := f.Attributes.GetGuestFileAttributes() 108 109 fmt.Fprintf(tw, "%s\t%s\t%s", units.FileSize(f.Size), attr.ModificationTime.Format("Jan 2 15:04 2006"), f.Path) 110 if attr.SymlinkTarget != "" { 111 fmt.Fprintf(tw, " -> %s", attr.SymlinkTarget) 112 } 113 fmt.Fprintln(tw) 114 } 115 116 err = tw.Flush() 117 if err != nil { 118 return err 119 } 120 121 if info.Remaining == 0 { 122 break 123 } 124 offset += int32(len(info.Files)) 125 } 126 127 return nil 128 }