github.com/vmware/govmomi@v0.43.0/govc/vm/dataset/rm.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 dataset
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  	"github.com/vmware/govmomi/vapi/vm/dataset"
    26  )
    27  
    28  type rm struct {
    29  	*flags.VirtualMachineFlag
    30  	force bool
    31  }
    32  
    33  func init() {
    34  	cli.Register("vm.dataset.rm", &rm{})
    35  }
    36  
    37  func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
    38  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    39  	cmd.VirtualMachineFlag.Register(ctx, f)
    40  	f.BoolVar(&cmd.force, "force", false, "Delete the data set even if it has entries")
    41  }
    42  
    43  func (cmd *rm) Process(ctx context.Context) error {
    44  	return cmd.VirtualMachineFlag.Process(ctx)
    45  }
    46  
    47  func (cmd *rm) Usage() string {
    48  	return "NAME"
    49  }
    50  
    51  func (cmd *rm) Description() string {
    52  	return `Delete data set.
    53  
    54  Fails if the data set has entries, unless the '-force' flag is set.
    55  
    56  Examples:
    57    govc vm.dataset.rm -vm $vm com.example.project2
    58    govc vm.dataset.rm -vm $vm -force=true com.example.project3`
    59  }
    60  
    61  func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
    62  	if f.NArg() != 1 {
    63  		return flag.ErrHelp
    64  	}
    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  	c, err := cmd.RestClient()
    76  	if err != nil {
    77  		return err
    78  	}
    79  	mgr := dataset.NewManager(c)
    80  
    81  	id, err := FindDataSetId(ctx, mgr, vmId, f.Arg(0))
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	err = mgr.DeleteDataSet(ctx, vmId, id, cmd.force)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	return nil
    92  }