github.com/vmware/govmomi@v0.43.0/govc/vm/dataset/entry/ls.go (about) 1 /* 2 Copyright (c) 2023-2023 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 entry 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 govc "github.com/vmware/govmomi/govc/vm/dataset" 28 "github.com/vmware/govmomi/vapi/vm/dataset" 29 ) 30 31 type ls struct { 32 *flags.VirtualMachineFlag 33 *flags.OutputFlag 34 dataSet string 35 } 36 37 func init() { 38 cli.Register("vm.dataset.entry.ls", &ls{}) 39 } 40 41 func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { 42 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 43 cmd.OutputFlag.Register(ctx, f) 44 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 45 cmd.VirtualMachineFlag.Register(ctx, f) 46 f.StringVar(&cmd.dataSet, "dataset", "", "Data set name or ID") 47 } 48 49 func (cmd *ls) Process(ctx context.Context) error { 50 if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { 51 return err 52 } 53 return cmd.OutputFlag.Process(ctx) 54 } 55 56 func (cmd *ls) Description() string { 57 return `List the keys of the entries in a data set. 58 59 Examples: 60 govc vm.dataset.entry.ls -vm $vm -dataset com.example.project2` 61 } 62 63 type lsResult []string 64 65 func (r lsResult) Write(w io.Writer) error { 66 for _, key := range r { 67 fmt.Fprintln(w, key) 68 } 69 return nil 70 } 71 72 func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { 73 74 vm, err := cmd.VirtualMachineFlag.VirtualMachine() 75 if err != nil { 76 return err 77 } 78 if vm == nil { 79 return flag.ErrHelp 80 } 81 vmId := vm.Reference().Value 82 83 if cmd.dataSet == "" { 84 return flag.ErrHelp 85 } 86 87 c, err := cmd.RestClient() 88 if err != nil { 89 return err 90 } 91 mgr := dataset.NewManager(c) 92 dataSetId, err := govc.FindDataSetId(ctx, mgr, vmId, cmd.dataSet) 93 if err != nil { 94 return err 95 } 96 l, err := mgr.ListEntries(ctx, vmId, dataSetId) 97 if err != nil { 98 return err 99 } 100 return cmd.WriteResult(lsResult(l)) 101 }