github.com/vmware/govmomi@v0.51.0/cli/about/command.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 about
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"text/tabwriter"
    13  
    14  	"github.com/vmware/govmomi/cli"
    15  	"github.com/vmware/govmomi/cli/flags"
    16  	"github.com/vmware/govmomi/vim25/soap"
    17  	"github.com/vmware/govmomi/vim25/types"
    18  )
    19  
    20  type about struct {
    21  	*flags.ClientFlag
    22  	*flags.OutputFlag
    23  
    24  	Long bool
    25  	c    bool
    26  }
    27  
    28  func init() {
    29  	cli.Register("about", &about{})
    30  }
    31  
    32  func (cmd *about) Register(ctx context.Context, f *flag.FlagSet) {
    33  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    34  	cmd.ClientFlag.Register(ctx, f)
    35  
    36  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    37  	cmd.OutputFlag.Register(ctx, f)
    38  
    39  	f.BoolVar(&cmd.Long, "l", false, "Include service content")
    40  	f.BoolVar(&cmd.c, "c", false, "Include client info")
    41  }
    42  
    43  func (cmd *about) Description() string {
    44  	return `Display About info for HOST.
    45  
    46  System information including the name, type, version, and build number.
    47  
    48  Examples:
    49    govc about
    50    govc about -json | jq -r .about.productLineId`
    51  }
    52  
    53  func (cmd *about) Process(ctx context.Context) error {
    54  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    55  		return err
    56  	}
    57  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    58  		return err
    59  	}
    60  	return nil
    61  }
    62  
    63  func (cmd *about) Run(ctx context.Context, f *flag.FlagSet) error {
    64  	c, err := cmd.Client()
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	res := infoResult{
    70  		a: &c.ServiceContent.About,
    71  	}
    72  
    73  	if cmd.Long {
    74  		res.Content = &c.ServiceContent
    75  	} else {
    76  		res.About = res.a
    77  	}
    78  
    79  	if cmd.c {
    80  		res.Client = c.Client
    81  	}
    82  
    83  	return cmd.WriteResult(&res)
    84  }
    85  
    86  type infoResult struct {
    87  	Content *types.ServiceContent `json:"content,omitempty"`
    88  	About   *types.AboutInfo      `json:"about,omitempty"`
    89  	Client  *soap.Client          `json:"client,omitempty"`
    90  	a       *types.AboutInfo
    91  }
    92  
    93  func (r *infoResult) Write(w io.Writer) error {
    94  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    95  	fmt.Fprintf(tw, "FullName:\t%s\n", r.a.FullName)
    96  	fmt.Fprintf(tw, "Name:\t%s\n", r.a.Name)
    97  	fmt.Fprintf(tw, "Vendor:\t%s\n", r.a.Vendor)
    98  	fmt.Fprintf(tw, "Version:\t%s\n", r.a.Version)
    99  	fmt.Fprintf(tw, "Build:\t%s\n", r.a.Build)
   100  	fmt.Fprintf(tw, "OS type:\t%s\n", r.a.OsType)
   101  	fmt.Fprintf(tw, "API type:\t%s\n", r.a.ApiType)
   102  	fmt.Fprintf(tw, "API version:\t%s\n", r.a.ApiVersion)
   103  	fmt.Fprintf(tw, "Product ID:\t%s\n", r.a.ProductLineId)
   104  	fmt.Fprintf(tw, "UUID:\t%s\n", r.a.InstanceUuid)
   105  	return tw.Flush()
   106  }
   107  
   108  func (r *infoResult) Dump() any {
   109  	if r.Content != nil {
   110  		return r.Content
   111  	}
   112  	return r.About
   113  }