-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring: Generic CLI commands. (#140)
- Loading branch information
Showing
67 changed files
with
3,870 additions
and
5,319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package completion | ||
|
||
import ( | ||
"github.com/metal-stack/metalctl/pkg/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (c *Completion) ContextListCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
ctxs, err := api.GetContexts() | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
var names []string | ||
for name := range ctxs.Contexts { | ||
names = append(names, name) | ||
} | ||
return names, cobra.ShellCompDirectiveNoFileComp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package completion | ||
|
||
import ( | ||
"github.com/metal-stack/metal-go/api/client/filesystemlayout" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (c *Completion) FilesystemLayoutListCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
resp, err := c.client.Filesystemlayout().ListFilesystemLayouts(filesystemlayout.NewListFilesystemLayoutsParams(), nil) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
var names []string | ||
for _, s := range resp.Payload { | ||
names = append(names, *s.ID+"\t"+s.Description) | ||
} | ||
return names, cobra.ShellCompDirectiveNoFileComp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package completion | ||
|
||
import ( | ||
"github.com/metal-stack/metal-go/api/client/firewall" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (c *Completion) FirewallListCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
resp, err := c.client.Firewall().ListFirewalls(firewall.NewListFirewallsParams(), nil) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
var names []string | ||
for _, m := range resp.Payload { | ||
name := *m.ID | ||
if m.Allocation != nil && *m.Allocation.Hostname != "" { | ||
name = name + "\t" + *m.Allocation.Hostname | ||
} | ||
names = append(names, name) | ||
} | ||
return names, cobra.ShellCompDirectiveNoFileComp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package completion | ||
|
||
import ( | ||
metalgo "github.com/metal-stack/metal-go" | ||
"github.com/metal-stack/metal-go/api/client/firmware" | ||
"github.com/metal-stack/metal-go/api/client/machine" | ||
"github.com/metal-stack/metal-lib/pkg/pointer" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (c *Completion) FirmwareKindCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{string(metalgo.Bmc), string(metalgo.Bios)}, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
|
||
func (c *Completion) FirmwareVendorCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
resp, err := c.client.Firmware().ListFirmwares(firmware.NewListFirmwaresParams(), nil) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
|
||
var vendors []string | ||
for _, vv := range resp.Payload.Revisions { | ||
for v := range vv.VendorRevisions { | ||
vendors = append(vendors, v) | ||
} | ||
} | ||
return vendors, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
|
||
func (c *Completion) FirmwareBoardCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
resp, err := c.client.Firmware().ListFirmwares(firmware.NewListFirmwaresParams(), nil) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
|
||
var boards []string | ||
for _, vv := range resp.Payload.Revisions { | ||
for _, bb := range vv.VendorRevisions { | ||
for b := range bb.BoardRevisions { | ||
boards = append(boards, b) | ||
} | ||
} | ||
} | ||
return boards, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
func (c *Completion) FirmwareRevisionCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return c.firmwareRevisions("", "") | ||
} | ||
func (c *Completion) FirmwareBiosRevisionCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if len(args) != 1 { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
return c.firmwareRevisions(args[0], metalgo.Bios) | ||
} | ||
func (c *Completion) FirmwareBmcRevisionCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if len(args) != 1 { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
return c.firmwareRevisions(args[0], metalgo.Bmc) | ||
} | ||
|
||
func (c *Completion) firmwareRevisions(machineID string, kind metalgo.FirmwareKind) ([]string, cobra.ShellCompDirective) { | ||
vendor := "" | ||
board := "" | ||
if machineID != "" { | ||
m, err := c.client.Machine().FindIPMIMachine(machine.NewFindIPMIMachineParams().WithID(machineID), nil) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
board = m.Payload.Ipmi.Fru.BoardPartNumber | ||
vendor = m.Payload.Ipmi.Fru.BoardMfg | ||
} | ||
resp, err := c.client.Firmware().ListFirmwares(firmware.NewListFirmwaresParams().WithKind(pointer.Pointer(string(kind))).WithVendor(&vendor).WithBoard(&board), nil) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
|
||
var revisions []string | ||
for _, vv := range resp.Payload.Revisions { | ||
for _, bb := range vv.VendorRevisions { | ||
for _, rr := range bb.BoardRevisions { | ||
revisions = append(revisions, rr...) | ||
} | ||
} | ||
} | ||
return revisions, cobra.ShellCompDirectiveNoFileComp | ||
} |
Oops, something went wrong.