github.com/vmware/govmomi@v0.51.0/cli/library/publish.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 publish struct {
    18  	*flags.ClientFlag
    19  }
    20  
    21  func init() {
    22  	cli.Register("library.publish", &publish{})
    23  }
    24  
    25  func (cmd *publish) 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 *publish) Usage() string {
    31  	return "NAME|ITEM [SUBSCRIPTION-ID]..."
    32  }
    33  
    34  func (cmd *publish) Description() string {
    35  	return `Publish library NAME or ITEM to subscribers.
    36  
    37  If no subscriptions are specified, then publishes the library to all its subscribers.
    38  See 'govc library.subscriber.ls' to get a list of subscription IDs.
    39  
    40  Examples:
    41    govc library.publish /my-library
    42    govc library.publish /my-library subscription-id1 subscription-id2
    43    govc library.publish /my-library/my-item
    44    govc library.publish /my-library/my-item subscription-id1 subscription-id2`
    45  }
    46  
    47  func (cmd *publish) Run(ctx context.Context, f *flag.FlagSet) error {
    48  	c, err := cmd.RestClient()
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	res, err := flags.ContentLibraryResult(ctx, c, "", f.Arg(0))
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	m := library.NewManager(c)
    59  
    60  	ids := f.Args()[1:]
    61  
    62  	switch t := res.GetResult().(type) {
    63  	case library.Library:
    64  		return m.PublishLibrary(ctx, &t, ids)
    65  	case library.Item:
    66  		return m.PublishLibraryItem(ctx, &t, false, ids)
    67  	default:
    68  		return fmt.Errorf("%q is a %T", res.GetPath(), t)
    69  	}
    70  }