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

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