Skip to content

Commit a41fa44

Browse files
authored
Support for adding custom UserAgent ID (#181)
1 parent 85a7881 commit a41fa44

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Master
44

5+
- Support environment variable `ALIBABA_CLOUD_VENDOR`
6+
> Set environment variables `ALIBABA_CLOUD_VENDOR` to add a custom UserAgent ID
57
- Update API meta data, and update endpoint pattern of product cr
68
- Update gosdk dependence
79
- Change logic of flag `--pager`

config/hello.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package config
55

66
import (
77
"fmt"
8+
"os"
9+
810
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
911
"github.com/aliyun/aliyun-cli/cli"
1012
)
@@ -24,6 +26,9 @@ func GetRegions(ctx *cli.Context, profile *Profile) ([]Region, error) {
2426

2527
request := ecs.CreateDescribeRegionsRequest()
2628
response := ecs.CreateDescribeRegionsResponse()
29+
if vendorEnv, ok := os.LookupEnv("ALIBABA_CLOUD_VENDOR"); ok {
30+
client.AppendUserAgent("vendor", vendorEnv)
31+
}
2732
client.AppendUserAgent("Aliyun-CLI", cli.GetVersion())
2833
err = client.DoAction(request, response)
2934

@@ -53,6 +58,9 @@ func DoHello(ctx *cli.Context, profile *Profile) {
5358
}
5459
request := ecs.CreateDescribeRegionsRequest()
5560
response := ecs.CreateDescribeRegionsResponse()
61+
if vendorEnv, ok := os.LookupEnv("ALIBABA_CLOUD_VENDOR"); ok {
62+
client.AppendUserAgent("vendor", vendorEnv)
63+
}
5664
client.AppendUserAgent("Aliyun-CLI", cli.GetVersion())
5765
err = client.DoAction(request, response)
5866

config/hello_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package config
55

66
import (
77
"bytes"
8+
"os"
9+
"strings"
810
"testing"
911

1012
"github.com/stretchr/testify/assert"
@@ -30,6 +32,8 @@ func TestGetRegions(t *testing.T) {
3032
}
3133

3234
func TestDoHello(t *testing.T) {
35+
os.Setenv("ALIBABA_CLOUD_VENDOR", "cli_test_VendorTest")
36+
3337
w := new(bytes.Buffer)
3438
ctx := cli.NewCommandContext(w)
3539
ctx.Flags().AddByName("skip-secure-verify")
@@ -46,6 +50,7 @@ func TestDoHello(t *testing.T) {
4650
assert.Equal(t, exw, w.String())
4751

4852
w.Reset()
53+
os.Setenv("DEBUG", "sdk")
4954
profile.AccessKeyId = "AccessKeyId"
5055
profile.AccessKeySecret = "AccessKeySecret"
5156
profile.RegionId = "cn-hangzhou"
@@ -55,4 +60,5 @@ func TestDoHello(t *testing.T) {
5560

5661
}()
5762
DoHello(ctx, &profile)
63+
assert.True(t, strings.Contains(w.String(), "vender/cli_test_VendorTest"))
5864
}

openapi/invoker.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ package openapi
55

66
import (
77
"fmt"
8+
"os"
9+
"strings"
10+
811
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
912
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
1013
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
1114
"github.com/aliyun/aliyun-cli/cli"
1215
"github.com/aliyun/aliyun-cli/config"
1316
"github.com/aliyun/aliyun-cli/i18n"
1417
"github.com/aliyun/aliyun-cli/meta"
15-
"strings"
1618
)
1719

1820
//
@@ -63,7 +65,9 @@ func (a *BasicInvoker) Init(ctx *cli.Context, product *meta.Product) error {
6365
if err != nil {
6466
return fmt.Errorf("init client failed %s", err)
6567
}
66-
68+
if vendorEnv, ok := os.LookupEnv("ALIBABA_CLOUD_VENDOR"); ok {
69+
a.client.AppendUserAgent("vendor", vendorEnv)
70+
}
6771
a.client.AppendUserAgent("Aliyun-CLI", cli.GetVersion())
6872
a.request = requests.NewCommonRequest()
6973
a.request.Product = product.Code

openapi/invoker_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package openapi
22

33
import (
4-
"bufio"
4+
"bytes"
5+
"testing"
6+
57
"github.com/aliyun/aliyun-cli/cli"
68
"github.com/aliyun/aliyun-cli/config"
79
"github.com/aliyun/aliyun-cli/meta"
810
"github.com/stretchr/testify/assert"
9-
"testing"
1011
)
1112

1213
func TestBasicInvoker_Init(t *testing.T) {
@@ -20,7 +21,7 @@ func TestBasicInvoker_Init(t *testing.T) {
2021
req := invooker.getRequest()
2122
assert.Nil(t, req)
2223

23-
w := new(bufio.Writer)
24+
w := new(bytes.Buffer)
2425
ctx := cli.NewCommandContext(w)
2526

2627
regionflag := config.NewRegionFlag()

0 commit comments

Comments
 (0)