github.com/vmware/govmomi@v0.43.0/govc/vm/guest/download.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 guest
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"io"
    23  	"os"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/vim25/progress"
    27  )
    28  
    29  type download struct {
    30  	*GuestFlag
    31  
    32  	overwrite bool
    33  }
    34  
    35  func init() {
    36  	cli.Register("guest.download", &download{})
    37  }
    38  
    39  func (cmd *download) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.GuestFlag, ctx = newGuestFlag(ctx)
    41  	cmd.GuestFlag.Register(ctx, f)
    42  
    43  	f.BoolVar(&cmd.overwrite, "f", false, "If set, the local destination file is clobbered")
    44  }
    45  
    46  func (cmd *download) Usage() string {
    47  	return "SOURCE DEST"
    48  }
    49  
    50  func (cmd *download) Description() string {
    51  	return `Copy SOURCE from the guest VM to DEST on the local system.
    52  
    53  If DEST name is "-", source is written to stdout.
    54  
    55  Examples:
    56    govc guest.download -l user:pass -vm=my-vm /var/log/my.log ./local.log
    57    govc guest.download -l user:pass -vm=my-vm /etc/motd -
    58    tar -cf- foo/ | govc guest.run -d - tar -C /tmp -xf-
    59    govc guest.run tar -C /tmp -cf- foo/ | tar -C /tmp -xf- # download directory`
    60  }
    61  
    62  func (cmd *download) Process(ctx context.Context) error {
    63  	if err := cmd.GuestFlag.Process(ctx); err != nil {
    64  		return err
    65  	}
    66  	return nil
    67  }
    68  
    69  func (cmd *download) Run(ctx context.Context, f *flag.FlagSet) error {
    70  	if f.NArg() != 2 {
    71  		return flag.ErrHelp
    72  	}
    73  
    74  	src := f.Arg(0)
    75  	dst := f.Arg(1)
    76  
    77  	_, err := os.Stat(dst)
    78  	if err == nil && !cmd.overwrite {
    79  		return os.ErrExist
    80  	}
    81  
    82  	c, err := cmd.Toolbox(ctx)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	s, n, err := c.Download(ctx, src)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	if dst == "-" {
    93  		_, err = io.Copy(os.Stdout, s)
    94  		return err
    95  	}
    96  
    97  	var p progress.Sinker
    98  
    99  	if cmd.OutputFlag.TTY {
   100  		logger := cmd.ProgressLogger("Downloading... ")
   101  		p = logger
   102  		defer logger.Wait()
   103  	}
   104  
   105  	return c.ProcessManager.Client().WriteFile(ctx, dst, s, n, p, nil)
   106  }