Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions internal/cmd/affinity-groups/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
"$ stackit affinity-group create --name AFFINITY_GROUP_NAME --policy soft-affinity",
),
),
RunE: func(cmd *cobra.Command, _ []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd)
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func buildRequest(ctx context.Context, model inputModel, apiClient *iaas.APIClie
return req
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
Expand Down
40 changes: 3 additions & 37 deletions internal/cmd/affinity-groups/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
)
Expand Down Expand Up @@ -80,6 +81,7 @@ func fixturePayload(mods ...func(payload *iaas.CreateAffinityGroupPayload)) iaas
func TestParseInput(t *testing.T) {
tests := []struct {
description string
argValues []string
flagValues map[string]string
isValid bool
expectedModel *inputModel
Expand Down Expand Up @@ -120,43 +122,7 @@ func TestParseInput(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
cmd := NewCmd(&params.CmdParams{Printer: p})
if err := globalflags.Configure(cmd.Flags()); err != nil {
t.Fatalf("configure global flags: %v", err)
}

for flag, value := range tt.flagValues {
if err := cmd.Flags().Set(flag, value); err != nil {
if !tt.isValid {
return
}
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
}
}

if err := cmd.ValidateRequiredFlags(); err != nil {
if !tt.isValid {
return
}
t.Fatalf("error validating flags: %v", err)
}

model, err := parseInput(p, cmd)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("error parsing flags: %v", err)
}

if !tt.isValid {
t.Fatalf("did not fail on invalid input")
}
diff := cmp.Diff(model, tt.expectedModel)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/affinity-groups/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
"$ stackit affinity-group list --limit=10",
),
),
RunE: func(cmd *cobra.Command, _ []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd)
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func buildRequest(ctx context.Context, model inputModel, apiClient *iaas.APIClie
return apiClient.ListAffinityGroups(ctx, model.ProjectId)
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
Expand Down
40 changes: 3 additions & 37 deletions internal/cmd/affinity-groups/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
)
Expand Down Expand Up @@ -63,6 +64,7 @@ func fixtureRequest(mods ...func(request *iaas.ApiListAffinityGroupsRequest)) ia
func TestParseInput(t *testing.T) {
tests := []struct {
description string
argValues []string
flagValues map[string]string
isValid bool
expectedModel *inputModel
Expand Down Expand Up @@ -105,43 +107,7 @@ func TestParseInput(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
cmd := NewCmd(&params.CmdParams{Printer: p})
if err := globalflags.Configure(cmd.Flags()); err != nil {
t.Fatalf("configure global flags: %v", err)
}

for flag, value := range tt.flagValues {
if err := cmd.Flags().Set(flag, value); err != nil {
if !tt.isValid {
return
}
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
}
}

if err := cmd.ValidateRequiredFlags(); err != nil {
if !tt.isValid {
return
}
t.Fatalf("error validating flags: %v", err)
}

model, err := parseInput(p, cmd)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("error parsing flags: %v", err)
}

if !tt.isValid {
t.Fatalf("did not fail on invalid input")
}
diff := cmp.Diff(model, tt.expectedModel)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
"$ stackit auth activate-service-account --service-account-token my-service-account-token --only-print-access-token",
),
),
RunE: func(cmd *cobra.Command, _ []string) error {
model := parseInput(params.Printer, cmd)
RunE: func(cmd *cobra.Command, args []string) error {
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}

tokenCustomEndpoint := viper.GetString(config.TokenCustomEndpointKey)
if !model.OnlyPrintAccessToken {
Expand Down Expand Up @@ -113,7 +116,7 @@ func configureFlags(cmd *cobra.Command) {
cmd.Flags().Bool(onlyPrintAccessTokenFlag, false, "If this is set to true the credentials are not stored in either the keyring or a file")
}

func parseInput(p *print.Printer, cmd *cobra.Command) *inputModel {
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
model := inputModel{
ServiceAccountToken: flags.FlagToStringValue(p, cmd, serviceAccountTokenFlag),
ServiceAccountKeyPath: flags.FlagToStringValue(p, cmd, serviceAccountKeyPathFlag),
Expand All @@ -122,7 +125,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) *inputModel {
}

p.DebugInputModel(model)
return &model
return &model, nil
}

func storeCustomEndpoint(tokenCustomEndpoint string) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package activateserviceaccount
import (
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"

"github.com/spf13/viper"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/zalando/go-keyring"

"github.com/google/go-cmp/cmp"
)

var testTokenCustomEndpoint = "token_url"
Expand Down Expand Up @@ -45,6 +42,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
func TestParseInput(t *testing.T) {
tests := []struct {
description string
argValues []string
flagValues map[string]string
tokenCustomEndpoint string
isValid bool
Expand Down Expand Up @@ -106,32 +104,7 @@ func TestParseInput(t *testing.T) {

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
cmd := NewCmd(&params.CmdParams{Printer: p})
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flags: %v", err)
}

for flag, value := range tt.flagValues {
err := cmd.Flags().Set(flag, value)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
}
}

model := parseInput(p, cmd)

if !tt.isValid {
t.Fatalf("did not fail on invalid input")
}
diff := cmp.Diff(model, tt.expectedModel)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/auth/get-access-token/get_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
`Print a short-lived access token`,
"$ stackit auth get-access-token"),
),
RunE: func(cmd *cobra.Command, _ []string) error {
model, err := parseInput(params.Printer, cmd)
RunE: func(cmd *cobra.Command, args []string) error {
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
return cmd
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)

model := inputModel{
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/beta/alb/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
`Create an application loadbalancer from a configuration file`,
"$ stackit beta alb create --configuration my-loadbalancer.json"),
),
RunE: func(cmd *cobra.Command, _ []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd)
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func configureFlags(cmd *cobra.Command) {
cobra.CheckErr(err)
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
Expand Down
43 changes: 3 additions & 40 deletions internal/cmd/beta/alb/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/alb"
)
Expand Down Expand Up @@ -79,6 +80,7 @@ func fixtureRequest(mods ...func(request *alb.ApiCreateLoadBalancerRequest)) alb
func TestParseInput(t *testing.T) {
tests := []struct {
description string
argValues []string
flagValues map[string]string
isValid bool
expectedModel *inputModel
Expand Down Expand Up @@ -134,46 +136,7 @@ func TestParseInput(t *testing.T) {

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
cmd := NewCmd(&params.CmdParams{Printer: p})
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flags: %v", err)
}

for flag, value := range tt.flagValues {
err := cmd.Flags().Set(flag, value)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
}
}

err = cmd.ValidateRequiredFlags()
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("error validating flags: %v", err)
}

model, err := parseInput(p, cmd)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("error parsing flags: %v", err)
}

if !tt.isValid {
t.Fatalf("did not fail on invalid input")
}
diff := cmp.Diff(model, tt.expectedModel)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
})
}
}
Expand Down
Loading
Loading