github.com/vmware/govmomi@v0.37.2/govc/about/command.go (about)

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