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- */
1+ // © Broadcom. All Rights Reserved.
2+ // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+ // SPDX-License-Identifier: Apache-2.0
164
175package datastore
186
@@ -23,10 +11,14 @@ import (
2311 "fmt"
2412 "io"
2513 "os"
14+ "path"
2615
2716 "github.com/vmware/govmomi/cli"
2817 "github.com/vmware/govmomi/cli/flags"
18+ "github.com/vmware/govmomi/object"
19+ "github.com/vmware/govmomi/ovf"
2920 "github.com/vmware/govmomi/vim25/soap"
21+ "github.com/vmware/govmomi/vmdk"
3022)
3123
3224type download struct {
@@ -67,7 +59,10 @@ If DEST name is "-", source is written to stdout.
6759
6860Examples:
6961 govc datastore.download vm-name/vmware.log ./local.log
70- govc datastore.download vm-name/vmware.log - | grep -i error`
62+ govc datastore.download vm-name/vmware.log - | grep -i error
63+ govc datastore.download -json vm-name/vm-name.vmdk - | jq .ddb
64+ ovf=$(govc library.info -l -L vmservice/photon-5.0/*.ovf)
65+ govc datastore.download -json "$ovf" - | jq -r .diskSection.disk[].capacity`
7166}
7267
7368func (cmd * download ) Run (ctx context.Context , f * flag.FlagSet ) error {
@@ -76,6 +71,16 @@ func (cmd *download) Run(ctx context.Context, f *flag.FlagSet) error {
7671 return errors .New ("invalid arguments" )
7772 }
7873
74+ src := args [0 ]
75+ dst := args [1 ]
76+
77+ var dp object.DatastorePath
78+ if dp .FromString (src ) {
79+ // e.g. `govc library.info -l -L ...`
80+ cmd .DatastoreFlag .Name = dp .Datastore
81+ src = dp .Path
82+ }
83+
7984 ds , err := cmd .Datastore ()
8085 if err != nil {
8186 return err
@@ -95,14 +100,29 @@ func (cmd *download) Run(ctx context.Context, f *flag.FlagSet) error {
95100
96101 p := soap .DefaultDownload
97102
98- src := args [0 ]
99- dst := args [1 ]
100-
101103 if dst == "-" {
102104 f , _ , err := ds .Download (ctx , src , & p )
103105 if err != nil {
104106 return err
105107 }
108+
109+ if cmd .DatastoreFlag .All () {
110+ switch path .Ext (src ) {
111+ case ".vmdk" :
112+ data , err := vmdk .ParseDescriptor (f )
113+ if err != nil {
114+ return err
115+ }
116+ return cmd .DatastoreFlag .WriteResult (data )
117+ case ".ovf" :
118+ data , err := ovf .Unmarshal (f )
119+ if err != nil {
120+ return err
121+ }
122+ return cmd .DatastoreFlag .WriteResult (data )
123+ }
124+ }
125+
106126 _ , err = io .Copy (os .Stdout , f )
107127 return err
108128 }
0 commit comments