github.com/vmware/govmomi@v0.37.2/govc/datastore/download.go (about) 1 /* 2 Copyright (c) 2014-2015 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 datastore 18 19 import ( 20 "context" 21 "errors" 22 "flag" 23 "fmt" 24 "io" 25 "os" 26 27 "github.com/vmware/govmomi/govc/cli" 28 "github.com/vmware/govmomi/govc/flags" 29 "github.com/vmware/govmomi/vim25/soap" 30 ) 31 32 type download struct { 33 *flags.DatastoreFlag 34 *flags.HostSystemFlag 35 } 36 37 func init() { 38 cli.Register("datastore.download", &download{}) 39 } 40 41 func (cmd *download) Register(ctx context.Context, f *flag.FlagSet) { 42 cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx) 43 cmd.DatastoreFlag.Register(ctx, f) 44 45 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 46 cmd.HostSystemFlag.Register(ctx, f) 47 } 48 49 func (cmd *download) Process(ctx context.Context) error { 50 if err := cmd.DatastoreFlag.Process(ctx); err != nil { 51 return err 52 } 53 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 54 return err 55 } 56 return nil 57 } 58 59 func (cmd *download) Usage() string { 60 return "SOURCE DEST" 61 } 62 63 func (cmd *download) Description() string { 64 return `Copy SOURCE from DS to DEST on the local system. 65 66 If DEST name is "-", source is written to stdout. 67 68 Examples: 69 govc datastore.download vm-name/vmware.log ./local.log 70 govc datastore.download vm-name/vmware.log - | grep -i error` 71 } 72 73 func (cmd *download) Run(ctx context.Context, f *flag.FlagSet) error { 74 args := f.Args() 75 if len(args) != 2 { 76 return errors.New("invalid arguments") 77 } 78 79 ds, err := cmd.Datastore() 80 if err != nil { 81 return err 82 } 83 84 h, err := cmd.HostSystemIfSpecified() 85 if err != nil { 86 return err 87 } 88 89 var via string 90 91 if h != nil { 92 via = fmt.Sprintf(" via %s", h.InventoryPath) 93 ctx = ds.HostContext(ctx, h) 94 } 95 96 p := soap.DefaultDownload 97 98 src := args[0] 99 dst := args[1] 100 101 if dst == "-" { 102 f, _, err := ds.Download(ctx, src, &p) 103 if err != nil { 104 return err 105 } 106 _, err = io.Copy(os.Stdout, f) 107 return err 108 } 109 110 if cmd.DatastoreFlag.OutputFlag.TTY { 111 logger := cmd.DatastoreFlag.ProgressLogger(fmt.Sprintf("Downloading%s... ", via)) 112 p.Progress = logger 113 defer logger.Wait() 114 } 115 116 return ds.DownloadFile(ctx, src, dst, &p) 117 }