github.com/vmware/govmomi@v0.51.0/cli/library/evict.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 evict struct {
    18  	*flags.ClientFlag
    19  }
    20  
    21  func init() {
    22  	cli.Register("library.evict", &evict{})
    23  }
    24  
    25  func (cmd *evict) Register(ctx context.Context, f *flag.FlagSet) {
    26  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    27  	cmd.ClientFlag.Register(ctx, f)
    28  }
    29  
    30  func (cmd *evict) Usage() string {
    31  	return "LIBRARY NAME | ITEM NAME"
    32  }
    33  
    34  func (cmd *evict) Description() string {
    35  	return `Evict library NAME or item NAME.
    36  
    37  Examples:
    38    govc library.evict subscribed-library
    39    govc library.evict subscribed-library/item`
    40  }
    41  
    42  func (cmd *evict) Run(ctx context.Context, f *flag.FlagSet) error {
    43  	if f.NArg() != 1 {
    44  		return flag.ErrHelp
    45  	}
    46  
    47  	c, err := cmd.RestClient()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	m := library.NewManager(c)
    53  
    54  	res, err := flags.ContentLibraryResult(ctx, c, "", f.Arg(0))
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	switch t := res.GetResult().(type) {
    60  	case library.Library:
    61  		return m.EvictSubscribedLibrary(ctx, &t)
    62  	case library.Item:
    63  		return m.EvictSubscribedLibraryItem(ctx, &t)
    64  	default:
    65  		return fmt.Errorf("%q is a %T", f.Arg(0), t)
    66  	}
    67  }