github.com/vmware/govmomi@v0.51.0/cli/library/update.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 library
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/vapi/library"
    15  )
    16  
    17  type update struct {
    18  	*flags.ClientFlag
    19  
    20  	name string
    21  	desc *string
    22  }
    23  
    24  func init() {
    25  	cli.Register("library.update", &update{})
    26  }
    27  
    28  func (cmd *update) Register(ctx context.Context, f *flag.FlagSet) {
    29  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    30  	cmd.ClientFlag.Register(ctx, f)
    31  
    32  	f.StringVar(&cmd.name, "n", "", "Library or item name")
    33  	f.Var(flags.NewOptionalString(&cmd.desc), "d", "Library or item description")
    34  }
    35  
    36  func (cmd *update) Usage() string {
    37  	return "PATH"
    38  }
    39  
    40  func (cmd *update) Description() string {
    41  	return `Update library or item PATH.
    42  
    43  Examples:
    44    govc library.update -d "new library description" -n "new-name" my-library
    45    govc library.update -d "new item description" -n "new-item-name" my-library/my-item`
    46  }
    47  
    48  func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
    49  	if f.NArg() != 1 {
    50  		return flag.ErrHelp
    51  	}
    52  
    53  	c, err := cmd.RestClient()
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	m := library.NewManager(c)
    59  
    60  	res, err := flags.ContentLibraryResult(ctx, c, "", f.Arg(0))
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	switch t := res.GetResult().(type) {
    66  	case library.Library:
    67  		lib := &library.Library{
    68  			ID:   t.ID,
    69  			Name: cmd.name,
    70  		}
    71  		if cmd.desc != nil {
    72  			lib.Description = cmd.desc
    73  		}
    74  		t.Patch(lib)
    75  		return m.UpdateLibrary(ctx, &t)
    76  	case library.Item:
    77  		item := &library.Item{
    78  			ID:   t.ID,
    79  			Name: cmd.name,
    80  		}
    81  		if cmd.desc != nil {
    82  			item.Description = cmd.desc
    83  		}
    84  		t.Patch(item)
    85  		return m.UpdateLibraryItem(ctx, item)
    86  	default:
    87  		return fmt.Errorf("%q is a %T", f.Arg(0), t)
    88  	}
    89  }