Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
  •  
  •  
  •  
16 changes: 8 additions & 8 deletions internal/cmd/affinity-groups/affinity-groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import (
"github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/delete"
"github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/describe"
"github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/list"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)

func NewCmd(p *print.Printer) *cobra.Command {
func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "affinity-group",
Short: "Manage server affinity groups",
Long: "Manage the lifecycle of server affinity groups.",
Args: args.NoArgs,
Run: utils.CmdHelp,
}
addSubcommands(cmd, p)
addSubcommands(cmd, params)
return cmd
}

func addSubcommands(cmd *cobra.Command, p *print.Printer) {
func addSubcommands(cmd *cobra.Command, params *params.CmdParams) {
cmd.AddCommand(
create.NewCmd(p),
delete.NewCmd(p),
describe.NewCmd(p),
list.NewCmd(p),
create.NewCmd(params),
delete.NewCmd(params),
describe.NewCmd(params),
list.NewCmd(params),
)
}
11 changes: 6 additions & 5 deletions internal/cmd/affinity-groups/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand All @@ -29,7 +30,7 @@ type inputModel struct {
Policy string
}

func NewCmd(p *print.Printer) *cobra.Command {
func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates an affinity groups",
Expand All @@ -43,20 +44,20 @@ func NewCmd(p *print.Printer) *cobra.Command {
),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd)
model, err := parseInput(params.Printer, cmd)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
apiClient, err := client.ConfigureClient(params.Printer)
if err != nil {
return err
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create the affinity group %q?", model.Name)
err = p.PromptForConfirmation(prompt)
err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
Expand All @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create affinity group: %w", err)
}
if resp := result; resp != nil {
return outputResult(p, *model, *resp)
return outputResult(params.Printer, *model, *resp)
}
return fmt.Errorf("create affinity group: nil result")
},
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/affinity-groups/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"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/utils"
Expand Down Expand Up @@ -120,7 +121,7 @@ func TestParseInput(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
cmd := NewCmd(p)
cmd := NewCmd(&params.CmdParams{Printer: p})
if err := globalflags.Configure(cmd.Flags()); err != nil {
t.Fatalf("configure global flags: %v", err)
}
Expand Down Expand Up @@ -212,7 +213,7 @@ func TestOutputResult(t *testing.T) {
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := outputResult(p, tt.model, tt.response)
Expand Down
17 changes: 9 additions & 8 deletions internal/cmd/affinity-groups/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand All @@ -26,7 +27,7 @@ const (
affinityGroupIdArg = "AFFINITY_GROUP"
)

func NewCmd(p *print.Printer) *cobra.Command {
func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("delete %s", affinityGroupIdArg),
Short: "Deletes an affinity group",
Expand All @@ -40,32 +41,32 @@ func NewCmd(p *print.Printer) *cobra.Command {
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd, args)
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
apiClient, err := client.ConfigureClient(params.Printer)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, p, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
if err != nil {
p.Debug(print.ErrorLevel, "get project name: %v", err)
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
}

affinityGroupLabel, err := iaasUtils.GetAffinityGroupName(ctx, apiClient, model.ProjectId, model.AffinityGroupId)
if err != nil {
p.Debug(print.ErrorLevel, "get affinity group name: %v", err)
params.Printer.Debug(print.ErrorLevel, "get affinity group name: %v", err)
affinityGroupLabel = model.AffinityGroupId
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to delete affinity group %q?", affinityGroupLabel)
err = p.PromptForConfirmation(prompt)
err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
Expand All @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
return fmt.Errorf("delete affinity group: %w", err)
}
p.Info("Deleted affinity group %q for %q\n", affinityGroupLabel, projectLabel)
params.Printer.Info("Deleted affinity group %q for %q\n", affinityGroupLabel, projectLabel)

return nil
},
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/affinity-groups/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"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-sdk-go/services/iaas"
Expand Down Expand Up @@ -97,7 +98,7 @@ func TestParseInput(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
cmd := NewCmd(p)
cmd := NewCmd(&params.CmdParams{Printer: p})
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flags: %v", err)
Expand Down
9 changes: 5 additions & 4 deletions internal/cmd/affinity-groups/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand All @@ -27,7 +28,7 @@ const (
affinityGroupId = "AFFINITY_GROUP_ID"
)

func NewCmd(p *print.Printer) *cobra.Command {
func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("describe %s", affinityGroupId),
Short: "Show details of an affinity group",
Expand All @@ -41,13 +42,13 @@ func NewCmd(p *print.Printer) *cobra.Command {
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd, args)
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
apiClient, err := client.ConfigureClient(params.Printer)
if err != nil {
return err
}
Expand All @@ -59,7 +60,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("get affinity group: %w", err)
}

if err := outputResult(p, *model, *result); err != nil {
if err := outputResult(params.Printer, *model, *result); err != nil {
return err
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/affinity-groups/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"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-sdk-go/services/iaas"
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestParseInput(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
cmd := NewCmd(p)
cmd := NewCmd(&params.CmdParams{Printer: p})
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flags: %v", err)
Expand Down Expand Up @@ -191,7 +192,7 @@ func TestOutputResult(t *testing.T) {
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
p.Cmd = NewCmd(&params.CmdParams{Printer: p})

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
Expand Down
11 changes: 6 additions & 5 deletions internal/cmd/affinity-groups/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/goccy/go-yaml"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

Expand All @@ -27,7 +28,7 @@ type inputModel struct {

const limitFlag = "limit"

func NewCmd(p *print.Printer) *cobra.Command {
func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists affinity groups",
Expand All @@ -45,13 +46,13 @@ func NewCmd(p *print.Printer) *cobra.Command {
),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd)
model, err := parseInput(params.Printer, cmd)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
apiClient, err := client.ConfigureClient(params.Printer)
if err != nil {
return err
}
Expand All @@ -67,10 +68,10 @@ func NewCmd(p *print.Printer) *cobra.Command {
if model.Limit != nil && len(*items) > int(*model.Limit) {
*items = (*items)[:*model.Limit]
}
return outputResult(p, *model, *items)
return outputResult(params.Printer, *model, *items)
}

p.Outputln("No affinity groups found")
params.Printer.Outputln("No affinity groups found")
return nil
},
}
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/affinity-groups/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"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/utils"
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestParseInput(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
cmd := NewCmd(p)
cmd := NewCmd(&params.CmdParams{Printer: p})
if err := globalflags.Configure(cmd.Flags()); err != nil {
t.Fatalf("configure global flags: %v", err)
}
Expand Down Expand Up @@ -186,7 +187,7 @@ func TestOutputResult(t *testing.T) {
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := outputResult(p, tt.model, tt.response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/spf13/viper"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
Expand Down Expand Up @@ -32,7 +33,7 @@ type inputModel struct {
OnlyPrintAccessToken bool
}

func NewCmd(p *print.Printer) *cobra.Command {
func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "activate-service-account",
Short: "Authenticates using a service account",
Expand All @@ -58,7 +59,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
),
),
RunE: func(cmd *cobra.Command, _ []string) error {
model := parseInput(p, cmd)
model := parseInput(params.Printer, cmd)

tokenCustomEndpoint := viper.GetString(config.TokenCustomEndpointKey)
if !model.OnlyPrintAccessToken {
Expand All @@ -78,12 +79,12 @@ func NewCmd(p *print.Printer) *cobra.Command {
// Initializes the authentication flow
rt, err := sdkAuth.SetupAuth(cfg)
if err != nil {
p.Debug(print.ErrorLevel, "setup auth: %v", err)
params.Printer.Debug(print.ErrorLevel, "setup auth: %v", err)
return &cliErr.ActivateServiceAccountError{}
}

// Authenticates the service account and stores credentials
email, accessToken, err := auth.AuthenticateServiceAccount(p, rt, model.OnlyPrintAccessToken)
email, accessToken, err := auth.AuthenticateServiceAccount(params.Printer, rt, model.OnlyPrintAccessToken)
if err != nil {
var activateServiceAccountError *cliErr.ActivateServiceAccountError
if !errors.As(err, &activateServiceAccountError) {
Expand All @@ -94,9 +95,9 @@ func NewCmd(p *print.Printer) *cobra.Command {

if model.OnlyPrintAccessToken {
// Only output is the access token
p.Outputf("%s\n", accessToken)
params.Printer.Outputf("%s\n", accessToken)
} else {
p.Outputf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email)
params.Printer.Outputf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email)
}
return nil
},
Expand Down
Loading