diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 0ad5063..719f700 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -21,6 +21,7 @@ jobs: go-build: uses: metal-stack/actions-common/.github/workflows/go-build.yaml@v1 + secrets: inherit with: build: false diff --git a/cmd/api/v2/ip.go b/cmd/api/v2/ip.go index 866a7a6..afe2341 100644 --- a/cmd/api/v2/ip.go +++ b/cmd/api/v2/ip.go @@ -46,6 +46,8 @@ func newIPCmd(c *config.Config) *cobra.Command { cmd.Flags().StringP("addressfamily", "", "", "addressfamily, can be either IPv4|IPv6, defaults to IPv4 (optional)") genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.Project)) + genericcli.Must(cmd.RegisterFlagCompletionFunc("network", c.Completion.Network)) + genericcli.Must(cmd.RegisterFlagCompletionFunc("addressfamily", c.Completion.AddressFamily)) }, UpdateCmdMutateFn: func(cmd *cobra.Command) { cmd.Flags().StringP("project", "p", "", "project of the ip") @@ -88,7 +90,7 @@ func (c *ip) createFromCLI() (*apiv2.IPServiceCreateRequest, error) { Description: pointer.PointerOrNil(viper.GetString("description")), Labels: labels, Type: new(ipStaticToType(viper.GetBool("static"))), - AddressFamily: addressFamilyToType(viper.GetString("addressfamily")), + AddressFamily: helpers.IPAddressFamilyToType(viper.GetString("addressfamily")), }, nil } @@ -257,16 +259,3 @@ func ipStaticToType(b bool) apiv2.IPType { } return apiv2.IPType_IP_TYPE_EPHEMERAL } - -func addressFamilyToType(af string) *apiv2.IPAddressFamily { - switch af { - case "": - return nil - case "ipv4", "IPv4": - return apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_V4.Enum() - case "ipv6", "IPv6": - return apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_V6.Enum() - default: - return apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_UNSPECIFIED.Enum() - } -} diff --git a/cmd/completion/completion.go b/cmd/completion/completion.go index 90d554d..aca23c9 100644 --- a/cmd/completion/completion.go +++ b/cmd/completion/completion.go @@ -6,15 +6,8 @@ import ( ) type Completion struct { - client client.Client - project string -} - -func New(c client.Client, project string) *Completion { - return &Completion{ - client: c, - project: project, - } + Client client.Client + Proj string } func OutputFormat(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { diff --git a/cmd/completion/ip.go b/cmd/completion/ip.go index c8f4b24..3ff3aa0 100644 --- a/cmd/completion/ip.go +++ b/cmd/completion/ip.go @@ -1,15 +1,16 @@ package completion import ( + "github.com/metal-stack/api/go/enum" apiv2 "github.com/metal-stack/api/go/metalstack/api/v2" "github.com/spf13/cobra" ) func (c *Completion) Ip(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { req := &apiv2.IPServiceListRequest{ - Project: c.project, + Project: c.Proj, } - resp, err := c.client.Apiv2().IP().List(cmd.Context(), req) + resp, err := c.Client.Apiv2().IP().List(cmd.Context(), req) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -19,3 +20,17 @@ func (c *Completion) Ip(cmd *cobra.Command, args []string, toComplete string) ([ } return names, cobra.ShellCompDirectiveNoFileComp } +func (c *Completion) AddressFamily(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + var afs []string + for _, af := range []apiv2.IPAddressFamily{ + apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_V4, + apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_V6} { + stringValue, err := enum.GetStringValue(af) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + afs = append(afs, *stringValue) + } + + return afs, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/completion/network.go b/cmd/completion/network.go index 91feaee..c302495 100644 --- a/cmd/completion/network.go +++ b/cmd/completion/network.go @@ -9,15 +9,15 @@ import ( ) func (c *Completion) Network(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - ownNetworks, err := c.client.Apiv2().Network().List(cmd.Context(), &apiv2.NetworkServiceListRequest{ - Project: c.project, + ownNetworks, err := c.Client.Apiv2().Network().List(cmd.Context(), &apiv2.NetworkServiceListRequest{ + Project: c.Proj, }) if err != nil { return nil, cobra.ShellCompDirectiveError } - baseNetworks, err := c.client.Apiv2().Network().ListBaseNetworks(cmd.Context(), &apiv2.NetworkServiceListBaseNetworksRequest{ - Project: c.project, + baseNetworks, err := c.Client.Apiv2().Network().ListBaseNetworks(cmd.Context(), &apiv2.NetworkServiceListBaseNetworksRequest{ + Project: c.Proj, }) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -71,7 +71,7 @@ func (c *Completion) NetworkAddressFamily(cmd *cobra.Command, args []string, toC } func (c *Completion) NetworkAdmin(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - networks, err := c.client.Adminv2().Network().List(cmd.Context(), &adminv2.NetworkServiceListRequest{}) + networks, err := c.Client.Adminv2().Network().List(cmd.Context(), &adminv2.NetworkServiceListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/cmd/completion/partition.go b/cmd/completion/partition.go index af22118..c1b3082 100644 --- a/cmd/completion/partition.go +++ b/cmd/completion/partition.go @@ -7,7 +7,7 @@ import ( func (c *Completion) Partition(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { req := &apiv2.PartitionServiceListRequest{} - resp, err := c.client.Apiv2().Partition().List(cmd.Context(), req) + resp, err := c.Client.Apiv2().Partition().List(cmd.Context(), req) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/cmd/completion/project.go b/cmd/completion/project.go index f5cf734..c4cc89a 100644 --- a/cmd/completion/project.go +++ b/cmd/completion/project.go @@ -8,7 +8,7 @@ import ( func (c *Completion) Project(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { req := &apiv2.ProjectServiceListRequest{} - resp, err := c.client.Apiv2().Project().List(cmd.Context(), req) + resp, err := c.Client.Apiv2().Project().List(cmd.Context(), req) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -36,8 +36,8 @@ func (c *Completion) ProjectRole(cmd *cobra.Command, args []string, toComplete s } func (c *Completion) ProjectInvite(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - resp, err := c.client.Apiv2().Project().InvitesList(cmd.Context(), &apiv2.ProjectServiceInvitesListRequest{ - Project: c.project, + resp, err := c.Client.Apiv2().Project().InvitesList(cmd.Context(), &apiv2.ProjectServiceInvitesListRequest{ + Project: c.Proj, }) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -53,8 +53,8 @@ func (c *Completion) ProjectInvite(cmd *cobra.Command, args []string, toComplete } func (c *Completion) ProjectMember(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - resp, err := c.client.Apiv2().Project().Get(cmd.Context(), &apiv2.ProjectServiceGetRequest{ - Project: c.project, + resp, err := c.Client.Apiv2().Project().Get(cmd.Context(), &apiv2.ProjectServiceGetRequest{ + Project: c.Proj, }) if err != nil { return nil, cobra.ShellCompDirectiveError diff --git a/cmd/completion/size.go b/cmd/completion/size.go index 204f97f..b9b84cb 100644 --- a/cmd/completion/size.go +++ b/cmd/completion/size.go @@ -7,7 +7,7 @@ import ( func (c *Completion) Size(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { req := &apiv2.SizeServiceListRequest{} - resp, err := c.client.Apiv2().Size().List(cmd.Context(), req) + resp, err := c.Client.Apiv2().Size().List(cmd.Context(), req) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/cmd/completion/switch.go b/cmd/completion/switch.go index 9889946..a1e33b4 100644 --- a/cmd/completion/switch.go +++ b/cmd/completion/switch.go @@ -7,7 +7,7 @@ import ( ) func (c *Completion) Switch(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - resp, err := c.client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) + resp, err := c.Client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -21,7 +21,7 @@ func (c *Completion) Switch(cmd *cobra.Command, args []string, toComplete string } func (c *Completion) SwitchPartition(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - resp, err := c.client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) + resp, err := c.Client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -35,7 +35,7 @@ func (c *Completion) SwitchPartition(cmd *cobra.Command, args []string, toComple } func (c *Completion) SwitchRack(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - resp, err := c.client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) + resp, err := c.Client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -49,7 +49,7 @@ func (c *Completion) SwitchRack(cmd *cobra.Command, args []string, toComplete st } func (c *Completion) SwitchOSVendor(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - resp, err := c.client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) + resp, err := c.Client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -63,7 +63,7 @@ func (c *Completion) SwitchOSVendor(cmd *cobra.Command, args []string, toComplet } func (c *Completion) SwitchOSVersion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - resp, err := c.client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) + resp, err := c.Client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -82,7 +82,7 @@ func (c *Completion) SwitchPorts(cmd *cobra.Command, args []string, toComplete s return nil, cobra.ShellCompDirectiveNoFileComp } - resp, err := c.client.Adminv2().Switch().Get(cmd.Context(), &adminv2.SwitchServiceGetRequest{ + resp, err := c.Client.Adminv2().Switch().Get(cmd.Context(), &adminv2.SwitchServiceGetRequest{ Id: args[0], }) if err != nil { diff --git a/cmd/completion/tenant.go b/cmd/completion/tenant.go index e6e8d13..e99e735 100644 --- a/cmd/completion/tenant.go +++ b/cmd/completion/tenant.go @@ -8,7 +8,7 @@ import ( func (c *Completion) Tenant(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { req := &apiv2.TenantServiceListRequest{} - resp, err := c.client.Apiv2().Tenant().List(cmd.Context(), req) + resp, err := c.Client.Apiv2().Tenant().List(cmd.Context(), req) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -35,14 +35,14 @@ func (c *Completion) TenantRole(cmd *cobra.Command, args []string, toComplete st } func (c *Completion) TenantInvite(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - projectResp, err := c.client.Apiv2().Project().Get(cmd.Context(), &apiv2.ProjectServiceGetRequest{ - Project: c.project, + projectResp, err := c.Client.Apiv2().Project().Get(cmd.Context(), &apiv2.ProjectServiceGetRequest{ + Project: c.Proj, }) if err != nil { return nil, cobra.ShellCompDirectiveError } - resp, err := c.client.Apiv2().Tenant().InvitesList(cmd.Context(), &apiv2.TenantServiceInvitesListRequest{ + resp, err := c.Client.Apiv2().Tenant().InvitesList(cmd.Context(), &apiv2.TenantServiceInvitesListRequest{ Login: projectResp.Project.Tenant, }) if err != nil { @@ -59,14 +59,14 @@ func (c *Completion) TenantInvite(cmd *cobra.Command, args []string, toComplete } func (c *Completion) TenantMember(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - projectResp, err := c.client.Apiv2().Project().Get(cmd.Context(), &apiv2.ProjectServiceGetRequest{ - Project: c.project, + projectResp, err := c.Client.Apiv2().Project().Get(cmd.Context(), &apiv2.ProjectServiceGetRequest{ + Project: c.Proj, }) if err != nil { return nil, cobra.ShellCompDirectiveError } - resp, err := c.client.Apiv2().Tenant().Get(cmd.Context(), &apiv2.TenantServiceGetRequest{ + resp, err := c.Client.Apiv2().Tenant().Get(cmd.Context(), &apiv2.TenantServiceGetRequest{ Login: projectResp.Project.Tenant, }) if err != nil { @@ -84,7 +84,7 @@ func (c *Completion) TenantMember(cmd *cobra.Command, args []string, toComplete func (c *Completion) AdminTenant(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { req := &adminv2.TenantServiceListRequest{} - resp, err := c.client.Adminv2().Tenant().List(cmd.Context(), req) + resp, err := c.Client.Adminv2().Tenant().List(cmd.Context(), req) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/cmd/completion/token.go b/cmd/completion/token.go index 9aa49b3..623d170 100644 --- a/cmd/completion/token.go +++ b/cmd/completion/token.go @@ -10,7 +10,7 @@ import ( func (c *Completion) Token(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { req := &apiv2.TokenServiceListRequest{} - resp, err := c.client.Apiv2().Token().List(cmd.Context(), req) + resp, err := c.Client.Apiv2().Token().List(cmd.Context(), req) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -24,7 +24,7 @@ func (c *Completion) Token(cmd *cobra.Command, args []string, toComplete string) } func (c *Completion) TokenProjectRoles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - methods, err := c.client.Apiv2().Method().TokenScopedList(cmd.Context(), &apiv2.MethodServiceTokenScopedListRequest{}) + methods, err := c.Client.Apiv2().Method().TokenScopedList(cmd.Context(), &apiv2.MethodServiceTokenScopedListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -42,7 +42,7 @@ func (c *Completion) TokenProjectRoles(cmd *cobra.Command, args []string, toComp } func (c *Completion) TokenTenantRoles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - methods, err := c.client.Apiv2().Method().TokenScopedList(cmd.Context(), &apiv2.MethodServiceTokenScopedListRequest{}) + methods, err := c.Client.Apiv2().Method().TokenScopedList(cmd.Context(), &apiv2.MethodServiceTokenScopedListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -60,7 +60,7 @@ func (c *Completion) TokenTenantRoles(cmd *cobra.Command, args []string, toCompl } func (c *Completion) TokenMachineRoles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - methods, err := c.client.Apiv2().Method().TokenScopedList(cmd.Context(), &apiv2.MethodServiceTokenScopedListRequest{}) + methods, err := c.Client.Apiv2().Method().TokenScopedList(cmd.Context(), &apiv2.MethodServiceTokenScopedListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -104,7 +104,7 @@ func (c *Completion) TokenInfraRole(cmd *cobra.Command, args []string, toComplet } func (c *Completion) TokenPermissions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - methods, err := c.client.Apiv2().Method().List(cmd.Context(), &apiv2.MethodServiceListRequest{}) + methods, err := c.Client.Apiv2().Method().List(cmd.Context(), &apiv2.MethodServiceListRequest{}) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/cmd/root.go b/cmd/root.go index 8041aae..ae6ea2d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,10 +20,11 @@ import ( func Execute() { cfg := &config.Config{ - Fs: afero.NewOsFs(), - Out: os.Stdout, - PromptOut: os.Stdout, - In: os.Stdin, + Fs: afero.NewOsFs(), + Out: os.Stdout, + PromptOut: os.Stdout, + In: os.Stdin, + Completion: &completion.Completion{}, } cmd := NewRootCmd(cfg) @@ -127,7 +128,8 @@ func initConfigWithViperCtx(c *config.Config) error { } c.Client = mc - c.Completion = completion.New(mc, c.GetProject()) + c.Completion.Client = mc + c.Completion.Proj = c.GetProject() return nil } diff --git a/testing/e2e/test_cmd.go b/testing/e2e/test_cmd.go index 405464f..ba36594 100644 --- a/testing/e2e/test_cmd.go +++ b/testing/e2e/test_cmd.go @@ -57,12 +57,14 @@ func NewRootCmd(t *testing.T, c *TestConfig) e2e_test.NewRootCmdFunc { viper.Reset() return cmd.NewRootCmd(&config.Config{ - Fs: fs, - Out: &out, - In: in, - PromptOut: io.Discard, - Completion: completion.New(cl, ""), - Client: cl, + Fs: fs, + Out: &out, + In: in, + PromptOut: io.Discard, + Completion: &completion.Completion{ + Client: cl, + }, + Client: cl, }), &out } }