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

     1  /*
     2  Copyright (c) 2016-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 folder
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"strings"
    25  	"text/tabwriter"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/flags"
    29  	"github.com/vmware/govmomi/object"
    30  	"github.com/vmware/govmomi/property"
    31  	"github.com/vmware/govmomi/vim25/mo"
    32  	"github.com/vmware/govmomi/vim25/types"
    33  )
    34  
    35  type info struct {
    36  	*flags.ClientFlag
    37  	*flags.OutputFlag
    38  	*flags.DatacenterFlag
    39  }
    40  
    41  func init() {
    42  	cli.Register("folder.info", &info{})
    43  }
    44  
    45  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    46  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    47  	cmd.ClientFlag.Register(ctx, f)
    48  
    49  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    50  	cmd.OutputFlag.Register(ctx, f)
    51  
    52  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    53  	cmd.DatacenterFlag.Register(ctx, f)
    54  }
    55  
    56  func (cmd *info) Process(ctx context.Context) error {
    57  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    58  		return err
    59  	}
    60  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    61  		return err
    62  	}
    63  	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
    64  		return err
    65  	}
    66  	return nil
    67  }
    68  
    69  func (cmd *info) Usage() string {
    70  	return "[PATH]..."
    71  }
    72  
    73  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    74  	c, err := cmd.Client()
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	finder, err := cmd.Finder()
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	args := f.Args()
    85  	if len(args) == 0 {
    86  		args = []string{"/"}
    87  	}
    88  
    89  	var props []string
    90  	var res infoResult
    91  
    92  	if !cmd.OutputFlag.All() {
    93  		props = []string{
    94  			"name",
    95  			"childEntity",
    96  			"childType",
    97  		}
    98  	}
    99  
   100  	for _, arg := range args {
   101  		object, err := finder.FolderList(ctx, arg)
   102  		if err != nil {
   103  			return err
   104  		}
   105  		res.objects = append(res.objects, object...)
   106  	}
   107  
   108  	if len(res.objects) != 0 {
   109  		refs := make([]types.ManagedObjectReference, 0, len(res.objects))
   110  		for _, o := range res.objects {
   111  			refs = append(refs, o.Reference())
   112  		}
   113  
   114  		pc := property.DefaultCollector(c)
   115  		err = pc.Retrieve(ctx, refs, props, &res.Folders)
   116  		if err != nil {
   117  			return err
   118  		}
   119  	}
   120  
   121  	return cmd.WriteResult(&res)
   122  }
   123  
   124  type infoResult struct {
   125  	Folders []mo.Folder `json:"folders"`
   126  	objects []*object.Folder
   127  }
   128  
   129  func (r *infoResult) Write(w io.Writer) error {
   130  	// Maintain order via r.objects as Property collector does not always return results in order.
   131  	objects := make(map[types.ManagedObjectReference]mo.Folder, len(r.Folders))
   132  	for _, o := range r.Folders {
   133  		objects[o.Reference()] = o
   134  	}
   135  
   136  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
   137  
   138  	for _, o := range r.objects {
   139  		info := objects[o.Reference()]
   140  		fmt.Fprintf(tw, "Name:\t%s\n", info.Name)
   141  		fmt.Fprintf(tw, "  Path:\t%s\n", o.InventoryPath)
   142  		fmt.Fprintf(tw, "  Types:\t%v\n", strings.Join(info.ChildType, ","))
   143  		fmt.Fprintf(tw, "  Children:\t%d\n", len(info.ChildEntity))
   144  	}
   145  
   146  	return tw.Flush()
   147  }