github.com/vmware/govmomi@v0.43.0/govc/vm/snapshot/remove.go (about) 1 /* 2 Copyright (c) 2016 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 snapshot 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/object" 26 ) 27 28 type remove struct { 29 *flags.VirtualMachineFlag 30 31 recursive bool 32 consolidate bool 33 } 34 35 func init() { 36 cli.Register("snapshot.remove", &remove{}) 37 } 38 39 func (cmd *remove) Register(ctx context.Context, f *flag.FlagSet) { 40 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 41 cmd.VirtualMachineFlag.Register(ctx, f) 42 43 f.BoolVar(&cmd.recursive, "r", false, "Remove snapshot children") 44 f.BoolVar(&cmd.consolidate, "c", true, "Consolidate disks") 45 } 46 47 func (cmd *remove) Usage() string { 48 return "NAME" 49 } 50 51 func (cmd *remove) Description() string { 52 return `Remove snapshot of VM with given NAME. 53 54 NAME can be the snapshot name, tree path, moid or '*' to remove all snapshots. 55 56 Examples: 57 govc snapshot.remove -vm my-vm happy-vm-state` 58 } 59 60 func (cmd *remove) Process(ctx context.Context) error { 61 if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { 62 return err 63 } 64 return nil 65 } 66 67 func (cmd *remove) Run(ctx context.Context, f *flag.FlagSet) error { 68 if f.NArg() != 1 { 69 return flag.ErrHelp 70 } 71 72 vm, err := cmd.VirtualMachine() 73 if err != nil { 74 return err 75 } 76 77 if vm == nil { 78 return flag.ErrHelp 79 } 80 81 var task *object.Task 82 83 if f.Arg(0) == "*" { 84 task, err = vm.RemoveAllSnapshot(ctx, &cmd.consolidate) 85 } else { 86 task, err = vm.RemoveSnapshot(ctx, f.Arg(0), cmd.recursive, &cmd.consolidate) 87 } 88 89 if err != nil { 90 return err 91 } 92 93 return task.Wait(ctx) 94 }