github.com/vmware/govmomi@v0.51.0/cli/namespace/service/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 service
     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.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.
    47  
    48  Examples:
    49    govc namespace.service.info my-supervisor-service
    50    govc namespace.service.info -json my-supervisor-service | jq .`
    51  }
    52  
    53  type infoWriter struct {
    54  	cmd     *info
    55  	Service namespace.SupervisorServiceInfo `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  	fmt.Fprintf(tw, "\n")
    65  
    66  	return tw.Flush()
    67  }
    68  
    69  func (r *infoWriter) MarshalJSON() ([]byte, error) {
    70  	return json.Marshal(r.Service)
    71  }
    72  
    73  func (r *infoWriter) Dump() any {
    74  	return r.Service
    75  }
    76  
    77  func (cmd *info) Usage() string {
    78  	return "NAME"
    79  }
    80  
    81  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    82  	service := f.Args()
    83  	if len(service) != 1 {
    84  		return flag.ErrHelp
    85  	}
    86  
    87  	c, err := cmd.RestClient()
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	m := namespace.NewManager(c)
    93  	supervisorservice, err := m.GetSupervisorService(ctx, service[0])
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	return cmd.WriteResult(&infoWriter{cmd, supervisorservice})
    99  }