github.com/vmware/govmomi@v0.51.0/cli/fields/add.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 fields 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/object" 15 ) 16 17 type add struct { 18 *flags.ClientFlag 19 kind string 20 } 21 22 func init() { 23 cli.Register("fields.add", &add{}) 24 } 25 26 func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) { 27 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 28 cmd.ClientFlag.Register(ctx, f) 29 30 f.StringVar(&cmd.kind, "type", "", "Managed object type") 31 } 32 33 func (cmd *add) Usage() string { 34 return "NAME" 35 } 36 37 func (cmd *add) Description() string { 38 return `Add a custom field type with NAME. 39 40 Examples: 41 govc fields.add my-field-name # adds a field to all managed object types 42 govc fields.add -type VirtualMachine my-vm-field-name # adds a field to the VirtualMachine type` 43 } 44 45 func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error { 46 if f.NArg() != 1 { 47 return flag.ErrHelp 48 } 49 50 c, err := cmd.Client() 51 if err != nil { 52 return err 53 } 54 55 m, err := object.GetCustomFieldsManager(c) 56 if err != nil { 57 return err 58 } 59 60 name := f.Arg(0) 61 62 def, err := m.Add(ctx, name, cmd.kind, nil, nil) 63 if err != nil { 64 return err 65 } 66 67 fmt.Printf("%d\n", def.Key) 68 69 return nil 70 }