github.com/vmware/govmomi@v0.51.0/cli/namespace/service/version/info.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 version
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"flag"
    11  	"fmt"
    12  	"io"
    13  	"os"
    14  	"text/tabwriter"
    15  
    16  	"github.com/vmware/govmomi/cli"
    17  	"github.com/vmware/govmomi/cli/flags"
    18  	"github.com/vmware/govmomi/vapi/namespace"
    19  )
    20  
    21  type info struct {
    22  	*flags.ClientFlag
    23  	*flags.OutputFlag
    24  }
    25  
    26  func init() {
    27  	cli.Register("namespace.service.version.info", &info{})
    28  }
    29  
    30  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    31  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    32  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    33  	cmd.ClientFlag.Register(ctx, f)
    34  	cmd.OutputFlag.Register(ctx, f)
    35  
    36  }
    37  
    38  func (cmd *info) Process(ctx context.Context) error {
    39  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    40  		return err
    41  	}
    42  	return cmd.OutputFlag.Process(ctx)
    43  }
    44  
    45  func (cmd *info) Description() string {
    46  	return `Gets information of a specific vSphere Supervisor Service version.
    47  
    48  Examples:
    49    govc namespace.service.version.info my-supervisor-service 2.0.0
    50    govc namespace.service.version.info -json my-supervisor-service 2.0.0 | jq .`
    51  }
    52  
    53  type infoWriter struct {
    54  	cmd     *info
    55  	Service namespace.SupervisorServiceVersionInfo `json:"service"`
    56  }
    57  
    58  func (r *infoWriter) Write(w io.Writer) error {
    59  	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
    60  
    61  	fmt.Fprintf(tw, "%s", r.Service.Name)
    62  	fmt.Fprintf(tw, "\t%s", r.Service.State)
    63  	fmt.Fprintf(tw, "\t%s", r.Service.Description)
    64  
    65  	fmt.Fprintf(tw, "\n")
    66  
    67  	return tw.Flush()
    68  }
    69  
    70  func (r *infoWriter) MarshalJSON() ([]byte, error) {
    71  	return json.Marshal(r.Service)
    72  }
    73  
    74  func (r *infoWriter) Dump() any {
    75  	return r.Service
    76  }
    77  
    78  func (cmd *info) Usage() string {
    79  	return "NAME VERSION"
    80  }
    81  
    82  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    83  	service := f.Arg(0)
    84  	if len(service) == 0 {
    85  		return flag.ErrHelp
    86  	}
    87  	version := f.Arg(1)
    88  	if len(version) == 0 {
    89  		return flag.ErrHelp
    90  	}
    91  
    92  	c, err := cmd.RestClient()
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	m := namespace.NewManager(c)
    98  	serviceVersion, err := m.GetSupervisorServiceVersion(ctx, service, version)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	return cmd.WriteResult(&infoWriter{cmd, serviceVersion})
   104  }