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

     1  /*
     2  Copyright (c) 2020 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 subscriber
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"text/tabwriter"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/vapi/library"
    29  )
    30  
    31  type ls struct {
    32  	*flags.ClientFlag
    33  	*flags.OutputFlag
    34  }
    35  
    36  func init() {
    37  	cli.Register("library.subscriber.ls", &ls{})
    38  }
    39  
    40  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    42  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    43  	cmd.ClientFlag.Register(ctx, f)
    44  	cmd.OutputFlag.Register(ctx, f)
    45  }
    46  
    47  func (cmd *ls) Process(ctx context.Context) error {
    48  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    49  		return err
    50  	}
    51  	return cmd.OutputFlag.Process(ctx)
    52  }
    53  
    54  func (cmd *ls) Description() string {
    55  	return `List library subscriptions.
    56  
    57  Examples:
    58    govc library.subscriber.ls library-name`
    59  }
    60  
    61  type lsResultsWriter []library.SubscriberSummary
    62  
    63  func (r lsResultsWriter) Write(w io.Writer) error {
    64  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    65  
    66  	_, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", "Subscription ID", "Library Name", "Library ID", "vCenter")
    67  
    68  	for _, i := range r {
    69  		_, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", i.SubscriptionID, i.LibraryName, i.LibraryID, i.LibraryVcenterHostname)
    70  	}
    71  
    72  	return tw.Flush()
    73  }
    74  
    75  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    76  	c, err := cmd.RestClient()
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	lib, err := flags.ContentLibrary(ctx, c, f.Arg(0))
    82  	if err != nil {
    83  		return err
    84  	}
    85  	m := library.NewManager(c)
    86  
    87  	s, err := m.ListSubscribers(ctx, lib)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	return cmd.WriteResult(lsResultsWriter(s))
    93  }