Skip to content

Commit

Permalink
Merge pull request #46 from tencentyun/dev/feature-init
Browse files Browse the repository at this point in the history
coscli initialization does not force the generation of a config file
  • Loading branch information
willppan authored Dec 4, 2023
2 parents 1f0c10d + abb3763 commit 5f3b97b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

## 下载链接

当前版本:v0.16.0-beta
当前版本:v0.17.0-beta

[Linux](https://github.com/tencentyun/coscli/releases/download/v0.16.0-beta/coscli-linux)
[Linux](https://github.com/tencentyun/coscli/releases/download/v0.17.0-beta/coscli-linux)

[Mac](https://github.com/tencentyun/coscli/releases/download/v0.16.0-beta/coscli-mac)
[Mac](https://github.com/tencentyun/coscli/releases/download/v0.17.0-beta/coscli-mac)

[Windows](https://github.com/tencentyun/coscli/releases/download/v0.16.0-beta/coscli-windows.exe)
[Windows](https://github.com/tencentyun/coscli/releases/download/v0.17.0-beta/coscli-windows.exe)

## 使用方法

Expand Down
30 changes: 26 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "coscli/logger"
"coscli/util"
"fmt"
logger "github.com/sirupsen/logrus"
"log"
"os"

Expand All @@ -25,7 +26,7 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
Version: "v0.16.0-beta",
Version: "v0.17.0-beta",
}

func Execute() {
Expand All @@ -46,6 +47,9 @@ func initConfig() {
home, err := homedir.Dir()
cobra.CheckErr(err)

// 获取命令参数
cmdArgs := os.Args[1]

viper.SetConfigType("yaml")
if cfgFile != "" {
if cfgFile[0] == '~' {
Expand All @@ -55,9 +59,27 @@ func initConfig() {
} else {
_, err = os.Stat(home + "/.cos.yaml")
if os.IsNotExist(err) {
log.Println("Welcome to coscli!\nWhen you use coscli for the first time, you need to input some necessary information to generate the default configuration file of coscli.")
initConfigFile(false)
cmdCnt++
// 执行命令除config相关命令外不再强制 init config文件,不存在则直接返回
if cmdArgs == "config" {
log.Println("Welcome to coscli!\nWhen you use coscli for the first time, you need to input some necessary information to generate the default configuration file of coscli.")
initConfigFile(false)
cmdCnt++
} else {
// 若无配置文件,则需有输入ak,sk及endpoint
if param.SecretID == "" {
logger.Fatalln("Auth failed,missing parameter SecretID")
os.Exit(1)
}
if param.SecretKey == "" {
logger.Fatalln("Auth failed,missing parameter SecretKey")
os.Exit(1)
}
if param.Endpoint == "" {
logger.Fatalln("Auth failed,missing parameter Endpoint")
os.Exit(1)
}
return
}
}

viper.AddConfigPath(home)
Expand Down
19 changes: 16 additions & 3 deletions cmd/signurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,25 @@ func GetSignedURL(path string, t int) {
Query: &url.Values{},
Header: &http.Header{},
}
if config.Base.SessionToken != "" {
opt.Query.Add("x-cos-security-token", config.Base.SessionToken)
// 格式化参数
secretID, secretKey, secretToken := config.Base.SecretID, config.Base.SecretKey, config.Base.SessionToken
if param.SecretID != "" {
secretID = param.SecretID
secretToken = ""
}
if param.SecretKey != "" {
secretKey = param.SecretKey
secretToken = ""
}
if param.SessionToken != "" {
secretToken = param.SessionToken
}
if secretToken != "" {
opt.Query.Add("x-cos-security-token", secretToken)
}

presignedURL, err := c.Object.GetPresignedURL(context.Background(), http.MethodGet, cosPath,
config.Base.SecretID, config.Base.SecretKey, time.Second*time.Duration(t), opt)
secretID, secretKey, time.Second*time.Duration(t), opt)
if err != nil {
logger.Fatalln(err)
os.Exit(1)
Expand Down
7 changes: 6 additions & 1 deletion util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ func CreateClient(config *Config, param *Param, bucketIDName string) *cos.Client
if param.SessionToken != "" {
secretToken = param.SessionToken
}
return cos.NewClient(CreateURL(bucketIDName, config.Base.Protocol, param.Endpoint), &http.Client{

protocol := "https"
if config.Base.Protocol != "" {
protocol = config.Base.Protocol
}
return cos.NewClient(CreateURL(bucketIDName, protocol, param.Endpoint), &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: secretID,
SecretKey: secretKey,
Expand Down
6 changes: 3 additions & 3 deletions util/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ func GetObjectsListRecursive(c *cos.Client, prefix string, limit int, include st
objects = append(objects, res.Contents...)
commonPrefixes = res.CommonPrefixes

// 对key进行urlDecode解码
objects = UrlDecodeCosPattern(objects)

if limit > 0 {
isTruncated = false
} else {
Expand All @@ -292,6 +289,9 @@ func GetObjectsListRecursive(c *cos.Client, prefix string, limit int, include st
}
}

// 对key进行urlDecode解码
objects = UrlDecodeCosPattern(objects)

if len(include) > 0 {
objects = MatchCosPattern(objects, include, true)
}
Expand Down
8 changes: 7 additions & 1 deletion util/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,11 @@ func GenURL(config *Config, param *Param, bucketName string) *cos.BaseURL {
if endpoint == "" && bucket.Region != "" {
endpoint = fmt.Sprintf("cos.%s.myqcloud.com", bucket.Region)
}
return CreateURL(idName, config.Base.Protocol, endpoint)

protocol := "https"
if config.Base.Protocol != "" {
protocol = config.Base.Protocol
}

return CreateURL(idName, protocol, endpoint)
}

0 comments on commit 5f3b97b

Please sign in to comment.