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

     1  /*
     2  Copyright (c) 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  	"bytes"
    21  	"context"
    22  	"flag"
    23  	"time"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/vim25/soap"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type touch struct {
    31  	*GuestFlag
    32  
    33  	nocreate bool
    34  	atime    bool
    35  	date     string
    36  }
    37  
    38  func init() {
    39  	cli.Register("guest.touch", &touch{})
    40  }
    41  
    42  func (cmd *touch) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.GuestFlag, ctx = newGuestFlag(ctx)
    44  	cmd.GuestFlag.Register(ctx, f)
    45  
    46  	f.BoolVar(&cmd.atime, "a", false, "Change only the access time")
    47  	f.BoolVar(&cmd.nocreate, "c", false, "Do not create any files")
    48  	f.StringVar(&cmd.date, "d", "", "Use DATE instead of current time")
    49  }
    50  
    51  func (cmd *touch) Process(ctx context.Context) error {
    52  	if err := cmd.GuestFlag.Process(ctx); err != nil {
    53  		return err
    54  	}
    55  	return nil
    56  }
    57  
    58  func (cmd *touch) Usage() string {
    59  	return "FILE"
    60  }
    61  
    62  func (cmd *touch) Description() string {
    63  	return `Change FILE times on VM.
    64  
    65  Examples:
    66    govc guest.touch -vm $name /var/log/foo.log
    67    govc guest.touch -vm $name -d "$(date -d '1 day ago')" /var/log/foo.log`
    68  }
    69  
    70  func (cmd *touch) Run(ctx context.Context, f *flag.FlagSet) error {
    71  	if f.NArg() != 1 {
    72  		return flag.ErrHelp
    73  	}
    74  
    75  	m, err := cmd.FileManager()
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	name := f.Arg(0)
    81  
    82  	var attr types.GuestFileAttributes
    83  	now := time.Now()
    84  
    85  	if cmd.date != "" {
    86  		now, err = time.Parse(time.UnixDate, cmd.date)
    87  		if err != nil {
    88  			return err
    89  		}
    90  	}
    91  
    92  	if cmd.atime {
    93  		attr.AccessTime = &now
    94  	} else {
    95  		attr.ModificationTime = &now
    96  	}
    97  
    98  	err = m.ChangeFileAttributes(ctx, cmd.Auth(), name, &attr)
    99  	if err != nil && !cmd.nocreate && soap.IsSoapFault(err) {
   100  		fault := soap.ToSoapFault(err)
   101  		if _, ok := fault.VimFault().(types.FileNotFound); ok {
   102  			// create a new empty file
   103  			url, cerr := m.InitiateFileTransferToGuest(ctx, cmd.Auth(), name, &attr, 0, false)
   104  			if cerr != nil {
   105  				return cerr
   106  			}
   107  
   108  			u, cerr := cmd.ParseURL(url)
   109  			if cerr != nil {
   110  				return cerr
   111  			}
   112  
   113  			c, cerr := cmd.Client()
   114  			if cerr != nil {
   115  				return cerr
   116  			}
   117  
   118  			err = c.Client.Upload(ctx, new(bytes.Buffer), u, &soap.DefaultUpload)
   119  			if err == nil && cmd.date != "" {
   120  				err = m.ChangeFileAttributes(ctx, cmd.Auth(), name, &attr)
   121  			}
   122  		}
   123  	}
   124  
   125  	return err
   126  }