github.com/vmware/govmomi@v0.37.2/govc/cluster/draft/ls.go (about)

     1  /*
     2  Copyright (c) 2024-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 draft
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"io"
    23  	"strings"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/govc/flags"
    27  	"github.com/vmware/govmomi/vapi/esx/settings/clusters"
    28  )
    29  
    30  type lsResult map[string]clusters.SettingsClustersSoftwareDraftsMetadata
    31  
    32  func (r lsResult) Write(w io.Writer) error {
    33  	return nil
    34  }
    35  
    36  type ls struct {
    37  	*flags.ClientFlag
    38  	*flags.OutputFlag
    39  
    40  	clusterId string
    41  	owners    string
    42  }
    43  
    44  func init() {
    45  	cli.Register("cluster.draft.ls", &ls{})
    46  }
    47  
    48  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    49  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    50  	cmd.ClientFlag.Register(ctx, f)
    51  
    52  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    53  
    54  	f.StringVar(&cmd.clusterId, "cluster-id", "", "The identifier of the cluster.")
    55  	f.StringVar(&cmd.owners, "owners", "", "A comma-separated list of owners to filter by.")
    56  }
    57  
    58  func (cmd *ls) Process(ctx context.Context) error {
    59  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    60  		return err
    61  	}
    62  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    63  		return err
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func (cmd *ls) Usage() string {
    70  	return "CLUSTER"
    71  }
    72  
    73  func (cmd *ls) Description() string {
    74  	return `Displays the list of software drafts.
    75  
    76  Examples:
    77    govc cluster.draft.ls -cluster-id=domain-c21
    78    govc cluster.draft.ls -cluster-id=domain-c21 -owners=stoyan1,stoyan2`
    79  }
    80  
    81  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    82  	rc, err := cmd.RestClient()
    83  
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	dm := clusters.NewManager(rc)
    89  
    90  	var owners *[]string
    91  	if cmd.owners != "" {
    92  		o := strings.Split(cmd.owners, ",")
    93  		owners = &o
    94  	}
    95  
    96  	if d, err := dm.ListSoftwareDrafts(cmd.clusterId, owners); err != nil {
    97  		return err
    98  	} else {
    99  		if !cmd.All() {
   100  			cmd.JSON = true
   101  		}
   102  		return cmd.WriteResult(lsResult(d))
   103  	}
   104  }