github.com/vmware/govmomi@v0.37.2/govc/device/floppy/insert.go (about)

     1  /*
     2  Copyright (c) 2014-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 floppy
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  )
    26  
    27  type insert struct {
    28  	*flags.DatastoreFlag
    29  	*flags.VirtualMachineFlag
    30  
    31  	device string
    32  }
    33  
    34  func init() {
    35  	cli.Register("device.floppy.insert", &insert{})
    36  }
    37  
    38  func (cmd *insert) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
    40  	cmd.DatastoreFlag.Register(ctx, f)
    41  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    42  	cmd.VirtualMachineFlag.Register(ctx, f)
    43  
    44  	f.StringVar(&cmd.device, "device", "", "Floppy device name")
    45  }
    46  
    47  func (cmd *insert) Process(ctx context.Context) error {
    48  	if err := cmd.DatastoreFlag.Process(ctx); err != nil {
    49  		return err
    50  	}
    51  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    52  		return err
    53  	}
    54  	return nil
    55  }
    56  
    57  func (cmd *insert) Usage() string {
    58  	return "IMG"
    59  }
    60  
    61  func (cmd *insert) Description() string {
    62  	return `Insert IMG on datastore into floppy device.
    63  
    64  If device is not specified, the first floppy device is used.
    65  
    66  Examples:
    67    govc device.floppy.insert -vm vm-1 vm-1/config.img`
    68  }
    69  
    70  func (cmd *insert) Run(ctx context.Context, f *flag.FlagSet) error {
    71  	vm, err := cmd.VirtualMachine()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	if vm == nil || f.NArg() != 1 {
    77  		return flag.ErrHelp
    78  	}
    79  
    80  	devices, err := vm.Device(ctx)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	c, err := devices.FindFloppy(cmd.device)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	img, err := cmd.DatastorePath(f.Arg(0))
    91  	if err != nil {
    92  		return nil
    93  	}
    94  
    95  	return vm.EditDevice(ctx, devices.InsertImg(c, img))
    96  }