func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) { cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) cmd.HostSystemFlag.Register(ctx, f) f.StringVar(&cmd.spec.VswitchName, "vswitch", "", "vSwitch Name") f.IntVar(&cmd.spec.VlanId, "vlan", 0, "VLAN ID") }
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx) cmd.DatacenterFlag.Register(ctx, f) f.BoolVar(&cmd.Long, "l", false, "Long listing format") f.StringVar(&cmd.Type, "t", "", "Object type") }
func (cmd *esxcli) Run(f *flag.FlagSet) error { c, err := cmd.Client() if err != nil { return nil } host, err := cmd.HostSystem() if err != nil { return err } e, err := NewExecutor(c, host) if err != nil { return err } res, err := e.Run(f.Args()) if err != nil { return err } if len(res.Values) == 0 { return nil } // TODO: OutputFlag / format options switch res.Info.Hints.Formatter() { case "table": cmd.formatTable(res) default: cmd.formatSimple(res) } return nil }
// CreateComponentUsage creates a usage function for a component. func createComponentUsage(info pythia.ComponentInfo, fs *flag.FlagSet) func() { return func() { fmt.Fprintf(os.Stderr, componentUsageHeader, os.Args[0], info.Name, info.Description) fs.PrintDefaults() } }
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) { switch ff.Value.(type) { case *StringSlice: default: set.Set(name, ff.Value.String()) } }
func addFetchFlags(fs *flag.FlagSet) { fs.StringVar(&branch, "branch", "", "branch of the package") fs.StringVar(&revision, "revision", "", "revision of the package") fs.StringVar(&tag, "tag", "", "tag of the package") fs.BoolVar(&noRecurse, "no-recurse", false, "do not fetch recursively") fs.BoolVar(&insecure, "precaire", false, "allow the use of insecure protocols") }
func commandHelp(name string, cmd Command, f *flag.FlagSet) { type HasUsage interface { Usage() string } fmt.Fprintf(os.Stderr, "Usage: %s %s [OPTIONS]", os.Args[0], name) if u, ok := cmd.(HasUsage); ok { fmt.Fprintf(os.Stderr, " %s", u.Usage()) } fmt.Fprintf(os.Stderr, "\n") type HasDescription interface { Description() string } if u, ok := cmd.(HasDescription); ok { fmt.Fprintf(os.Stderr, "%s\n", u.Description()) } n := 0 f.VisitAll(func(_ *flag.Flag) { n += 1 }) if n > 0 { fmt.Fprintf(os.Stderr, "\nOptions:\n") tw := tabwriter.NewWriter(os.Stderr, 2, 0, 2, ' ', 0) f.VisitAll(func(f *flag.Flag) { fmt.Fprintf(tw, "\t-%s=%s\t%s\n", f.Name, f.DefValue, f.Usage) }) tw.Flush() } }
func (cmd *set) Run(ctx context.Context, f *flag.FlagSet) error { c, err := cmd.Client() if err != nil { return err } refs, err := cmd.ManagedObjects(ctx, f.Args()) if err != nil { return err } m := object.NewAuthorizationManager(c) rl, err := m.RoleList(ctx) if err != nil { return err } role := rl.ByName(cmd.role) if role == nil { return fmt.Errorf("role '%s' not found", cmd.role) } cmd.Permission.RoleId = role.RoleId perms := []types.Permission{cmd.Permission} for _, ref := range refs { err = m.SetEntityPermissions(ctx, ref, perms) if err != nil { return err } } return nil }
func usageFor(fs *flag.FlagSet, usage string) func() { return func() { var b bytes.Buffer b.WriteString(ansi.Bold("Usage:") + "\n " + usage + "\n") var options [][]string fs.VisitAll(func(f *flag.Flag) { var opt = " -" + f.Name if f.DefValue != "false" { opt += "=" + f.DefValue } options = append(options, []string{opt, f.Usage}) }) if len(options) > 0 { b.WriteString("\n" + ansi.Bold("Options:") + "\n") optionTable(&b, options) } fmt.Println(b.String()) } }
func (cmd *connect) Register(ctx context.Context, f *flag.FlagSet) { cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) cmd.VirtualMachineFlag.Register(ctx, f) f.StringVar(&cmd.device, "device", "", "serial port device name") f.BoolVar(&cmd.client, "client", false, "Use client direction") }
func (cmd *service) Register(ctx context.Context, f *flag.FlagSet) { cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) cmd.HostSystemFlag.Register(ctx, f) f.BoolVar(&cmd.Enable, "enable", false, "Enable service") f.BoolVar(&cmd.Disable, "disable", false, "Disable service") }
func (cmd *install) Run(ctx context.Context, f *flag.FlagSet) error { host, err := cmd.HostSystem() if err != nil { return err } m, err := host.ConfigManager().CertificateManager(ctx) if err != nil { return err } var cert string name := f.Arg(0) if name == "-" || name == "" { var buf bytes.Buffer if _, err := io.Copy(&buf, os.Stdin); err != nil { return err } cert = buf.String() } else { b, err := ioutil.ReadFile(name) if err != nil { return err } cert = string(b) } return m.InstallServerCertificate(ctx, cert) }
// newCommandUnsub creates and returns an unsub command. func newCommandUnsub(args []string, cli *client.Client) (command, error) { // Create a flag set. var flg flag.FlagSet // Define the flags. topicFilter := flg.String("t", "", "Topic Filter") // Parse the flag. if err := flg.Parse(args); err != nil { return nil, errCmdArgsParse } // Create an unsub command. cmd := &commandUnsub{ cli: cli, unsubscribeOpts: &client.UnsubscribeOptions{ TopicFilters: [][]byte{ []byte(*topicFilter), }, }, } // Return the command. return cmd, nil }
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { finder, err := cmd.Finder() if err != nil { return err } lr := listResult{ Elements: nil, Long: cmd.Long, } args := f.Args() if len(args) == 0 { args = []string{"."} } for _, arg := range args { es, err := finder.ManagedObjectListChildren(context.TODO(), arg) if err != nil { return err } for _, e := range es { if cmd.typeMatch(e.Object.Reference()) { lr.Elements = append(lr.Elements, e) } } } return cmd.WriteResult(lr) }
func (cmd *remove) Run(ctx context.Context, f *flag.FlagSet) error { vm, err := cmd.VirtualMachine() if err != nil { return err } if vm == nil { return flag.ErrHelp } devices, err := vm.Device(ctx) if err != nil { return err } for _, name := range f.Args() { device := devices.Find(name) if device == nil { return fmt.Errorf("device '%s' not found", name) } if err = vm.RemoveDevice(ctx, cmd.keepFiles, device); err != nil { return err } } return nil }
func (cmd *download) Run(f *flag.FlagSet) error { m, err := cmd.FileManager() if err != nil { return err } src := f.Arg(0) dst := f.Arg(1) _, err = os.Stat(dst) if err == nil && !cmd.overwrite { return os.ErrExist } info, err := m.InitiateFileTransferFromGuest(context.TODO(), cmd.Auth(), src) if err != nil { return err } u, err := cmd.ParseURL(info.Url) if err != nil { return err } c, err := cmd.Client() if err != nil { return nil } return c.Client.DownloadFile(dst, u, nil) }
func lookupGeneric(name string, set *flag.FlagSet) interface{} { f := set.Lookup(name) if f != nil { return f.Value } return nil }
func (cmd *insert) Run(f *flag.FlagSet) error { vm, err := cmd.VirtualMachine() if err != nil { return err } if vm == nil || f.NArg() != 1 { return flag.ErrHelp } devices, err := vm.Device(context.TODO()) if err != nil { return err } c, err := devices.FindCdrom(cmd.device) if err != nil { return err } iso, err := cmd.DatastorePath(f.Arg(0)) if err != nil { return nil } return vm.EditDevice(context.TODO(), devices.InsertIso(c, iso)) }
func normalizeFlags(flags []Flag, set *flag.FlagSet) error { visited := make(map[string]bool) set.Visit(func(f *flag.Flag) { visited[f.Name] = true }) for _, f := range flags { parts := strings.Split(f.getName(), ",") if len(parts) == 1 { continue } var ff *flag.Flag for _, name := range parts { name = strings.Trim(name, " ") if visited[name] { if ff != nil { return errors.New("Cannot use two forms of the same flag: " + name + " " + ff.Name) } ff = set.Lookup(name) } } if ff == nil { continue } for _, name := range parts { name = strings.Trim(name, " ") if !visited[name] { copyFlag(name, ff, set) } } } return nil }
// Flag configures the given FlagSet with the flags to configure // all active experiments. func Flag(fs *flag.FlagSet) { for _, id := range All { desc := id.Flag() key := fmt.Sprintf("X%s", id.Flag()) fs.Var(&idValue{X: id}, key, desc) } }
func (cmd *ovfx) Prepare(f *flag.FlagSet) (string, error) { var err error args := f.Args() if len(args) != 1 { return "", errors.New("no file specified") } cmd.Client, err = cmd.DatastoreFlag.Client() if err != nil { return "", err } cmd.Datacenter, err = cmd.DatastoreFlag.Datacenter() if err != nil { return "", err } cmd.Datastore, err = cmd.DatastoreFlag.Datastore() if err != nil { return "", err } cmd.ResourcePool, err = cmd.ResourcePoolFlag.ResourcePool() if err != nil { return "", err } return f.Arg(0), nil }
// Apply populates the flag given the flag set and environment func (f Int64SliceFlag) Apply(set *flag.FlagSet) { if f.EnvVar != "" { for _, envVar := range strings.Split(f.EnvVar, ",") { envVar = strings.TrimSpace(envVar) if envVal := os.Getenv(envVar); envVal != "" { newVal := &Int64Slice{} for _, s := range strings.Split(envVal, ",") { s = strings.TrimSpace(s) err := newVal.Set(s) if err != nil { fmt.Fprintf(ErrWriter, err.Error()) } } f.Value = newVal break } } } eachName(f.Name, func(name string) { if f.Value == nil { f.Value = &Int64Slice{} } set.Var(f.Value, name, f.Usage) }) }
func (cmd *change) Run(f *flag.FlagSet) error { if f.NArg() == 0 { return flag.ErrHelp } finder, err := cmd.Finder() if err != nil { return err } cmd.SetAllocation(func(a types.BaseResourceAllocationInfo) { ra := a.GetResourceAllocationInfo() if ra.Shares.Level == "" { ra.Shares = nil } }) for _, arg := range f.Args() { pools, err := finder.ResourcePoolList(context.TODO(), arg) if err != nil { return err } for _, pool := range pools { err := pool.UpdateConfig(context.TODO(), cmd.name, &cmd.ResourceConfigSpec) if err != nil { return err } } } return nil }
func hasFlags(flags *flag.FlagSet) bool { any := false flags.VisitAll(func(*flag.Flag) { any = true }) return any }
func (cmd *change) Run(f *flag.FlagSet) error { ctx := context.TODO() finder, err := cmd.Finder() if err != nil { return err } for _, path := range f.Args() { clusters, err := finder.ClusterComputeResourceList(ctx, path) if err != nil { return err } for _, cluster := range clusters { task, err := cluster.Reconfigure(ctx, &cmd.ClusterConfigSpecEx, true) if err != nil { return err } _, err = task.WaitForResult(ctx, nil) if err != nil { return err } } } return nil }
func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) { cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) cmd.HostSystemFlag.Register(ctx, f) f.StringVar(&cmd.path, "dvs", "", "DVS path") f.StringVar(&cmd.pnic, "pnic", "vmnic0", "Name of the host physical NIC") }
func getFlags(flagset *flag.FlagSet) (flags []*flag.Flag) { flags = make([]*flag.Flag, 0) flagset.VisitAll(func(f *flag.Flag) { flags = append(flags, f) }) return }
func (cmd *destroy) Run(ctx context.Context, f *flag.FlagSet) error { if len(f.Args()) < 1 { return flag.ErrHelp } datacentersToDestroy := f.Args() client, err := cmd.ClientFlag.Client() if err != nil { return err } finder := find.NewFinder(client, false) for _, datacenterToDestroy := range datacentersToDestroy { foundDatacenters, err := finder.DatacenterList(context.TODO(), datacenterToDestroy) if err != nil { return err } for _, foundDatacenter := range foundDatacenters { task, err := foundDatacenter.Destroy(context.TODO()) if err != nil { return err } if err := task.Wait(context.TODO()); err != nil { return err } } } return nil }
func usageFor(fs *flag.FlagSet, usage string, extra string) func() { return func() { var b bytes.Buffer b.WriteString("Usage:\n " + usage + "\n") var options [][]string fs.VisitAll(func(f *flag.Flag) { var opt = " -" + f.Name if f.DefValue == "[]" { f.DefValue = "" } if f.DefValue != "false" { opt += "=" + fmt.Sprintf(`"%s"`, f.DefValue) } options = append(options, []string{opt, f.Usage}) }) if len(options) > 0 { b.WriteString("\nOptions:\n") optionTable(&b, options) } fmt.Println(b.String()) if len(extra) > 0 { fmt.Println(extra) } } }
func (cmd *disconnect) Run(ctx context.Context, f *flag.FlagSet) error { vm, err := cmd.VirtualMachine() if err != nil { return err } if vm == nil { return flag.ErrHelp } devices, err := vm.Device(context.TODO()) if err != nil { return err } for _, name := range f.Args() { device := devices.Find(name) if device == nil { return fmt.Errorf("device '%s' not found", name) } if err = devices.Disconnect(device); err != nil { return err } if err = vm.EditDevice(context.TODO(), device); err != nil { return err } } return nil }