github.com/vmware/govmomi@v0.43.0/govc/library/ls.go (about) 1 /* 2 Copyright (c) 2018 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 library 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 25 "github.com/vmware/govmomi/govc/cli" 26 "github.com/vmware/govmomi/govc/flags" 27 "github.com/vmware/govmomi/vapi/library" 28 "github.com/vmware/govmomi/vapi/library/finder" 29 ) 30 31 type ls struct { 32 *flags.ClientFlag 33 *flags.OutputFlag 34 } 35 36 func init() { 37 cli.Register("library.ls", &ls{}) 38 } 39 40 func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { 41 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 42 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 43 cmd.ClientFlag.Register(ctx, f) 44 cmd.OutputFlag.Register(ctx, f) 45 } 46 47 func (cmd *ls) Process(ctx context.Context) error { 48 if err := cmd.ClientFlag.Process(ctx); err != nil { 49 return err 50 } 51 return nil 52 } 53 54 func (cmd *ls) Description() string { 55 return `List libraries, items, and files. 56 57 Examples: 58 govc library.ls 59 govc library.ls /lib1 60 govc library.ls /lib1/item1 61 govc library.ls /lib1/item1/ 62 govc library.ls */ 63 govc library.ls -json | jq . 64 govc library.ls /lib1/item1 -json | jq .` 65 } 66 67 type lsResultsWriter []finder.FindResult 68 69 func (r lsResultsWriter) Write(w io.Writer) error { 70 for _, i := range r { 71 fmt.Fprintln(w, i.GetPath()) 72 } 73 return nil 74 } 75 76 func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { 77 c, err := cmd.RestClient() 78 if err != nil { 79 return err 80 } 81 82 m := library.NewManager(c) 83 finder := finder.NewFinder(m) 84 findResults, err := finder.Find(ctx, f.Args()...) 85 if err != nil { 86 return err 87 } 88 return cmd.WriteResult(lsResultsWriter(findResults)) 89 }