Skip to content

Commit

Permalink
Merge pull request #171 from xmudrii/flags
Browse files Browse the repository at this point in the history
[Idea] [Breaking changes!] Add shorthand flags
  • Loading branch information
nanzhong authored Dec 23, 2016
2 parents 9177435 + 58dad03 commit 658f28e
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 107 deletions.
19 changes: 19 additions & 0 deletions args_short.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2016 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package doctl

const (
// ArgShortDeleteForce forces deletion actions
ArgShortDeleteForce = "f"
)
14 changes: 7 additions & 7 deletions commands/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ func Actions() *Command {

cmdActionList := CmdBuilder(cmd, RunCmdActionList, "list", "list actions", Writer,
aliasOpt("ls"), displayerType(&action{}), docCategories("action"))
AddStringFlag(cmdActionList, doctl.ArgActionResourceType, "", "Action resource type")
AddStringFlag(cmdActionList, doctl.ArgActionRegion, "", "Action region")
AddStringFlag(cmdActionList, doctl.ArgActionAfter, "", "Action completed after in RFC3339 format")
AddStringFlag(cmdActionList, doctl.ArgActionBefore, "", "Action completed before in RFC3339 format")
AddStringFlag(cmdActionList, doctl.ArgActionStatus, "", "Action status")
AddStringFlag(cmdActionList, doctl.ArgActionType, "", "Action type")
AddStringFlag(cmdActionList, doctl.ArgActionResourceType, "", "", "Action resource type")
AddStringFlag(cmdActionList, doctl.ArgActionRegion, "", "", "Action region")
AddStringFlag(cmdActionList, doctl.ArgActionAfter, "", "", "Action completed after in RFC3339 format")
AddStringFlag(cmdActionList, doctl.ArgActionBefore, "", "", "Action completed before in RFC3339 format")
AddStringFlag(cmdActionList, doctl.ArgActionStatus, "", "", "Action status")
AddStringFlag(cmdActionList, doctl.ArgActionType, "", "", "Action type")

cmdActionWait := CmdBuilder(cmd, RunCmdActionWait, "wait ACTIONID", "wait for action to complete", Writer,
aliasOpt("w"), displayerType(&action{}), docCategories("action"))
AddIntFlag(cmdActionWait, doctl.ArgPollTime, 5, "Re-poll time in seconds")
AddIntFlag(cmdActionWait, doctl.ArgPollTime, "", 5, "Re-poll time in seconds")

return cmd
}
Expand Down
20 changes: 10 additions & 10 deletions commands/doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ func isBeta() bool {
}

// AddStringFlag adds a string flag to a command.
func AddStringFlag(cmd *Command, name, dflt, desc string, opts ...flagOpt) {
func AddStringFlag(cmd *Command, name, shorthand, dflt, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().String(name, dflt, desc)
cmd.Flags().StringP(name, shorthand, dflt, desc)

for _, o := range opts {
o(cmd, name, fn)
Expand All @@ -225,9 +225,9 @@ func AddStringFlag(cmd *Command, name, dflt, desc string, opts ...flagOpt) {
}

// AddIntFlag adds an integr flag to a command.
func AddIntFlag(cmd *Command, name string, def int, desc string, opts ...flagOpt) {
func AddIntFlag(cmd *Command, name, shorthand string, def int, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().Int(name, def, desc)
cmd.Flags().IntP(name, shorthand, def, desc)
viper.BindPFlag(fn, cmd.Flags().Lookup(name))

for _, o := range opts {
Expand All @@ -236,9 +236,9 @@ func AddIntFlag(cmd *Command, name string, def int, desc string, opts ...flagOpt
}

// AddBoolFlag adds a boolean flag to a command.
func AddBoolFlag(cmd *Command, name string, def bool, desc string, opts ...flagOpt) {
func AddBoolFlag(cmd *Command, name, shorthand string, def bool, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().Bool(name, def, desc)
cmd.Flags().BoolP(name, shorthand, def, desc)
viper.BindPFlag(fn, cmd.Flags().Lookup(name))

for _, o := range opts {
Expand All @@ -247,9 +247,9 @@ func AddBoolFlag(cmd *Command, name string, def bool, desc string, opts ...flagO
}

// AddStringSliceFlag adds a string slice flag to a command.
func AddStringSliceFlag(cmd *Command, name string, def []string, desc string, opts ...flagOpt) {
func AddStringSliceFlag(cmd *Command, name, shorthand string, def []string, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().StringSlice(name, def, desc)
cmd.Flags().StringSliceP(name, shorthand, def, desc)
viper.BindPFlag(fn, cmd.Flags().Lookup(name))

for _, o := range opts {
Expand Down Expand Up @@ -401,8 +401,8 @@ func cmdBuilderWithInit(parent *Command, cr CmdRunner, cliText, desc string, out
if cols := c.fmtCols; cols != nil {
formatHelp := fmt.Sprintf("Columns for output in a comma seperated list. Possible values: %s",
strings.Join(cols, ","))
AddStringFlag(c, doctl.ArgFormat, "", formatHelp)
AddBoolFlag(c, doctl.ArgNoHeader, false, "hide headers")
AddStringFlag(c, doctl.ArgFormat, "", "", formatHelp)
AddBoolFlag(c, doctl.ArgNoHeader, "", false, "hide headers")
}

return c
Expand Down
30 changes: 15 additions & 15 deletions commands/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Domain() *Command {

cmdDomainCreate := CmdBuilder(cmd, RunDomainCreate, "create <domain>", "create domain", Writer,
aliasOpt("c"), displayerType(&domain{}), docCategories("domain"))
AddStringFlag(cmdDomainCreate, doctl.ArgIPAddress, "", "IP address", requiredOpt())
AddStringFlag(cmdDomainCreate, doctl.ArgIPAddress, "", "", "IP address", requiredOpt())

CmdBuilder(cmd, RunDomainList, "list", "list domains", Writer,
aliasOpt("ls"), displayerType(&domain{}), docCategories("domain"))
Expand All @@ -59,29 +59,29 @@ func Domain() *Command {

cmdRecordList := CmdBuilder(cmdRecord, RunRecordList, "list <domain>", "list records", Writer,
aliasOpt("ls"), displayerType(&domainRecord{}), docCategories("domain"))
AddStringFlag(cmdRecordList, doctl.ArgDomainName, "", "Domain name")
AddStringFlag(cmdRecordList, doctl.ArgDomainName, "", "", "Domain name")

cmdRecordCreate := CmdBuilder(cmdRecord, RunRecordCreate, "create <domain>", "create record", Writer,
aliasOpt("c"), displayerType(&domainRecord{}), docCategories("domain"))
AddStringFlag(cmdRecordCreate, doctl.ArgRecordType, "", "Record type")
AddStringFlag(cmdRecordCreate, doctl.ArgRecordName, "", "Record name")
AddStringFlag(cmdRecordCreate, doctl.ArgRecordData, "", "Record data")
AddIntFlag(cmdRecordCreate, doctl.ArgRecordPriority, 0, "Record priority")
AddIntFlag(cmdRecordCreate, doctl.ArgRecordPort, 0, "Record port")
AddIntFlag(cmdRecordCreate, doctl.ArgRecordWeight, 0, "Record weight")
AddStringFlag(cmdRecordCreate, doctl.ArgRecordType, "", "", "Record type")
AddStringFlag(cmdRecordCreate, doctl.ArgRecordName, "", "", "Record name")
AddStringFlag(cmdRecordCreate, doctl.ArgRecordData, "", "", "Record data")
AddIntFlag(cmdRecordCreate, doctl.ArgRecordPriority, "", 0, "Record priority")
AddIntFlag(cmdRecordCreate, doctl.ArgRecordPort, "", 0, "Record port")
AddIntFlag(cmdRecordCreate, doctl.ArgRecordWeight, "", 0, "Record weight")

CmdBuilder(cmdRecord, RunRecordDelete, "delete <domain> <record id...>", "delete record", Writer,
aliasOpt("d"), docCategories("domain"))

cmdRecordUpdate := CmdBuilder(cmdRecord, RunRecordUpdate, "update <domain>", "update record", Writer,
aliasOpt("u"), displayerType(&domainRecord{}), docCategories("domain"))
AddIntFlag(cmdRecordUpdate, doctl.ArgRecordID, 0, "Record ID")
AddStringFlag(cmdRecordUpdate, doctl.ArgRecordType, "", "Record type")
AddStringFlag(cmdRecordUpdate, doctl.ArgRecordName, "", "Record name")
AddStringFlag(cmdRecordUpdate, doctl.ArgRecordData, "", "Record data")
AddIntFlag(cmdRecordUpdate, doctl.ArgRecordPriority, 0, "Record priority")
AddIntFlag(cmdRecordUpdate, doctl.ArgRecordPort, 0, "Record port")
AddIntFlag(cmdRecordUpdate, doctl.ArgRecordWeight, 0, "Record weight")
AddIntFlag(cmdRecordUpdate, doctl.ArgRecordID, "", 0, "Record ID")
AddStringFlag(cmdRecordUpdate, doctl.ArgRecordType, "", "", "Record type")
AddStringFlag(cmdRecordUpdate, doctl.ArgRecordName, "", "", "Record name")
AddStringFlag(cmdRecordUpdate, doctl.ArgRecordData, "", "", "Record data")
AddIntFlag(cmdRecordUpdate, doctl.ArgRecordPriority, "", 0, "Record priority")
AddIntFlag(cmdRecordUpdate, doctl.ArgRecordPort, "", 0, "Record port")
AddIntFlag(cmdRecordUpdate, doctl.ArgRecordWeight, "", 0, "Record weight")

return cmd
}
Expand Down
48 changes: 24 additions & 24 deletions commands/droplet_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,94 +61,94 @@ func DropletAction() *Command {

cmdDropletActionGet := CmdBuilder(cmd, RunDropletActionGet, "get", "get droplet action", Writer,
aliasOpt("g"), displayerType(&action{}), docCategories("droplet"))
AddIntFlag(cmdDropletActionGet, doctl.ArgActionID, 0, "Action ID", requiredOpt())
AddIntFlag(cmdDropletActionGet, doctl.ArgActionID, "", 0, "Action ID", requiredOpt())

cmdDropletActionDisableBackups := CmdBuilder(cmd, RunDropletActionDisableBackups,
"disable-backups <droplet-id>", "disable backups", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionDisableBackups, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionDisableBackups, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionReboot := CmdBuilder(cmd, RunDropletActionReboot,
"reboot <droplet-id>", "reboot droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionReboot, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionReboot, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionPowerCycle := CmdBuilder(cmd, RunDropletActionPowerCycle,
"power-cycle <droplet-id>", "power cycle droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionPowerCycle, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionPowerCycle, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionShutdown := CmdBuilder(cmd, RunDropletActionShutdown,
"shutdown <droplet-id>", "shutdown droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionShutdown, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionShutdown, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionPowerOff := CmdBuilder(cmd, RunDropletActionPowerOff,
"power-off <droplet-id>", "power off droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionPowerOff, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionPowerOff, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionPowerOn := CmdBuilder(cmd, RunDropletActionPowerOn,
"power-on <droplet-id>", "power on droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionPowerOn, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionPowerOn, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionPasswordReset := CmdBuilder(cmd, RunDropletActionPasswordReset,
"password-reset <droplet-id>", "password reset droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionPasswordReset, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionPasswordReset, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionEnableIPv6 := CmdBuilder(cmd, RunDropletActionEnableIPv6,
"enable-ipv6 <droplet-id>", "enable ipv6", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionEnableIPv6, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionEnableIPv6, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionEnablePrivateNetworking := CmdBuilder(cmd, RunDropletActionEnablePrivateNetworking,
"enable-private-networking <droplet-id>", "enable private networking", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionEnablePrivateNetworking, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionEnablePrivateNetworking, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionUpgrade := CmdBuilder(cmd, RunDropletActionUpgrade,
"upgrade <droplet-id>", "upgrade droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionUpgrade, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionUpgrade, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionRestore := CmdBuilder(cmd, RunDropletActionRestore,
"restore <droplet-id>", "restore backup", Writer,
displayerType(&action{}), docCategories("droplet"))
AddIntFlag(cmdDropletActionRestore, doctl.ArgImageID, 0, "Image ID", requiredOpt())
AddBoolFlag(cmdDropletActionRestore, doctl.ArgCommandWait, false, "Wait for action to complete")
AddIntFlag(cmdDropletActionRestore, doctl.ArgImageID, "", 0, "Image ID", requiredOpt())
AddBoolFlag(cmdDropletActionRestore, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionResize := CmdBuilder(cmd, RunDropletActionResize,
"resize <droplet-id>", "resize droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddBoolFlag(cmdDropletActionResize, doctl.ArgResizeDisk, false, "Resize disk")
AddStringFlag(cmdDropletActionResize, doctl.ArgSizeSlug, "", "New size")
AddBoolFlag(cmdDropletActionResize, doctl.ArgCommandWait, false, "Wait for action to complete")
AddBoolFlag(cmdDropletActionResize, doctl.ArgResizeDisk, "", false, "Resize disk")
AddStringFlag(cmdDropletActionResize, doctl.ArgSizeSlug, "", "", "New size")
AddBoolFlag(cmdDropletActionResize, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionRebuild := CmdBuilder(cmd, RunDropletActionRebuild,
"rebuild <droplet-id>", "rebuild droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddStringFlag(cmdDropletActionRebuild, doctl.ArgImage, "", "Image ID or Slug", requiredOpt())
AddBoolFlag(cmdDropletActionRebuild, doctl.ArgCommandWait, false, "Wait for action to complete")
AddStringFlag(cmdDropletActionRebuild, doctl.ArgImage, "", "", "Image ID or Slug", requiredOpt())
AddBoolFlag(cmdDropletActionRebuild, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionRename := CmdBuilder(cmd, RunDropletActionRename,
"rename <droplet-id>", "rename droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddStringFlag(cmdDropletActionRename, doctl.ArgDropletName, "", "Droplet name", requiredOpt())
AddBoolFlag(cmdDropletActionRename, doctl.ArgCommandWait, false, "Wait for action to complete")
AddStringFlag(cmdDropletActionRename, doctl.ArgDropletName, "", "", "Droplet name", requiredOpt())
AddBoolFlag(cmdDropletActionRename, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionChangeKernel := CmdBuilder(cmd, RunDropletActionChangeKernel,
"change-kernel <droplet-id>", "change kernel", Writer,
displayerType(&action{}), docCategories("droplet"))
AddIntFlag(cmdDropletActionChangeKernel, doctl.ArgKernelID, 0, "Kernel ID", requiredOpt())
AddBoolFlag(cmdDropletActionChangeKernel, doctl.ArgCommandWait, false, "Wait for action to complete")
AddIntFlag(cmdDropletActionChangeKernel, doctl.ArgKernelID, "", 0, "Kernel ID", requiredOpt())
AddBoolFlag(cmdDropletActionChangeKernel, doctl.ArgCommandWait, "", false, "Wait for action to complete")

cmdDropletActionSnapshot := CmdBuilder(cmd, RunDropletActionSnapshot,
"snapshot <droplet-id>", "snapshot droplet", Writer,
displayerType(&action{}), docCategories("droplet"))
AddStringFlag(cmdDropletActionSnapshot, doctl.ArgSnapshotName, "", "Snapshot name", requiredOpt())
AddBoolFlag(cmdDropletActionSnapshot, doctl.ArgCommandWait, false, "Wait for action to complete")
AddStringFlag(cmdDropletActionSnapshot, doctl.ArgSnapshotName, "", "", "Snapshot name", requiredOpt())
AddBoolFlag(cmdDropletActionSnapshot, doctl.ArgCommandWait, "", false, "Wait for action to complete")

return cmd
}
Expand Down
Loading

0 comments on commit 658f28e

Please sign in to comment.