github.com/vmware/govmomi@v0.43.0/govc/vm/dataset/entry/get.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  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	govc "github.com/vmware/govmomi/govc/vm/dataset"
    27  	"github.com/vmware/govmomi/vapi/vm/dataset"
    28  )
    29  
    30  type get struct {
    31  	*flags.VirtualMachineFlag
    32  	dataSet string
    33  }
    34  
    35  func init() {
    36  	cli.Register("vm.dataset.entry.get", &get{})
    37  }
    38  
    39  func (cmd *get) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    41  	cmd.VirtualMachineFlag.Register(ctx, f)
    42  	f.StringVar(&cmd.dataSet, "dataset", "", "Data set name or ID")
    43  }
    44  
    45  func (cmd *get) Process(ctx context.Context) error {
    46  	return cmd.VirtualMachineFlag.Process(ctx)
    47  }
    48  
    49  func (cmd *get) Usage() string {
    50  	return "KEY"
    51  }
    52  
    53  func (cmd *get) Description() string {
    54  	return `Read the value of a data set entry.
    55  
    56  Examples:
    57    govc vm.dataset.entry.get -vm $vm -dataset com.example.project2 somekey`
    58  }
    59  
    60  func (cmd *get) Run(ctx context.Context, f *flag.FlagSet) error {
    61  	if f.NArg() != 1 {
    62  		return flag.ErrHelp
    63  	}
    64  	entryKey := f.Arg(0)
    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  	entryValue, err := mgr.GetEntry(ctx, vmId, dataSetId, entryKey)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	fmt.Println(entryValue)
    94  	return nil
    95  }