github.com/vmware/govmomi@v0.43.0/govc/vm/dataset/info.go (about)

     1  /*
     2  Copyright (c) 2023-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 dataset
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"flag"
    23  	"fmt"
    24  	"io"
    25  	"text/tabwriter"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/flags"
    29  	"github.com/vmware/govmomi/vapi/vm/dataset"
    30  )
    31  
    32  type info struct {
    33  	*flags.VirtualMachineFlag
    34  	*flags.OutputFlag
    35  }
    36  
    37  func init() {
    38  	cli.Register("vm.dataset.info", &info{})
    39  }
    40  
    41  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    43  	cmd.OutputFlag.Register(ctx, f)
    44  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    45  	cmd.VirtualMachineFlag.Register(ctx, f)
    46  }
    47  
    48  func (cmd *info) Process(ctx context.Context) error {
    49  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    50  		return err
    51  	}
    52  	return cmd.OutputFlag.Process(ctx)
    53  }
    54  
    55  func (cmd *info) Usage() string {
    56  	return "NAME"
    57  }
    58  
    59  func (cmd *info) Description() string {
    60  	return `Display data set information.
    61  
    62  Examples:
    63    govc vm.dataset.info -vm $vm
    64    govc vm.dataset.info -vm $vm com.example.project2`
    65  }
    66  
    67  type infoResult []*dataset.Info
    68  
    69  func (r infoResult) Write(w io.Writer) error {
    70  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    71  
    72  	for _, item := range r {
    73  		fmt.Fprintf(tw, "Name:\t%s\n", item.Name)
    74  		fmt.Fprintf(tw, "  Description:\t%s\n", item.Description)
    75  		fmt.Fprintf(tw, "  Host:\t%s\n", item.Host)
    76  		fmt.Fprintf(tw, "  Guest:\t%s\n", item.Guest)
    77  		fmt.Fprintf(tw, "  Used: \t%d\n", item.Used)
    78  		fmt.Fprintf(tw, "  OmitFromSnapshotAndClone: \t%t\n", item.OmitFromSnapshotAndClone)
    79  	}
    80  
    81  	return tw.Flush()
    82  }
    83  
    84  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    85  	if f.NArg() > 1 {
    86  		return errors.New("please specify at most one data set")
    87  	}
    88  
    89  	vm, err := cmd.VirtualMachineFlag.VirtualMachine()
    90  	if err != nil {
    91  		return err
    92  	}
    93  	if vm == nil {
    94  		return flag.ErrHelp
    95  	}
    96  	vmId := vm.Reference().Value
    97  
    98  	c, err := cmd.RestClient()
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	mgr := dataset.NewManager(c)
   104  
   105  	// Discover the relevant data set id(s)
   106  	var ids []string
   107  	if f.NArg() == 1 {
   108  		// single data set
   109  		id, err := FindDataSetId(ctx, mgr, vmId, f.Arg(0))
   110  		if err != nil {
   111  			return err
   112  		}
   113  		ids = []string{id}
   114  	} else {
   115  		// all data sets
   116  		l, err := mgr.ListDataSets(ctx, vmId)
   117  		if err != nil {
   118  			return err
   119  		}
   120  		for _, summary := range l {
   121  			ids = append(ids, summary.DataSet)
   122  		}
   123  	}
   124  
   125  	// Fetch the information for each data set id
   126  	var res []*dataset.Info
   127  	for _, id := range ids {
   128  		inf, err := mgr.GetDataSet(ctx, vmId, id)
   129  		if err != nil {
   130  			return err
   131  		}
   132  		res = append(res, inf)
   133  	}
   134  
   135  	return cmd.WriteResult(infoResult(res))
   136  }