Skip to content

Commit d462b08

Browse files
committed
Update auth api commands to use types.CmdParams
Migrate from internal/cmd/params to internal/pkg/types package after upstream rename. Also remove unused imports and apply cobra.CheckErr fix to status command.
1 parent c2a8b0f commit d462b08

File tree

5 files changed

+28
-34
lines changed

5 files changed

+28
-34
lines changed

internal/cmd/auth/api/get-access-token/get_access_token.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
package getaccesstoken
22

33
import (
4-
"encoding/json"
5-
"fmt"
6-
74
"github.com/spf13/cobra"
8-
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
95
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
106
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
117
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
128
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
139
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1410
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1512
)
1613

1714
type inputModel struct {
1815
*globalflags.GlobalFlagModel
1916
}
2017

21-
func NewCmd(params *params.CmdParams) *cobra.Command {
18+
func NewCmd(p *types.CmdParams) *cobra.Command {
2219
cmd := &cobra.Command{
2320
Use: "get-access-token",
2421
Short: "Prints a short-lived access token for the STACKIT Terraform Provider and SDK",
@@ -30,7 +27,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
3027
"$ stackit auth api get-access-token"),
3128
),
3229
RunE: func(cmd *cobra.Command, args []string) error {
33-
model, err := parseInput(params.Printer, cmd, args)
30+
model, err := parseInput(p.Printer, cmd, args)
3431
if err != nil {
3532
return err
3633
}
@@ -43,17 +40,17 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4340
return &cliErr.SessionExpiredError{}
4441
}
4542

46-
accessToken, err := auth.GetValidAccessTokenWithContext(params.Printer, auth.StorageContextAPI)
43+
accessToken, err := auth.GetValidAccessTokenWithContext(p.Printer, auth.StorageContextAPI)
4744
if err != nil {
48-
params.Printer.Debug(print.ErrorLevel, "get valid access token: %v", err)
45+
p.Printer.Debug(print.ErrorLevel, "get valid access token: %v", err)
4946
return &cliErr.SessionExpiredError{}
5047
}
5148

5249
result := map[string]string{
5350
"access_token": accessToken,
5451
}
55-
return params.Printer.OutputResult(model.OutputFormat, result, func() error {
56-
params.Printer.Outputln(accessToken)
52+
return p.Printer.OutputResult(model.OutputFormat, result, func() error {
53+
p.Printer.Outputln(accessToken)
5754
return nil
5855
})
5956
},

internal/cmd/auth/api/login/login.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"fmt"
55

66
"github.com/spf13/cobra"
7-
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
87
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
98
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
109
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1111
)
1212

13-
func NewCmd(params *params.CmdParams) *cobra.Command {
13+
func NewCmd(p *types.CmdParams) *cobra.Command {
1414
cmd := &cobra.Command{
1515
Use: "login",
1616
Short: "Logs in for the STACKIT Terraform Provider and SDK",
@@ -25,12 +25,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
2525
"$ stackit auth api login"),
2626
),
2727
RunE: func(_ *cobra.Command, _ []string) error {
28-
err := auth.AuthorizeUser(params.Printer, auth.StorageContextAPI, false)
28+
err := auth.AuthorizeUser(p.Printer, auth.StorageContextAPI, false)
2929
if err != nil {
3030
return fmt.Errorf("authorization failed: %w", err)
3131
}
3232

33-
params.Printer.Outputln("Successfully logged in for STACKIT Terraform Provider and SDK.\n")
33+
p.Printer.Outputln("Successfully logged in for STACKIT Terraform Provider and SDK.\n")
3434

3535
return nil
3636
},

internal/cmd/auth/api/logout/logout.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"fmt"
55

66
"github.com/spf13/cobra"
7-
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
87
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
98
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
109
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1111
)
1212

13-
func NewCmd(params *params.CmdParams) *cobra.Command {
13+
func NewCmd(p *types.CmdParams) *cobra.Command {
1414
cmd := &cobra.Command{
1515
Use: "logout",
1616
Short: "Logs out from the STACKIT Terraform Provider and SDK",
@@ -27,7 +27,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
2727
return fmt.Errorf("log out failed: %w", err)
2828
}
2929

30-
params.Printer.Info("Successfully logged out from STACKIT Terraform Provider and SDK.\n")
30+
p.Printer.Info("Successfully logged out from STACKIT Terraform Provider and SDK.\n")
3131
return nil
3232
},
3333
}

internal/cmd/auth/api/provider.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"github.com/stackitcloud/stackit-cli/internal/cmd/auth/api/login"
77
"github.com/stackitcloud/stackit-cli/internal/cmd/auth/api/logout"
88
"github.com/stackitcloud/stackit-cli/internal/cmd/auth/api/status"
9-
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
109
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1111
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1212
)
1313

14-
func NewCmd(params *params.CmdParams) *cobra.Command {
14+
func NewCmd(p *types.CmdParams) *cobra.Command {
1515
cmd := &cobra.Command{
1616
Use: "api",
1717
Short: "Manages authentication for the STACKIT Terraform Provider and SDK",
@@ -27,13 +27,13 @@ Tokens are stored in the OS keychain with a fallback to local storage.`,
2727
Args: args.NoArgs,
2828
Run: utils.CmdHelp,
2929
}
30-
addSubcommands(cmd, params)
30+
addSubcommands(cmd, p)
3131
return cmd
3232
}
3333

34-
func addSubcommands(cmd *cobra.Command, params *params.CmdParams) {
35-
cmd.AddCommand(login.NewCmd(params))
36-
cmd.AddCommand(logout.NewCmd(params))
37-
cmd.AddCommand(getaccesstoken.NewCmd(params))
38-
cmd.AddCommand(status.NewCmd(params))
34+
func addSubcommands(cmd *cobra.Command, p *types.CmdParams) {
35+
cmd.AddCommand(login.NewCmd(p))
36+
cmd.AddCommand(logout.NewCmd(p))
37+
cmd.AddCommand(getaccesstoken.NewCmd(p))
38+
cmd.AddCommand(status.NewCmd(p))
3939
}

internal/cmd/auth/api/status/status.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package status
22

33
import (
4-
"encoding/json"
5-
"fmt"
6-
74
"github.com/spf13/cobra"
8-
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
95
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
106
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
117
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
128
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
139
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1411
)
1512

1613
type inputModel struct {
@@ -23,7 +20,7 @@ type statusOutput struct {
2320
AuthFlow string `json:"auth_flow,omitempty"`
2421
}
2522

26-
func NewCmd(params *params.CmdParams) *cobra.Command {
23+
func NewCmd(p *types.CmdParams) *cobra.Command {
2724
cmd := &cobra.Command{
2825
Use: "status",
2926
Short: "Shows authentication status for the STACKIT Terraform Provider and SDK",
@@ -35,7 +32,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
3532
"$ stackit auth api status"),
3633
),
3734
RunE: func(cmd *cobra.Command, args []string) error {
38-
model, err := parseInput(params.Printer, cmd, args)
35+
model, err := parseInput(p.Printer, cmd, args)
3936
if err != nil {
4037
return err
4138
}
@@ -44,7 +41,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4441
accessToken, err := auth.GetAuthFieldWithContext(auth.StorageContextAPI, auth.ACCESS_TOKEN)
4542
if err != nil || accessToken == "" {
4643
// Not authenticated
47-
return outputStatus(params.Printer, model, statusOutput{
44+
return outputStatus(p.Printer, model, statusOutput{
4845
Authenticated: false,
4946
})
5047
}
@@ -56,7 +53,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5653
email = ""
5754
}
5855

59-
return outputStatus(params.Printer, model, statusOutput{
56+
return outputStatus(p.Printer, model, statusOutput{
6057
Authenticated: true,
6158
Email: email,
6259
AuthFlow: string(flow),
@@ -66,7 +63,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6663

6764
// hide project id flag from help command because it could mislead users
6865
cmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
69-
_ = command.Flags().MarkHidden(globalflags.ProjectIdFlag) // nolint:errcheck // there's no chance to handle the error here
66+
cobra.CheckErr(command.Flags().MarkHidden(globalflags.ProjectIdFlag))
7067
command.Parent().HelpFunc()(command, strings)
7168
})
7269

0 commit comments

Comments
 (0)