github.com/vmware/govmomi@v0.51.0/cli/namespace/service/flag.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package service
     6  
     7  import (
     8  	"context"
     9  	"encoding/base64"
    10  	"flag"
    11  	"fmt"
    12  	"os"
    13  
    14  	"github.com/vmware/govmomi/vapi/namespace"
    15  )
    16  
    17  // Common processing of flags for service and service versions creation/update commands.
    18  type ServiceVersionFlag struct {
    19  	SpecType        string
    20  	TrustedProvider bool
    21  	AcceptEULA      bool
    22  	content         string
    23  }
    24  
    25  func (svf *ServiceVersionFlag) Register(ctx context.Context, f *flag.FlagSet) {
    26  	f.StringVar(&svf.SpecType, "spec-type", "carvel", "Type of Spec: vsphere (deprecated) or carvel")
    27  	f.BoolVar(&svf.TrustedProvider, "trusted", false, "Define if this is a trusted provider (only applicable for vSphere spec type)")
    28  	f.BoolVar(&svf.AcceptEULA, "accept-eula", false, "Auto accept EULA (only required for vSphere spec type)")
    29  }
    30  
    31  func (svf *ServiceVersionFlag) Process(ctx context.Context) error {
    32  	if svf.SpecType != "vsphere" && svf.SpecType != "carvel" {
    33  		return fmt.Errorf("Invalid type: '%v', only 'vsphere' and 'carvel' specs are supported", svf.SpecType)
    34  	}
    35  	return nil
    36  }
    37  
    38  // SupervisorServiceVersionSpec returns a spec for a supervisor service version definition
    39  func (svf *ServiceVersionFlag) SupervisorServiceVersionSpec(manifestFile string) (namespace.SupervisorService, error) {
    40  	service := namespace.SupervisorService{}
    41  	manifest, err := os.ReadFile(manifestFile)
    42  	if err != nil {
    43  		return service, fmt.Errorf("failed to read manifest file: %s", err)
    44  	}
    45  
    46  	content := base64.StdEncoding.EncodeToString(manifest)
    47  	if svf.SpecType == "carvel" {
    48  		service.CarvelService = &namespace.SupervisorServicesCarvelSpec{
    49  			VersionSpec: namespace.CarvelVersionCreateSpec{
    50  				Content: content,
    51  			},
    52  		}
    53  	} else {
    54  		service.VsphereService = &namespace.SupervisorServicesVSphereSpec{
    55  			VersionSpec: namespace.SupervisorServicesVSphereVersionCreateSpec{
    56  				Content:         content,
    57  				AcceptEula:      svf.AcceptEULA,
    58  				TrustedProvider: svf.TrustedProvider,
    59  			},
    60  		}
    61  	}
    62  	return service, nil
    63  }