Skip to content

Commit

Permalink
More completions (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Aug 19, 2021
1 parent 423df97 commit 9fdf890
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 41 deletions.
70 changes: 70 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ func machineListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDir
}
return names, cobra.ShellCompDirectiveNoFileComp
}
func firewallListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.FirewallList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, m := range resp.Firewalls {
name := *m.ID
if m.Allocation != nil && *m.Allocation.Hostname != "" {
name = name + "\t" + *m.Allocation.Hostname
}
names = append(names, name)
}
return names, cobra.ShellCompDirectiveNoFileComp
}
func networkListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.NetworkList()
if err != nil {
Expand Down Expand Up @@ -117,3 +132,58 @@ func outputFormatListCompletion() ([]string, cobra.ShellCompDirective) {
func outputOrderListCompletion() ([]string, cobra.ShellCompDirective) {
return []string{"size", "id", "status", "event", "when", "partition", "project"}, cobra.ShellCompDirectiveNoFileComp
}

var machineListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return machineListCompletion(driver)
}
var firewallListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return firewallListCompletion(driver)
}
var filesystemLayoutListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return filesystemLayoutListCompletion(driver)
}
var imageListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return imageListCompletion(driver)
}
var networkListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return networkListCompletion(driver)
}
var ipListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return ipListCompletion(driver)
}
var partitionListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return partitionListCompletion(driver)
}
var projectListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return projectListCompletion(driver)
}
var sizeListCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return sizeListCompletion(driver)
}
4 changes: 3 additions & 1 deletion cmd/filesystemlayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
RunE: func(cmd *cobra.Command, args []string) error {
return filesystemDescribe(driver, args)
},
ValidArgsFunction: filesystemLayoutListCompletionFunc,
}
filesystemApplyCmd = &cobra.Command{
Use: "apply",
Expand All @@ -52,7 +53,8 @@ var (
RunE: func(cmd *cobra.Command, args []string) error {
return filesystemDelete(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: filesystemLayoutListCompletionFunc,
}
filesystemTryCmd = &cobra.Command{
Use: "try",
Expand Down
11 changes: 7 additions & 4 deletions cmd/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var (
RunE: func(cmd *cobra.Command, args []string) error {
return firewallDescribe(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: firewallListCompletionFunc,
}

firewallDestroyCmd = &cobra.Command{
Expand All @@ -55,7 +56,8 @@ A destroyed firewall can not restored anymore`,
RunE: func(cmd *cobra.Command, args []string) error {
return firewallDestroy(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: firewallListCompletionFunc,
}

firewallReserveCmd = &cobra.Command{
Expand All @@ -67,7 +69,8 @@ should be removed with --remove.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineReserve(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: firewallListCompletionFunc,
}
)

Expand Down Expand Up @@ -102,7 +105,7 @@ func init() {
log.Fatal(err.Error())
}
err = firewallListCmd.RegisterFlagCompletionFunc("id", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return machineListCompletion(driver)
return firewallListCompletion(driver)
})
if err != nil {
log.Fatal(err.Error())
Expand Down
7 changes: 5 additions & 2 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
RunE: func(cmd *cobra.Command, args []string) error {
return imageDescribe(driver, args)
},
ValidArgsFunction: imageListCompletionFunc,
}
imageCreateCmd = &cobra.Command{
Use: "create",
Expand Down Expand Up @@ -67,15 +68,17 @@ var (
RunE: func(cmd *cobra.Command, args []string) error {
return imageDelete(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: imageListCompletionFunc,
}
imageEditCmd = &cobra.Command{
Use: "edit <imageID>",
Short: "edit a image",
RunE: func(cmd *cobra.Command, args []string) error {
return imageEdit(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: imageListCompletionFunc,
}
)

Expand Down
57 changes: 38 additions & 19 deletions cmd/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ Once created the machine installation can not be modified anymore.
RunE: func(cmd *cobra.Command, args []string) error {
return machineDescribe(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineConsolePasswordCmd = &cobra.Command{
Expand All @@ -117,7 +118,8 @@ Once created the machine installation can not be modified anymore.
RunE: func(cmd *cobra.Command, args []string) error {
return machineConsolePassword(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineDestroyCmd = &cobra.Command{
Expand All @@ -129,7 +131,8 @@ A destroyed machine can not restored anymore`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineDestroy(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machinePowerCmd = &cobra.Command{
Expand All @@ -144,7 +147,8 @@ A destroyed machine can not restored anymore`,
RunE: func(cmd *cobra.Command, args []string) error {
return machinePowerOn(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machinePowerOffCmd = &cobra.Command{
Expand All @@ -156,7 +160,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machinePowerOff(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machinePowerResetCmd = &cobra.Command{
Expand All @@ -166,7 +171,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machinePowerReset(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machinePowerCycleCmd = &cobra.Command{
Expand All @@ -176,7 +182,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machinePowerCycle(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineUpdateCmd = &cobra.Command{
Expand All @@ -192,7 +199,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineUpdateBios(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineUpdateBmcCmd = &cobra.Command{
Expand All @@ -202,7 +210,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineUpdateBmc(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineBootBiosCmd = &cobra.Command{
Expand All @@ -212,7 +221,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineBootBios(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineBootPxeCmd = &cobra.Command{
Expand All @@ -222,7 +232,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineBootPxe(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineBootDiskCmd = &cobra.Command{
Expand All @@ -232,7 +243,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineBootDisk(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineIdentifyCmd = &cobra.Command{
Expand All @@ -247,7 +259,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineIdentifyOn(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineIdentifyOffCmd = &cobra.Command{
Expand All @@ -257,7 +270,8 @@ Power on will therefore not work if the machine is in the powering off phase.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineIdentifyOff(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineReserveCmd = &cobra.Command{
Expand All @@ -269,7 +283,8 @@ should be removed with --remove.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineReserve(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineLockCmd = &cobra.Command{
Expand All @@ -279,7 +294,8 @@ should be removed with --remove.`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineLock(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineReinstallCmd = &cobra.Command{
Expand All @@ -290,7 +306,8 @@ is wiped and the new image will subsequently be installed on that device`,
RunE: func(cmd *cobra.Command, args []string) error {
return machineReinstall(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}

machineConsoleCmd = &cobra.Command{
Expand All @@ -300,7 +317,8 @@ In case the machine did not register properly a direct ipmi console access is av
RunE: func(cmd *cobra.Command, args []string) error {
return machineConsole(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}
machineIpmiCmd = &cobra.Command{
Use: "ipmi [<machine ID>]",
Expand All @@ -325,7 +343,8 @@ In case the machine did not register properly a direct ipmi console access is av
RunE: func(cmd *cobra.Command, args []string) error {
return machineLogs(driver, args)
},
PreRun: bindPFlags,
PreRun: bindPFlags,
ValidArgsFunction: machineListCompletionFunc,
}
)

Expand Down
Loading

0 comments on commit 9fdf890

Please sign in to comment.