github.com/vmware/govmomi@v0.43.0/govc/vm/dataset/entry/set.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 23 "github.com/vmware/govmomi/govc/cli" 24 "github.com/vmware/govmomi/govc/flags" 25 govc "github.com/vmware/govmomi/govc/vm/dataset" 26 "github.com/vmware/govmomi/vapi/vm/dataset" 27 ) 28 29 type set struct { 30 *flags.VirtualMachineFlag 31 dataSet string 32 } 33 34 func init() { 35 cli.Register("vm.dataset.entry.set", &set{}) 36 } 37 38 func (cmd *set) Register(ctx context.Context, f *flag.FlagSet) { 39 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 40 cmd.VirtualMachineFlag.Register(ctx, f) 41 f.StringVar(&cmd.dataSet, "dataset", "", "Data set name or ID") 42 } 43 44 func (cmd *set) Process(ctx context.Context) error { 45 return cmd.VirtualMachineFlag.Process(ctx) 46 } 47 48 func (cmd *set) Usage() string { 49 return "KEY VALUE" 50 } 51 52 func (cmd *set) Description() string { 53 return `Set the value of a data set entry. 54 55 Examples: 56 govc vm.dataset.entry.set -vm $vm -dataset com.example.project2 somekey somevalue` 57 } 58 59 func (cmd *set) Run(ctx context.Context, f *flag.FlagSet) error { 60 if f.NArg() != 2 { 61 return flag.ErrHelp 62 } 63 entryKey := f.Arg(0) 64 entryValue := f.Arg(1) 65 66 vm, err := cmd.VirtualMachineFlag.VirtualMachine() 67 if err != nil { 68 return err 69 } 70 if vm == nil { 71 return flag.ErrHelp 72 } 73 vmId := vm.Reference().Value 74 75 if cmd.dataSet == "" { 76 return flag.ErrHelp 77 } 78 79 c, err := cmd.RestClient() 80 if err != nil { 81 return err 82 } 83 mgr := dataset.NewManager(c) 84 dataSetId, err := govc.FindDataSetId(ctx, mgr, vmId, cmd.dataSet) 85 if err != nil { 86 return err 87 } 88 return mgr.SetEntry(ctx, vmId, dataSetId, entryKey, entryValue) 89 }