github.com/vmware/govmomi@v0.37.1/govc/license/decode.go (about) 1 /* 2 Copyright (c) 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 type decode struct { 29 *flags.ClientFlag 30 *flags.OutputFlag 31 32 feature string 33 } 34 35 func init() { 36 cli.Register("license.decode", &decode{}) 37 } 38 39 func (cmd *decode) Register(ctx context.Context, f *flag.FlagSet) { 40 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 41 cmd.ClientFlag.Register(ctx, f) 42 43 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 44 cmd.OutputFlag.Register(ctx, f) 45 46 f.StringVar(&cmd.feature, "feature", "", featureUsage) 47 } 48 49 func (cmd *decode) Process(ctx context.Context) error { 50 if err := cmd.ClientFlag.Process(ctx); err != nil { 51 return err 52 } 53 if err := cmd.OutputFlag.Process(ctx); err != nil { 54 return err 55 } 56 return nil 57 } 58 59 func (cmd *decode) Usage() string { 60 return "KEY..." 61 } 62 63 func (cmd *decode) Run(ctx context.Context, f *flag.FlagSet) error { 64 client, err := cmd.Client() 65 if err != nil { 66 return err 67 } 68 69 var result license.InfoList 70 m := license.NewManager(client) 71 for _, v := range f.Args() { 72 license, err := m.Decode(ctx, v) 73 if err != nil { 74 return err 75 } 76 77 result = append(result, license) 78 } 79 80 if cmd.feature != "" { 81 result = result.WithFeature(cmd.feature) 82 } 83 84 return cmd.WriteResult(licenseOutput(result)) 85 }