Skip to content

Commit

Permalink
Adjust the parameter priority of oss bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSpaceiiii committed Jan 10, 2025
1 parent f36184a commit 33e214e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
6 changes: 6 additions & 0 deletions cli/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type Flag struct {

// return true if flag appeared, either `--flag1` or `--flag1 value1`
func (f *Flag) IsAssigned() bool {
if f == nil {
return false
}
return f.assigned
}

Expand All @@ -101,6 +104,9 @@ func (f *Flag) SetValue(value string) {
//
// for `AssignedMode == AssignedRepeatable`. Use GetValues() to get all values
func (f *Flag) GetValue() (string, bool) {
if f == nil {
return "", false
}
if f.IsAssigned() {
return f.value, true
} else if f.Required {
Expand Down
7 changes: 5 additions & 2 deletions cli/flag_set.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
// Package cli Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,8 +64,11 @@ func (fs *FlagSet) AddByName(name string) (*Flag, error) {
return f, nil
}

// get flag by name, sample --name
// Get fetch flag by name, sample --name
func (fs *FlagSet) Get(name string) *Flag {
if fs == nil || fs.index == nil {
return nil
}
if f, ok := fs.index["--"+name]; ok {
return f
}
Expand Down
2 changes: 1 addition & 1 deletion main/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package main

import "testing"

func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
Main([]string{})
}
43 changes: 39 additions & 4 deletions oss/lib/cli_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,40 @@ func NewCommandBridge(cmd Command) *cli.Command {
return result
}

// ParseAndGetEndpoint get oss endpoint from cli context
func ParseAndGetEndpoint(ctx *cli.Context, args []string) (string, error) {
profile, err := config.LoadProfileWithContext(ctx)
if err != nil {
return "", fmt.Errorf("config failed: %s", err.Error())
}
// try fetch endpoint from args
if len(args) > 0 {
for i, arg := range args {
if arg == "--endpoint" {
if i+1 < len(args) {
return args[i+1], nil
}
}
}
}
// try fetch region from args
if len(args) > 0 {
for i, arg := range args {
if arg == "--region" {
if i+1 < len(args) {
return "oss-" + args[i+1] + ".aliyuncs.com", nil
}
}
}
}
// check endpoint from flags
if ep, ok := ctx.Flags().GetValue("endpoint"); !ok {
return "oss-" + profile.RegionId + ".aliyuncs.com", nil
} else {
return ep, nil
}
}

func ParseAndRunCommandFromCli(ctx *cli.Context, args []string) error {
// 利用 parser 解析 flags,否则下文读不到
parser := cli.NewParser(args, ctx)
Expand Down Expand Up @@ -149,11 +183,12 @@ func ParseAndRunCommandFromCli(ctx *cli.Context, args []string) error {
configs["sts-token"] = *model.SecurityToken
}

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

a2 := []string{"aliyun", "oss"}
a2 = append(a2, ctx.Command().Name)
Expand Down
62 changes: 62 additions & 0 deletions oss/lib/cli_bridge_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
package lib

import (
"bytes"
"fmt"
"github.com/aliyun/aliyun-cli/cli"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCliBridge(t *testing.T) {
NewCommandBridge(configCommand.command)
}

func TestParseAndGetEndpoint(t *testing.T) {
type args struct {
ctx *cli.Context
args []string
}
w := new(bytes.Buffer)
stderr := new(bytes.Buffer)
context := cli.NewCommandContext(w, stderr)
flag := cli.Flag{
Name: "endpoint",
}
flag.SetValue("oss-cn-hangzhou.aliyuncs.com")
context.Flags().Add(&flag)

tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "Valid endpoint from args",
args: args{
ctx: new(cli.Context),
args: []string{"--endpoint", "oss-cn-shenzhen.aliyuncs.com"},
},
want: "oss-cn-shenzhen.aliyuncs.com",
wantErr: assert.NoError,
},
{
name: "Valid region from args",
args: args{
ctx: new(cli.Context),
args: []string{"--region", "cn-shenzhen"},
},
want: "oss-cn-shenzhen.aliyuncs.com",
wantErr: assert.NoError,
},
{
name: "Fetch endpoint flag from context",
args: args{
ctx: context,
},
want: "oss-cn-hangzhou.aliyuncs.com",
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseAndGetEndpoint(tt.args.ctx, tt.args.args)
if !tt.wantErr(t, err, fmt.Sprintf("ParseAndGetEndpoint(%v, %v)", tt.args.ctx, tt.args.args)) {
return
}
assert.Equalf(t, tt.want, got, "ParseAndGetEndpoint(%v, %v)", tt.args.ctx, tt.args.args)
})
}
}

0 comments on commit 33e214e

Please sign in to comment.