github.com/vmware/govmomi@v0.43.0/govc/flags/env.go (about)

     1  /*
     2  Copyright (c) 2023-2023 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 flags
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/object"
    24  	"github.com/vmware/govmomi/vim25/mo"
    25  )
    26  
    27  type EnvBrowser struct {
    28  	*ClusterFlag
    29  	*HostSystemFlag
    30  	*VirtualMachineFlag
    31  }
    32  
    33  func (cmd *EnvBrowser) Register(ctx context.Context, f *flag.FlagSet) {
    34  	cmd.ClusterFlag, ctx = NewClusterFlag(ctx)
    35  	cmd.ClusterFlag.Register(ctx, f)
    36  
    37  	cmd.HostSystemFlag, ctx = NewHostSystemFlag(ctx)
    38  	cmd.HostSystemFlag.Register(ctx, f)
    39  
    40  	cmd.VirtualMachineFlag, ctx = NewVirtualMachineFlag(ctx)
    41  	cmd.VirtualMachineFlag.Register(ctx, f)
    42  }
    43  
    44  func (cmd *EnvBrowser) Process(ctx context.Context) error {
    45  	if err := cmd.ClusterFlag.Process(ctx); err != nil {
    46  		return err
    47  	}
    48  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    49  		return err
    50  	}
    51  	return cmd.VirtualMachineFlag.Process(ctx)
    52  }
    53  
    54  func (cmd *EnvBrowser) Browser(ctx context.Context) (*object.EnvironmentBrowser, error) {
    55  	c, err := cmd.VirtualMachineFlag.Client()
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	vm, err := cmd.VirtualMachine()
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	if vm != nil {
    65  		return vm.EnvironmentBrowser(ctx)
    66  	}
    67  
    68  	host, err := cmd.HostSystemIfSpecified()
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	if host != nil {
    74  		var h mo.HostSystem
    75  		err = host.Properties(ctx, host.Reference(), []string{"parent"}, &h)
    76  		if err != nil {
    77  			return nil, err
    78  		}
    79  
    80  		return object.NewComputeResource(c, *h.Parent).EnvironmentBrowser(ctx)
    81  	}
    82  
    83  	finder, ferr := cmd.ClusterFlag.Finder()
    84  	if ferr != nil {
    85  		return nil, ferr
    86  	}
    87  
    88  	cr, ferr := finder.ComputeResourceOrDefault(ctx, cmd.ClusterFlag.Name)
    89  	if ferr != nil {
    90  		return nil, ferr
    91  	}
    92  
    93  	return cr.EnvironmentBrowser(ctx)
    94  }