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

     1  /*
     2  Copyright (c) 2014-2015 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 license
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  	"github.com/vmware/govmomi/license"
    26  )
    27  
    28  var featureUsage = "List licenses with given feature"
    29  
    30  type ls struct {
    31  	*flags.ClientFlag
    32  	*flags.OutputFlag
    33  
    34  	feature string
    35  }
    36  
    37  func init() {
    38  	cli.Register("license.ls", &ls{})
    39  }
    40  
    41  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    43  	cmd.ClientFlag.Register(ctx, f)
    44  
    45  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    46  	cmd.OutputFlag.Register(ctx, f)
    47  
    48  	f.StringVar(&cmd.feature, "feature", "", featureUsage)
    49  }
    50  
    51  func (cmd *ls) Process(ctx context.Context) error {
    52  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    53  		return err
    54  	}
    55  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    56  		return err
    57  	}
    58  	return nil
    59  }
    60  
    61  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    62  	client, err := cmd.Client()
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	m := license.NewManager(client)
    68  	result, err := m.List(ctx)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	if cmd.feature != "" {
    74  		result = result.WithFeature(cmd.feature)
    75  	}
    76  
    77  	return cmd.WriteResult(licenseOutput(result))
    78  }