github.com/vmware/govmomi@v0.37.1/govc/vm/guest/upload.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 "path/filepath" 25 26 "github.com/vmware/govmomi/govc/cli" 27 "github.com/vmware/govmomi/vim25/soap" 28 ) 29 30 type upload struct { 31 *GuestFlag 32 *FileAttrFlag 33 34 overwrite bool 35 } 36 37 func init() { 38 cli.Register("guest.upload", &upload{}) 39 } 40 41 func (cmd *upload) Register(ctx context.Context, f *flag.FlagSet) { 42 cmd.GuestFlag, ctx = newGuestFlag(ctx) 43 cmd.GuestFlag.Register(ctx, f) 44 cmd.FileAttrFlag, ctx = newFileAttrFlag(ctx) 45 cmd.FileAttrFlag.Register(ctx, f) 46 47 f.BoolVar(&cmd.overwrite, "f", false, "If set, the guest destination file is clobbered") 48 } 49 50 func (cmd *upload) Usage() string { 51 return "SOURCE DEST" 52 } 53 54 func (cmd *upload) Description() string { 55 return `Copy SOURCE from the local system to DEST in the guest VM. 56 57 If SOURCE name is "-", read source from stdin. 58 59 Examples: 60 govc guest.upload -l user:pass -vm=my-vm ~/.ssh/id_rsa.pub /home/$USER/.ssh/authorized_keys 61 cowsay "have a great day" | govc guest.upload -l user:pass -vm=my-vm - /etc/motd 62 tar -cf- foo/ | govc guest.run -d - tar -C /tmp -xf- # upload a directory` 63 } 64 65 func (cmd *upload) Process(ctx context.Context) error { 66 if err := cmd.GuestFlag.Process(ctx); err != nil { 67 return err 68 } 69 if err := cmd.FileAttrFlag.Process(ctx); err != nil { 70 return err 71 } 72 return nil 73 } 74 75 func (cmd *upload) Run(ctx context.Context, f *flag.FlagSet) error { 76 if f.NArg() != 2 { 77 return flag.ErrHelp 78 } 79 80 c, err := cmd.Toolbox(ctx) 81 if err != nil { 82 return err 83 } 84 85 src := f.Arg(0) 86 dst := f.Arg(1) 87 88 p := soap.DefaultUpload 89 90 var r io.Reader = os.Stdin 91 92 if src != "-" { 93 f, err := os.Open(filepath.Clean(src)) 94 if err != nil { 95 return err 96 } 97 defer f.Close() 98 99 r = f 100 101 if cmd.OutputFlag.TTY { 102 logger := cmd.ProgressLogger("Uploading... ") 103 p.Progress = logger 104 defer logger.Wait() 105 } 106 } 107 108 return c.Upload(ctx, r, dst, p, cmd.Attr(), cmd.overwrite) 109 }