Skip to content

Commit e9afe3e

Browse files
Add insecure flag support and update endpoint handling (#1218)
1 parent 9144cae commit e9afe3e

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

cli/context.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ type Context struct {
4545
stdout io.Writer
4646
stderr io.Writer
4747
inConfigureMode bool
48+
// use http instead of https
49+
insecure bool
50+
}
51+
52+
func (ctx *Context) Insecure() bool {
53+
return ctx.insecure
54+
}
55+
56+
func (ctx *Context) SetInsecure(insecure bool) {
57+
ctx.insecure = insecure
4858
}
4959

5060
func (ctx *Context) InConfigureMode() bool {

main/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func Main(args []string) {
6464
ctx.EnterCommand(rootCmd)
6565
ctx.SetCompletion(cli.ParseCompletionForShell())
6666
ctx.SetInConfigureMode(openapi.DetectInConfigureMode(ctx.Flags()))
67+
// use http force, current use in oss bridge
68+
insecure, _ := ParseInSecure(args)
69+
ctx.SetInsecure(insecure)
6770

6871
rootCmd.AddSubCommand(config.NewConfigureCommand())
6972
rootCmd.AddSubCommand(lib.NewOssCommand())
@@ -76,6 +79,16 @@ func Main(args []string) {
7679
}
7780
}
7881

82+
func ParseInSecure(args []string) (bool, interface{}) {
83+
// check has insecure flag
84+
for _, arg := range args {
85+
if arg == "--insecure" {
86+
return true, nil
87+
}
88+
}
89+
return false, nil
90+
}
91+
7992
func main() {
8093
Main(os.Args[1:])
8194
}

main/main_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
11
package main
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
func TestMain(m *testing.M) {
68
Main([]string{})
79
}
10+
11+
func TestParseInSecure(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
args []string
15+
expected bool
16+
}{
17+
{
18+
name: "Insecure flag present",
19+
args: []string{"--insecure"},
20+
expected: true,
21+
},
22+
{
23+
name: "Insecure flag with value",
24+
args: []string{"--insecure", "true"},
25+
expected: true,
26+
},
27+
{
28+
name: "Insecure flag absent",
29+
args: []string{"--secure"},
30+
expected: false,
31+
},
32+
{
33+
name: "Empty args",
34+
args: []string{},
35+
expected: false,
36+
},
37+
}
38+
39+
for _, tt := range tests {
40+
t.Run(tt.name, func(t *testing.T) {
41+
result, _ := ParseInSecure(tt.args)
42+
if result != tt.expected {
43+
t.Errorf("ParseInSecure(%v) = %v; want %v", tt.args, result, tt.expected)
44+
}
45+
})
46+
}
47+
}

oss/lib/cli_bridge.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lib
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/alibabacloud-go/tea/tea"
89
"github.com/aliyun/aliyun-cli/cli"
@@ -149,7 +150,10 @@ func ParseAndGetEndpoint(ctx *cli.Context, args []string) (string, error) {
149150
func ParseAndRunCommandFromCli(ctx *cli.Context, args []string) error {
150151
// 利用 parser 解析 flags,否则下文读不到
151152
parser := cli.NewParser(args, ctx)
152-
parser.ReadAll()
153+
_, err := parser.ReadAll()
154+
if err != nil {
155+
return err
156+
}
153157

154158
profile, err := config.LoadProfileWithContext(ctx)
155159
if err != nil {
@@ -188,6 +192,15 @@ func ParseAndRunCommandFromCli(ctx *cli.Context, args []string) error {
188192
if err != nil {
189193
return fmt.Errorf("parse endpoint failed: %s", err)
190194
}
195+
// check use http force
196+
forceUseHttp := ctx.Insecure()
197+
if endpoint != "" && !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") {
198+
if forceUseHttp {
199+
endpoint = "http://" + endpoint
200+
} else {
201+
endpoint = "https://" + endpoint
202+
}
203+
}
191204
configs["endpoint"] = endpoint
192205

193206
a2 := []string{"aliyun", "oss"}

0 commit comments

Comments
 (0)