-
Notifications
You must be signed in to change notification settings - Fork 0
Fix completion #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
majst01
wants to merge
5
commits into
main
Choose a base branch
from
fix-completion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Fix completion #47
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,132 @@ | ||
| package v2 | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2" | ||
| apiv2 "github.com/metal-stack/api/go/metalstack/api/v2" | ||
| "github.com/metal-stack/cli/cmd/config" | ||
| "github.com/metal-stack/cli/cmd/sorters" | ||
| "github.com/metal-stack/cli/pkg/helpers" | ||
| "github.com/metal-stack/metal-lib/pkg/genericcli" | ||
| "github.com/metal-stack/metal-lib/pkg/genericcli/printers" | ||
| "github.com/metal-stack/metal-lib/pkg/pointer" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| type ip struct { | ||
| c *config.Config | ||
| } | ||
|
|
||
| func newIPCmd(c *config.Config) *cobra.Command { | ||
| w := &ip{ | ||
| c: c, | ||
| } | ||
|
|
||
| cmdsConfig := &genericcli.CmdsConfig[any, any, *apiv2.IP]{ | ||
| BinaryName: config.BinaryName, | ||
| GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs), | ||
| Singular: "ip", | ||
| Plural: "ips", | ||
| Description: "an ip address of metal-stack.io", | ||
| Sorter: sorters.IPSorter(), | ||
| DescribePrinter: func() printers.Printer { return c.DescribePrinter }, | ||
| ListPrinter: func() printers.Printer { return c.ListPrinter }, | ||
| ListCmdMutateFn: func(cmd *cobra.Command) { | ||
| cmd.Flags().String("ip", "", "ip which should be listed") | ||
| cmd.Flags().String("uuid", "", "allocation uuid of ip which should be listed") | ||
| cmd.Flags().String("project", "", "project from where ips should be listed") | ||
| cmd.Flags().String("name", "", "name from ips which should be listed") | ||
| cmd.Flags().String("network", "", "network from where ips should be listed") | ||
| cmd.Flags().String("machine", "", "machine where ips are attached to") | ||
| cmd.Flags().StringSlice("labels", nil, "lists only ips with the given labels") | ||
| cmd.Flags().String("addressfamily", "", "addressfamily of ips which should be listed") | ||
| cmd.Flags().String("type", "", "type of ips which should be listed") | ||
|
|
||
| genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.Project)) | ||
| genericcli.Must(cmd.RegisterFlagCompletionFunc("network", c.Completion.Network)) | ||
| genericcli.Must(cmd.RegisterFlagCompletionFunc("machine", c.Completion.Machine)) | ||
| genericcli.Must(cmd.RegisterFlagCompletionFunc("addressfamily", c.Completion.AddressFamily)) | ||
| genericcli.Must(cmd.RegisterFlagCompletionFunc("type", c.Completion.IPType)) | ||
|
|
||
| }, | ||
| ValidArgsFn: c.Completion.Ip, | ||
| } | ||
|
|
||
| return genericcli.NewCmds(cmdsConfig) | ||
| } | ||
|
|
||
| func (c *ip) Create(_ any) (*apiv2.IP, error) { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| func (c *ip) Delete(id string) (*apiv2.IP, error) { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| func (c *ip) Get(id string) (*apiv2.IP, error) { | ||
| ctx, cancel := c.c.NewRequestContext() | ||
| defer cancel() | ||
|
|
||
| resp, err := c.c.Client.Adminv2().IP().List(ctx, &adminv2.IPServiceListRequest{ | ||
| Query: &apiv2.IPQuery{ | ||
| Ip: &id, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| switch len(resp.Ips) { | ||
| case 0: | ||
| return nil, fmt.Errorf("no ip found for ip:%s", id) | ||
| case 1: | ||
| return resp.Ips[0], nil | ||
| default: | ||
| return nil, fmt.Errorf("more than one ip found for ip:%s", id) | ||
| } | ||
| } | ||
|
|
||
| func (c *ip) List() ([]*apiv2.IP, error) { | ||
| ctx, cancel := c.c.NewRequestContext() | ||
| defer cancel() | ||
|
|
||
| var labels *apiv2.Labels | ||
| if labelSlice := viper.GetStringSlice("labels"); len(labelSlice) > 0 { | ||
| var err error | ||
|
|
||
| labels, err = helpers.LabelsFromSlice(labelSlice) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| resp, err := c.c.Client.Adminv2().IP().List(ctx, &adminv2.IPServiceListRequest{ | ||
| Query: &apiv2.IPQuery{ | ||
| Ip: pointer.PointerOrNil(viper.GetString("ip")), | ||
| Uuid: pointer.PointerOrNil(viper.GetString("uuid")), | ||
| Network: pointer.PointerOrNil(viper.GetString("network")), | ||
| Project: pointer.PointerOrNil(viper.GetString("project")), | ||
| Name: pointer.PointerOrNil(viper.GetString("name")), | ||
| Machine: pointer.PointerOrNil(viper.GetString("machine")), | ||
| ParentPrefixCidr: pointer.PointerOrNil(viper.GetString("parent-prefix")), | ||
| Labels: labels, | ||
| Type: helpers.IPTypeToType(viper.GetString("type")), | ||
| AddressFamily: helpers.IPAddressFamilyToType(viper.GetString("addressfamily")), | ||
| Namespace: pointer.PointerOrNil(viper.GetString("namespace")), | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return resp.Ips, nil | ||
| } | ||
|
|
||
| func (c *ip) Update(_ any) (*apiv2.IP, error) { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| func (c *ip) Convert(r *apiv2.IP) (string, any, any, error) { | ||
| panic("unimplemented") | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| package completion | ||
|
|
||
| import ( | ||
| adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func (c *Completion) Machine(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| req := &adminv2.MachineServiceListRequest{} | ||
| resp, err := c.Client.Adminv2().Machine().List(cmd.Context(), req) | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
| var names []string | ||
| for _, m := range resp.Machines { | ||
| var hostname string | ||
| if m.Allocation != nil { | ||
| hostname = m.Allocation.Hostname | ||
| names = append(names, m.Uuid+"\t"+hostname) | ||
| } else { | ||
| names = append(names, m.Uuid) | ||
| } | ||
| } | ||
| return names, cobra.ShellCompDirectiveNoFileComp | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to wrap this into an own PR. Makes it much easier to review.