github.com/vmware/govmomi@v0.37.2/govc/namespace/vmclass/info.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 vmclass
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"text/tabwriter"
    25  
    26  	"github.com/vmware/govmomi/units"
    27  	"github.com/vmware/govmomi/vapi/namespace"
    28  
    29  	"github.com/vmware/govmomi/govc/cli"
    30  	"github.com/vmware/govmomi/govc/flags"
    31  )
    32  
    33  type infoResult namespace.VirtualMachineClassInfo
    34  
    35  func (r infoResult) Write(w io.Writer) error {
    36  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    37  
    38  	fmt.Fprintf(tw, "ID:\t%s\n", r.Id)
    39  	fmt.Fprintf(tw, "CPUs:\t%d\n", r.CpuCount)
    40  	fmt.Fprintf(tw, "Memory:\t%s\n", units.ByteSize(r.MemoryMb*1024*1024))
    41  
    42  	return tw.Flush()
    43  }
    44  
    45  type info struct {
    46  	*flags.ClientFlag
    47  	*flags.OutputFlag
    48  }
    49  
    50  func init() {
    51  	cli.Register("namespace.vmclass.info", &info{})
    52  }
    53  
    54  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    55  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    56  	cmd.ClientFlag.Register(ctx, f)
    57  
    58  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    59  	cmd.OutputFlag.Register(ctx, f)
    60  }
    61  
    62  func (cmd *info) Process(ctx context.Context) error {
    63  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    64  		return err
    65  	}
    66  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    67  		return err
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func (cmd *info) Usage() string {
    74  	return "NAME"
    75  }
    76  
    77  func (cmd *info) Description() string {
    78  	return `Displays the details of a virtual machine class.
    79  
    80  Examples:
    81    govc namespace.vmclass.info test-class`
    82  }
    83  
    84  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    85  	if f.NArg() != 1 {
    86  		return flag.ErrHelp
    87  	}
    88  
    89  	rc, err := cmd.RestClient()
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	nm := namespace.NewManager(rc)
    95  
    96  	d, err := nm.GetVmClass(ctx, f.Arg(0))
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	return cmd.WriteResult(infoResult(d))
   102  }