github.com/vmware/govmomi@v0.43.0/govc/vm/guest/mktemp.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  	"fmt"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  )
    26  
    27  type mktemp struct {
    28  	*GuestFlag
    29  
    30  	dir    bool
    31  	path   string
    32  	prefix string
    33  	suffix string
    34  }
    35  
    36  func init() {
    37  	cli.Register("guest.mktemp", &mktemp{})
    38  }
    39  
    40  func (cmd *mktemp) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.GuestFlag, ctx = newGuestFlag(ctx)
    42  	cmd.GuestFlag.Register(ctx, f)
    43  
    44  	f.BoolVar(&cmd.dir, "d", false, "Make a directory instead of a file")
    45  	f.StringVar(&cmd.path, "p", "", "If specified, create relative to this directory")
    46  	f.StringVar(&cmd.prefix, "t", "", "Prefix")
    47  	f.StringVar(&cmd.suffix, "s", "", "Suffix")
    48  }
    49  
    50  func (cmd *mktemp) Process(ctx context.Context) error {
    51  	if err := cmd.GuestFlag.Process(ctx); err != nil {
    52  		return err
    53  	}
    54  	return nil
    55  }
    56  
    57  func (cmd *mktemp) Description() string {
    58  	return `Create a temporary file or directory in VM.
    59  
    60  Examples:
    61    govc guest.mktemp -vm $name
    62    govc guest.mktemp -vm $name -d
    63    govc guest.mktemp -vm $name -t myprefix
    64    govc guest.mktemp -vm $name -p /var/tmp/$USER`
    65  }
    66  
    67  func (cmd *mktemp) Run(ctx context.Context, f *flag.FlagSet) error {
    68  	m, err := cmd.FileManager()
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	mk := m.CreateTemporaryFile
    74  	if cmd.dir {
    75  		mk = m.CreateTemporaryDirectory
    76  	}
    77  
    78  	name, err := mk(ctx, cmd.Auth(), cmd.prefix, cmd.suffix, cmd.path)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	fmt.Println(name)
    84  
    85  	return nil
    86  }