github.com/vmware/govmomi@v0.43.0/govc/vm/guest/mkdir.go (about)

     1  /*
     2  Copyright (c) 2014-2017 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 guest
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/vim25/soap"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  type mkdir struct {
    29  	*GuestFlag
    30  
    31  	createParents bool
    32  }
    33  
    34  func init() {
    35  	cli.Register("guest.mkdir", &mkdir{})
    36  }
    37  
    38  func (cmd *mkdir) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.GuestFlag, ctx = newGuestFlag(ctx)
    40  	cmd.GuestFlag.Register(ctx, f)
    41  
    42  	f.BoolVar(&cmd.createParents, "p", false, "Create intermediate directories as needed")
    43  }
    44  
    45  func (cmd *mkdir) Process(ctx context.Context) error {
    46  	if err := cmd.GuestFlag.Process(ctx); err != nil {
    47  		return err
    48  	}
    49  	return nil
    50  }
    51  
    52  func (cmd *mkdir) Usage() string {
    53  	return "PATH"
    54  }
    55  
    56  func (cmd *mkdir) Description() string {
    57  	return `Create directory PATH in VM.
    58  
    59  Examples:
    60    govc guest.mkdir -vm $name /tmp/logs
    61    govc guest.mkdir -vm $name -p /tmp/logs/foo/bar`
    62  }
    63  
    64  func (cmd *mkdir) Run(ctx context.Context, f *flag.FlagSet) error {
    65  	m, err := cmd.FileManager()
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	err = m.MakeDirectory(ctx, cmd.Auth(), f.Arg(0), cmd.createParents)
    71  
    72  	// ignore EEXIST if -p flag is given
    73  	if err != nil && cmd.createParents {
    74  		if soap.IsSoapFault(err) {
    75  			soapFault := soap.ToSoapFault(err)
    76  			if _, ok := soapFault.VimFault().(types.FileAlreadyExists); ok {
    77  				return nil
    78  			}
    79  		}
    80  	}
    81  
    82  	return err
    83  }