1/* 2Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package datastore 18 19import ( 20 "context" 21 "errors" 22 "flag" 23 "os" 24 25 "github.com/vmware/govmomi/govc/cli" 26 "github.com/vmware/govmomi/govc/flags" 27 "github.com/vmware/govmomi/vim25/soap" 28) 29 30type upload struct { 31 *flags.OutputFlag 32 *flags.DatastoreFlag 33} 34 35func init() { 36 cli.Register("datastore.upload", &upload{}) 37} 38 39func (cmd *upload) Register(ctx context.Context, f *flag.FlagSet) { 40 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 41 cmd.OutputFlag.Register(ctx, f) 42 43 cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx) 44 cmd.DatastoreFlag.Register(ctx, f) 45} 46 47func (cmd *upload) Process(ctx context.Context) error { 48 if err := cmd.OutputFlag.Process(ctx); err != nil { 49 return err 50 } 51 if err := cmd.DatastoreFlag.Process(ctx); err != nil { 52 return err 53 } 54 return nil 55} 56 57func (cmd *upload) Usage() string { 58 return "SOURCE DEST" 59} 60 61func (cmd *upload) Description() string { 62 return `Copy SOURCE from the local system to DEST on DS. 63 64If SOURCE name is "-", read source from stdin. 65 66Examples: 67 govc datastore.upload -ds datastore1 ./config.iso vm-name/config.iso 68 genisoimage ... | govc datastore.upload -ds datastore1 - vm-name/config.iso` 69} 70 71func (cmd *upload) Run(ctx context.Context, f *flag.FlagSet) error { 72 args := f.Args() 73 if len(args) != 2 { 74 return errors.New("invalid arguments") 75 } 76 77 ds, err := cmd.Datastore() 78 if err != nil { 79 return err 80 } 81 82 p := soap.DefaultUpload 83 84 src := args[0] 85 dst := args[1] 86 87 if src == "-" { 88 return ds.Upload(ctx, os.Stdin, dst, &p) 89 } 90 91 if cmd.OutputFlag.TTY { 92 logger := cmd.ProgressLogger("Uploading... ") 93 p.Progress = logger 94 defer logger.Wait() 95 } 96 97 return ds.UploadFile(ctx, src, dst, &p) 98} 99