github.com/vmware/govmomi@v0.43.0/govc/importx/spec.go (about)

     1  /*
     2  Copyright (c) 2015-2024 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 importx
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"path"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/ovf/importer"
    29  )
    30  
    31  type spec struct {
    32  	*flags.ClientFlag
    33  	*flags.OutputFlag
    34  
    35  	Archive importer.Archive
    36  
    37  	hidden bool
    38  }
    39  
    40  func init() {
    41  	cli.Register("import.spec", &spec{})
    42  }
    43  
    44  func (cmd *spec) Register(ctx context.Context, f *flag.FlagSet) {
    45  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    46  	cmd.ClientFlag.Register(ctx, f)
    47  
    48  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    49  	cmd.OutputFlag.Register(ctx, f)
    50  
    51  	f.BoolVar(&cmd.hidden, "hidden", false, "Enable hidden properties")
    52  }
    53  
    54  func (cmd *spec) Process(ctx context.Context) error {
    55  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    56  		return err
    57  	}
    58  	return cmd.OutputFlag.Process(ctx)
    59  }
    60  
    61  func (cmd *spec) Usage() string {
    62  	return "PATH_TO_OVF_OR_OVA"
    63  }
    64  
    65  func (cmd *spec) Run(ctx context.Context, f *flag.FlagSet) error {
    66  	fpath := ""
    67  	args := f.Args()
    68  	if len(args) == 1 {
    69  		fpath = f.Arg(0)
    70  	}
    71  
    72  	if len(fpath) > 0 {
    73  		switch path.Ext(fpath) {
    74  		case ".ovf":
    75  			cmd.Archive = &importer.FileArchive{Path: fpath}
    76  		case "", ".ova":
    77  			cmd.Archive = &importer.TapeArchive{Path: fpath}
    78  			fpath = "*.ovf"
    79  		default:
    80  			return fmt.Errorf("invalid file extension %s", path.Ext(fpath))
    81  		}
    82  
    83  		if importer.IsRemotePath(f.Arg(0)) {
    84  			client, err := cmd.Client()
    85  			if err != nil {
    86  				return err
    87  			}
    88  			switch archive := cmd.Archive.(type) {
    89  			case *importer.FileArchive:
    90  				archive.Client = client
    91  			case *importer.TapeArchive:
    92  				archive.Client = client
    93  			}
    94  		}
    95  	}
    96  
    97  	env, err := importer.Spec(fpath, cmd.Archive, cmd.hidden, cmd.Verbose())
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	if !cmd.All() {
   103  		cmd.JSON = true
   104  	}
   105  	return cmd.WriteResult(&specResult{env})
   106  }
   107  
   108  type specResult struct {
   109  	*importer.Options
   110  }
   111  
   112  func (*specResult) Write(w io.Writer) error {
   113  	return nil
   114  }