From 33e214e7e3ee04f60da84daa3937382edd8a2fe2 Mon Sep 17 00:00:00 2001 From: yuangen Date: Fri, 10 Jan 2025 13:49:14 +0800 Subject: [PATCH] Adjust the parameter priority of oss bridge --- cli/flag.go | 6 ++++ cli/flag_set.go | 7 +++-- main/main_test.go | 2 +- oss/lib/cli_bridge.go | 43 +++++++++++++++++++++++--- oss/lib/cli_bridge_test.go | 62 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 7 deletions(-) diff --git a/cli/flag.go b/cli/flag.go index 52077fdf8..b1011822f 100644 --- a/cli/flag.go +++ b/cli/flag.go @@ -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 } @@ -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 { diff --git a/cli/flag_set.go b/cli/flag_set.go index 020036e28..3ffb9fbde 100644 --- a/cli/flag_set.go +++ b/cli/flag_set.go @@ -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. @@ -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 } diff --git a/main/main_test.go b/main/main_test.go index e0c3bd688..49c77e75f 100644 --- a/main/main_test.go +++ b/main/main_test.go @@ -2,6 +2,6 @@ package main import "testing" -func TestMain(t *testing.T) { +func TestMain(m *testing.M) { Main([]string{}) } diff --git a/oss/lib/cli_bridge.go b/oss/lib/cli_bridge.go index a0f11f08e..0465847b5 100644 --- a/oss/lib/cli_bridge.go +++ b/oss/lib/cli_bridge.go @@ -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) @@ -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) diff --git a/oss/lib/cli_bridge_test.go b/oss/lib/cli_bridge_test.go index b841a8e17..46fd09543 100644 --- a/oss/lib/cli_bridge_test.go +++ b/oss/lib/cli_bridge_test.go @@ -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) + }) + } +}