Skip to content

Commit 4f78047

Browse files
authored
feat(cli): set custom user-agent header for STACKIT API calls (#741)
relates to STACKITCLI-180
1 parent 2cac307 commit 4f78047

File tree

382 files changed

+724
-635
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

382 files changed

+724
-635
lines changed

.github/docs/contribution-guide/client.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
package client
22

33
import (
4-
"github.com/spf13/cobra"
54
"github.com/spf13/viper"
65
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
6+
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
77
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
10+
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
811
"github.com/stackitcloud/stackit-sdk-go/services/foo"
912
// (...)
1013
)
1114

12-
func ConfigureClient(cmd *cobra.Command) (*foo.APIClient, error) {
13-
var err error
14-
var apiClient foo.APIClient
15-
var cfgOptions []sdkConfig.ConfigurationOption
16-
17-
authCfgOption, err := auth.AuthenticationConfig(cmd, auth.AuthorizeUser)
15+
func ConfigureClient(p *print.Printer, cliVersion string) (*foo.APIClient, error) {
16+
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
1817
if err != nil {
1918
return nil, &errors.AuthError{}
2019
}
21-
cfgOptions = append(cfgOptions, authCfgOption, sdkConfig.WithRegion("eu01")) // Configuring region is needed if "foo" is a regional API
20+
21+
region := viper.GetString(config.RegionKey)
22+
cfgOptions := []sdkConfig.ConfigurationOption{
23+
utils.UserAgentConfigOption(cliVersion),
24+
sdkConfig.WithRegion(region), // Configuring region is needed if "foo" is a regional API
25+
authCfgOption,
26+
}
2227

2328
customEndpoint := viper.GetString(config.fooCustomEndpointKey)
2429

2530
if customEndpoint != "" {
2631
cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint))
2732
}
2833

29-
apiClient, err = foo.NewAPIClient(cfgOptions...)
34+
if p.IsVerbosityDebug() {
35+
cfgOptions = append(cfgOptions,
36+
sdkConfig.WithMiddleware(print.RequestResponseCapturer(p, nil)),
37+
)
38+
}
39+
40+
apiClient, err := foo.NewAPIClient(cfgOptions...)
3041
if err != nil {
42+
p.Debug(print.ErrorLevel, "create new API client: %v", err)
3143
return nil, &errors.AuthError{}
3244
}
3345

.github/docs/contribution-guide/cmd.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
1313
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1617
"github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client"
1718
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
@@ -54,7 +55,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5455
}
5556

5657
// Configure API client
57-
apiClient, err := client.ConfigureClient(params.Printer, cmd)
58+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5859
if err != nil {
5960
return err
6061
}
@@ -66,7 +67,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6667
return fmt.Errorf("(...): %w", err)
6768
}
6869

69-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
70+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
7071
if err != nil {
7172
projectLabel = model.ProjectId
7273
}
@@ -86,22 +87,22 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8687

8788
// Configure command flags (type, default value, and description)
8889
func configureFlags(cmd *cobra.Command) {
89-
cmd.Flags().StringP(myFlag, "defaultValue", "My flag description")
90+
cmd.Flags().StringP(someFlag, "shorthand", "defaultValue", "My flag description")
9091
}
9192

9293
// Parse user input (arguments and/or flags)
9394
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
9495
myArg := inputArgs[0]
9596

96-
globalFlags := globalflags.Parse(cmd)
97+
globalFlags := globalflags.Parse(p, cmd)
9798
if globalFlags.ProjectId == "" {
9899
return nil, &errors.ProjectIdError{}
99100
}
100101

101102
model := inputModel{
102103
GlobalFlagModel: globalFlags,
103104
MyArg: myArg,
104-
MyFlag: flags.FlagToStringPointer(cmd, myFlag),
105+
MyFlag: flags.FlagToStringPointer(p, cmd, someFlag),
105106
}
106107

107108
// Write the input model to the debug logs
@@ -119,7 +120,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
119120

120121
// Build request to the API
121122
func buildRequest(ctx context.Context, model *inputModel, apiClient *foo.APIClient) foo.ApiListInstancesRequest {
122-
req := apiClient.GetBar(ctx, model.ProjectId, model.MyArg, someParam)
123+
req := apiClient.GetBar(ctx, model.ProjectId, model.MyArg, someArg)
123124
return req
124125
}
125126

@@ -147,7 +148,7 @@ func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, res
147148
resource := resources[i]
148149
table.AddRow(*resource.ResourceId, *resource.Name, *resource.State)
149150
}
150-
err := table.Display(cmd)
151+
err := table.Display(p)
151152
if err != nil {
152153
return fmt.Errorf("render table: %w", err)
153154
}

CONTRIBUTION.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Please remember to run `make generate-docs` after your changes to keep the comma
5353

5454
Below is a typical structure of a CLI command:
5555

56-
https://github.com/stackitcloud/stackit-cli/blob/6f762bd56407ed232080efabc4d2bf87f260b71d/.github/docs/contribution-guide/cmd.go#L23-L156
56+
https://github.com/stackitcloud/stackit-cli/blob/85ce44cd18d11169f2548d8657031b5fc6f94740/.github/docs/contribution-guide/cmd.go#L23-L156
5757

5858
Please remember to always add unit tests for `parseInput`, `buildRequest` (in `bar_test.go`), and any other util functions used.
5959

@@ -83,7 +83,7 @@ If you want to add a command that uses a STACKIT service `foo` that was not yet
8383
1. This is done in `internal/pkg/services/foo/client/client.go`
8484
2. Below is an example of a typical `client.go` file structure:
8585

86-
https://github.com/stackitcloud/stackit-cli/blob/6f762bd56407ed232080efabc4d2bf87f260b71d/.github/docs/contribution-guide/client.go#L12-L35
86+
https://github.com/stackitcloud/stackit-cli/blob/85ce44cd18d11169f2548d8657031b5fc6f94740/.github/docs/contribution-guide/client.go#L12-L35
8787

8888
### Local development
8989

internal/cmd/affinity-groups/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5050
}
5151

5252
// Configure API client
53-
apiClient, err := client.ConfigureClient(params.Printer)
53+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5454
if err != nil {
5555
return err
5656
}

internal/cmd/affinity-groups/delete/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4747
}
4848

4949
// Configure API client
50-
apiClient, err := client.ConfigureClient(params.Printer)
50+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5151
if err != nil {
5252
return err
5353
}
5454

55-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
55+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
5656
if err != nil {
5757
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
5858
projectLabel = model.ProjectId

internal/cmd/affinity-groups/describe/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4848
}
4949

5050
// Configure API client
51-
apiClient, err := client.ConfigureClient(params.Printer)
51+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5252
if err != nil {
5353
return err
5454
}

internal/cmd/affinity-groups/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5252
}
5353

5454
// Configure API client
55-
apiClient, err := client.ConfigureClient(params.Printer)
55+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5656
if err != nil {
5757
return err
5858
}

internal/cmd/beta/alb/create/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5454
}
5555

5656
// Configure API client
57-
apiClient, err := client.ConfigureClient(params.Printer)
57+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5858
if err != nil {
5959
return err
6060
}
6161

62-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
62+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
6363
if err != nil {
6464
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
6565
projectLabel = model.ProjectId

internal/cmd/beta/alb/delete/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4545
}
4646

4747
// Configure API client
48-
apiClient, err := client.ConfigureClient(params.Printer)
48+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
4949
if err != nil {
5050
return err
5151
}
5252

53-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
53+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
5454
if err != nil {
5555
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
5656
projectLabel = model.ProjectId

internal/cmd/beta/alb/describe/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5050
}
5151

5252
// Configure API client
53-
apiClient, err := client.ConfigureClient(params.Printer)
53+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5454
if err != nil {
5555
return err
5656
}

0 commit comments

Comments
 (0)