Skip to content

Commit ca31463

Browse files
CodeSpaceiiiiyndu13
authored andcommitted
Adjust the parameter priority of oss bridge
1 parent f36184a commit ca31463

File tree

5 files changed

+113
-7
lines changed

5 files changed

+113
-7
lines changed

cli/flag.go

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ type Flag struct {
8686

8787
// return true if flag appeared, either `--flag1` or `--flag1 value1`
8888
func (f *Flag) IsAssigned() bool {
89+
if f == nil {
90+
return false
91+
}
8992
return f.assigned
9093
}
9194

@@ -101,6 +104,9 @@ func (f *Flag) SetValue(value string) {
101104
//
102105
// for `AssignedMode == AssignedRepeatable`. Use GetValues() to get all values
103106
func (f *Flag) GetValue() (string, bool) {
107+
if f == nil {
108+
return "", false
109+
}
104110
if f.IsAssigned() {
105111
return f.value, true
106112
} else if f.Required {

cli/flag_set.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
1+
// Package cli Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -64,8 +64,11 @@ func (fs *FlagSet) AddByName(name string) (*Flag, error) {
6464
return f, nil
6565
}
6666

67-
// get flag by name, sample --name
67+
// Get fetch flag by name, sample --name
6868
func (fs *FlagSet) Get(name string) *Flag {
69+
if fs == nil || fs.index == nil {
70+
return nil
71+
}
6972
if f, ok := fs.index["--"+name]; ok {
7073
return f
7174
}

main/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package main
22

33
import "testing"
44

5-
func TestMain(t *testing.T) {
5+
func TestMain(m *testing.M) {
66
Main([]string{})
77
}

oss/lib/cli_bridge.go

+39-4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,40 @@ func NewCommandBridge(cmd Command) *cli.Command {
112112
return result
113113
}
114114

115+
// ParseAndGetEndpoint get oss endpoint from cli context
116+
func ParseAndGetEndpoint(ctx *cli.Context, args []string) (string, error) {
117+
profile, err := config.LoadProfileWithContext(ctx)
118+
if err != nil {
119+
return "", fmt.Errorf("config failed: %s", err.Error())
120+
}
121+
// try fetch endpoint from args
122+
if len(args) > 0 {
123+
for i, arg := range args {
124+
if arg == "--endpoint" {
125+
if i+1 < len(args) {
126+
return args[i+1], nil
127+
}
128+
}
129+
}
130+
}
131+
// try fetch region from args
132+
if len(args) > 0 {
133+
for i, arg := range args {
134+
if arg == "--region" {
135+
if i+1 < len(args) {
136+
return "oss-" + args[i+1] + ".aliyuncs.com", nil
137+
}
138+
}
139+
}
140+
}
141+
// check endpoint from flags
142+
if ep, ok := ctx.Flags().GetValue("endpoint"); !ok {
143+
return "oss-" + profile.RegionId + ".aliyuncs.com", nil
144+
} else {
145+
return ep, nil
146+
}
147+
}
148+
115149
func ParseAndRunCommandFromCli(ctx *cli.Context, args []string) error {
116150
// 利用 parser 解析 flags,否则下文读不到
117151
parser := cli.NewParser(args, ctx)
@@ -149,11 +183,12 @@ func ParseAndRunCommandFromCli(ctx *cli.Context, args []string) error {
149183
configs["sts-token"] = *model.SecurityToken
150184
}
151185

152-
if ep, ok := ctx.Flags().GetValue("endpoint"); !ok {
153-
configs["endpoint"] = "oss-" + profile.RegionId + ".aliyuncs.com"
154-
} else {
155-
configs["endpoint"] = ep
186+
// read endpoint from flags
187+
endpoint, err := ParseAndGetEndpoint(ctx, args)
188+
if err != nil {
189+
return fmt.Errorf("parse endpoint failed: %s", err)
156190
}
191+
configs["endpoint"] = endpoint
157192

158193
a2 := []string{"aliyun", "oss"}
159194
a2 = append(a2, ctx.Command().Name)

oss/lib/cli_bridge_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,71 @@
11
package lib
22

33
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/aliyun/aliyun-cli/cli"
7+
"github.com/stretchr/testify/assert"
48
"testing"
59
)
610

711
func TestCliBridge(t *testing.T) {
812
NewCommandBridge(configCommand.command)
913
}
14+
15+
func TestParseAndGetEndpoint(t *testing.T) {
16+
type args struct {
17+
ctx *cli.Context
18+
args []string
19+
}
20+
w := new(bytes.Buffer)
21+
stderr := new(bytes.Buffer)
22+
context := cli.NewCommandContext(w, stderr)
23+
flag := cli.Flag{
24+
Name: "endpoint",
25+
}
26+
flag.SetValue("oss-cn-hangzhou.aliyuncs.com")
27+
context.Flags().Add(&flag)
28+
29+
tests := []struct {
30+
name string
31+
args args
32+
want string
33+
wantErr assert.ErrorAssertionFunc
34+
}{
35+
{
36+
name: "Valid endpoint from args",
37+
args: args{
38+
ctx: new(cli.Context),
39+
args: []string{"--endpoint", "oss-cn-shenzhen.aliyuncs.com"},
40+
},
41+
want: "oss-cn-shenzhen.aliyuncs.com",
42+
wantErr: assert.NoError,
43+
},
44+
{
45+
name: "Valid region from args",
46+
args: args{
47+
ctx: new(cli.Context),
48+
args: []string{"--region", "cn-shenzhen"},
49+
},
50+
want: "oss-cn-shenzhen.aliyuncs.com",
51+
wantErr: assert.NoError,
52+
},
53+
{
54+
name: "Fetch endpoint flag from context",
55+
args: args{
56+
ctx: context,
57+
},
58+
want: "oss-cn-hangzhou.aliyuncs.com",
59+
wantErr: assert.NoError,
60+
},
61+
}
62+
for _, tt := range tests {
63+
t.Run(tt.name, func(t *testing.T) {
64+
got, err := ParseAndGetEndpoint(tt.args.ctx, tt.args.args)
65+
if !tt.wantErr(t, err, fmt.Sprintf("ParseAndGetEndpoint(%v, %v)", tt.args.ctx, tt.args.args)) {
66+
return
67+
}
68+
assert.Equalf(t, tt.want, got, "ParseAndGetEndpoint(%v, %v)", tt.args.ctx, tt.args.args)
69+
})
70+
}
71+
}

0 commit comments

Comments
 (0)