Skip to content

Commit 997a2eb

Browse files
authored
Merge pull request #91 from tencentyun/feature_willppan_ed611fa0
coscli v1.0.7
2 parents 78f27ce + 169bb83 commit 997a2eb

88 files changed

Lines changed: 5719 additions & 716 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66

77
## 下载链接
88

9-
当前版本:v1.0.6
9+
当前版本:v1.0.7
1010

11-
[Linux-386](https://github.com/tencentyun/coscli/releases/download/v1.0.6/coscli-v1.0.6-linux-386)
11+
[Linux-386](https://github.com/tencentyun/coscli/releases/download/v1.0.7/coscli-v1.0.7-linux-386)
1212

13-
[Linux-amd64](https://github.com/tencentyun/coscli/releases/download/v1.0.6/coscli-v1.0.6-linux-amd64)
13+
[Linux-amd64](https://github.com/tencentyun/coscli/releases/download/v1.0.7/coscli-v1.0.7-linux-amd64)
1414

15-
[Linux-arm](https://github.com/tencentyun/coscli/releases/download/v1.0.6/coscli-v1.0.6-linux-arm)
15+
[Linux-arm](https://github.com/tencentyun/coscli/releases/download/v1.0.7/coscli-v1.0.7-linux-arm)
1616

17-
[Linux-arm64](https://github.com/tencentyun/coscli/releases/download/v1.0.6/coscli-v1.0.6-linux-arm64)
17+
[Linux-arm64](https://github.com/tencentyun/coscli/releases/download/v1.0.7/coscli-v1.0.7-linux-arm64)
1818

19-
[Mac-amd64](https://github.com/tencentyun/coscli/releases/download/v1.0.6/coscli-v1.0.6-darwin-amd64)
19+
[Mac-amd64](https://github.com/tencentyun/coscli/releases/download/v1.0.7/coscli-v1.0.7-darwin-amd64)
2020

21-
[Mac-arm64](https://github.com/tencentyun/coscli/releases/download/v1.0.6/coscli-v1.0.6-darwin-arm64)
21+
[Mac-arm64](https://github.com/tencentyun/coscli/releases/download/v1.0.7/coscli-v1.0.7-darwin-arm64)
2222

23-
[Windows-386](https://github.com/tencentyun/coscli/releases/download/v1.0.6/coscli-v1.0.6-windows-386.exe)
23+
[Windows-386](https://github.com/tencentyun/coscli/releases/download/v1.0.7/coscli-v1.0.7-windows-386.exe)
2424

25-
[Windows-amd64](https://github.com/tencentyun/coscli/releases/download/v1.0.6/coscli-v1.0.6-windows-amd64.exe)
25+
[Windows-amd64](https://github.com/tencentyun/coscli/releases/download/v1.0.7/coscli-v1.0.7-windows-amd64.exe)
2626

2727
## 使用方法
2828

cmd/bucket_acl.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cmd
2+
3+
import (
4+
"coscli/util"
5+
"fmt"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var bucketAclCmd = &cobra.Command{
10+
Use: "bucket-acl",
11+
Short: "Modify bucket acl",
12+
Long: `Modify bucket acl
13+
14+
Format:
15+
./coscli bucket-acl --method [method] cos://<bucket-name>
16+
17+
Example:
18+
./coscli bucket-acl --method put cos://examplebucket --grant-read="id=\"100000000003\",id=\"100000000002\""
19+
./coscli bucket-acl --method get cos://examplebucket`,
20+
Args: cobra.ExactArgs(1),
21+
RunE: func(cmd *cobra.Command, args []string) error {
22+
var aclSettings util.ACLSettings
23+
method, _ := cmd.Flags().GetString("method")
24+
aclSettings.ACL, _ = cmd.Flags().GetString("acl")
25+
aclSettings.GrantRead, _ = cmd.Flags().GetString("grant-read")
26+
aclSettings.GrantWrite, _ = cmd.Flags().GetString("grant-write")
27+
aclSettings.GrantReadACP, _ = cmd.Flags().GetString("grant-read-acp")
28+
aclSettings.GrantWriteACP, _ = cmd.Flags().GetString("grant-write-acp")
29+
aclSettings.GrantFullControl, _ = cmd.Flags().GetString("grant-full-control")
30+
31+
var err error
32+
cosPath := args[0]
33+
if !util.IsCosPath(cosPath) {
34+
return fmt.Errorf("cospath needs to contain cos://")
35+
}
36+
37+
bucketName, _ := util.ParsePath(cosPath)
38+
c, err := util.NewClient(&config, &param, bucketName)
39+
if err != nil {
40+
return err
41+
}
42+
43+
if method == "put" {
44+
err = util.PutBucketAcl(c, aclSettings)
45+
} else if method == "get" {
46+
err = util.GetBucketAcl(c)
47+
} else {
48+
err = fmt.Errorf("method '%s' is not supported, valid methods are 'put' and 'get'", method)
49+
}
50+
51+
return err
52+
},
53+
}
54+
55+
func init() {
56+
rootCmd.AddCommand(bucketAclCmd)
57+
bucketAclCmd.Flags().String("method", "", "put/get")
58+
bucketAclCmd.Flags().String("acl", "", "Defines the Access Control List (ACL) property of an bucket. The default value is default.")
59+
bucketAclCmd.Flags().String("grant-read", "", "Grants the grantee permission to read the bucket. The format is id=\"[OwnerUin]\", for example, id=\"100000000001\". Multiple grantees can be specified using commas (,), for example, id=\"100000000001\",id=\"100000000002\".")
60+
bucketAclCmd.Flags().String("grant-write", "", "Grants the grantee permission to write the bucket. The format is id=\"[OwnerUin]\", for example, id=\"100000000001\". Multiple grantees can be specified using commas (,), for example, id='100000000001',id=\"100000000002\".")
61+
bucketAclCmd.Flags().String("grant-read-acp", "", "Grants the grantee permission to read the bucket's Access Control List (ACL). The format is id=\"[OwnerUin]\", for example, id=\"100000000001\". Multiple grantees can be specified using commas (,), for example, id=\"100000000001\",id=\"100000000002\".")
62+
bucketAclCmd.Flags().String("grant-write-acp", "", "Grants the grantee permission to write the bucket's Access Control List (ACL). The format is id=\"[OwnerUin]\", for example, id=\"100000000001\". Multiple grantees can be specified using commas (,), for example, id=\"100000000001\",id=\"100000000002\".")
63+
bucketAclCmd.Flags().String("grant-full-control", "", "Grants the grantee full permissions to operate on the bucket. The format is id=\"[OwnerUin]\", for example, id=\"100000000001\". Multiple grantees can be specified using commas (,), for example, id=\"100000000001\",id=\"100000000002\".")
64+
}

cmd/bucket_acl_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"coscli/util"
6+
"fmt"
7+
. "github.com/agiledragon/gomonkey/v2"
8+
. "github.com/smartystreets/goconvey/convey"
9+
"github.com/tencentyun/cos-go-sdk-v5"
10+
"reflect"
11+
"testing"
12+
)
13+
14+
func TestBucketAclCmd(t *testing.T) {
15+
fmt.Println("TestBucketAclCmd")
16+
testBucket = randStr(8)
17+
testAlias = testBucket + "-alias"
18+
setUp(testBucket, testAlias, testEndpoint, false, false)
19+
defer tearDown(testBucket, testAlias, testEndpoint, false)
20+
clearCmd()
21+
cmd := rootCmd
22+
cmd.SilenceErrors = true
23+
cmd.SilenceUsage = true
24+
genDir(testDir, 3)
25+
defer delDir(testDir)
26+
27+
Convey("test coscli bucket_acl", t, func() {
28+
Convey("success", func() {
29+
Convey("put", func() {
30+
clearCmd()
31+
var c *cos.BucketService
32+
patches := ApplyMethodFunc(reflect.TypeOf(c), "PutTagging", func(ctx context.Context, opt *cos.BucketPutACLOptions) (*cos.Response, error) {
33+
return nil, nil
34+
})
35+
defer patches.Reset()
36+
cmd := rootCmd
37+
args := []string{"bucket-acl", "--method", "put",
38+
fmt.Sprintf("cos://%s", testAlias), "--grant-read", "id=\"100000000003\",id=\"100000000002\""}
39+
cmd.SetArgs(args)
40+
e := cmd.Execute()
41+
So(e, ShouldBeNil)
42+
})
43+
Convey("get", func() {
44+
clearCmd()
45+
cmd := rootCmd
46+
args := []string{"bucket-acl", "--method", "get",
47+
fmt.Sprintf("cos://%s", testAlias)}
48+
cmd.SetArgs(args)
49+
e := cmd.Execute()
50+
So(e, ShouldBeNil)
51+
})
52+
})
53+
Convey("fail", func() {
54+
Convey("clinet err", func() {
55+
clearCmd()
56+
cmd := rootCmd
57+
patches := ApplyFunc(util.NewClient, func(config *util.Config, param *util.Param, bucketName string) (client *cos.Client, err error) {
58+
return nil, fmt.Errorf("test put client error")
59+
})
60+
defer patches.Reset()
61+
args := []string{"bucket-acl", "--method", "put",
62+
fmt.Sprintf("cos://%s", testAlias), "--grant-read", "id=\"100000000003\",id=\"100000000002\""}
63+
cmd.SetArgs(args)
64+
e := cmd.Execute()
65+
fmt.Printf(" : %v", e)
66+
So(e, ShouldBeError)
67+
})
68+
Convey("cos path error", func() {
69+
clearCmd()
70+
cmd := rootCmd
71+
72+
args := []string{"bucket-acl", "--method", "put",
73+
fmt.Sprintf("cos:/%s", testAlias), "--grant-read", "id=\"100000000003\",id=\"100000000002\""}
74+
cmd.SetArgs(args)
75+
e := cmd.Execute()
76+
fmt.Printf(" : %v", e)
77+
So(e, ShouldBeError)
78+
})
79+
Convey("invalid method", func() {
80+
clearCmd()
81+
cmd := rootCmd
82+
83+
args := []string{"bucket-acl", "--method", "add",
84+
fmt.Sprintf("cos://%s", testAlias), "--grant-read", "id=\"100000000003\",id=\"100000000002\""}
85+
cmd.SetArgs(args)
86+
e := cmd.Execute()
87+
fmt.Printf(" : %v", e)
88+
So(e, ShouldBeError)
89+
})
90+
})
91+
})
92+
}

cmd/bucket_encryption.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd
2+
3+
import (
4+
"coscli/util"
5+
"fmt"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var bucketEncryptionCmd = &cobra.Command{
10+
Use: "bucket-encryption",
11+
Short: "Modify bucket encryption",
12+
Long: `Modify bucket encryption
13+
14+
Format:
15+
./coscli bucket-encryption --method [method] cos://<bucket-name>
16+
17+
Example:
18+
./coscli bucket-encryption --method put cos://examplebucket --grant-read="id=\"100000000003\",id=\"100000000002\""
19+
./coscli bucket-encryption --method get cos://examplebucket
20+
./coscli bucket-encryption --method delete cos://examplebucket`,
21+
Args: cobra.ExactArgs(1),
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
var bucketEncryptionSettings util.BucketEncryptionSettings
24+
method, _ := cmd.Flags().GetString("method")
25+
bucketEncryptionSettings.SSEAlgorithm, _ = cmd.Flags().GetString("sse-algorithm")
26+
bucketEncryptionSettings.KMSMasterKeyID, _ = cmd.Flags().GetString("kms-master-key-id")
27+
bucketEncryptionSettings.KMSAlgorithm, _ = cmd.Flags().GetString("kms-algorithm")
28+
29+
var err error
30+
cosPath := args[0]
31+
if !util.IsCosPath(cosPath) {
32+
return fmt.Errorf("cospath needs to contain cos://")
33+
}
34+
35+
bucketName, _ := util.ParsePath(cosPath)
36+
c, err := util.NewClient(&config, &param, bucketName)
37+
if err != nil {
38+
return err
39+
}
40+
41+
if method == "put" {
42+
err = util.PutBucketEncryption(c, bucketEncryptionSettings)
43+
} else if method == "get" {
44+
err = util.GetBucketEncryption(c)
45+
} else if method == "delete" {
46+
err = util.DeleteBucketEncryption(c)
47+
} else {
48+
err = fmt.Errorf("method '%s' is not supported, valid methods are 'put', 'get', and 'delete'", method)
49+
}
50+
51+
return err
52+
},
53+
}
54+
55+
func init() {
56+
rootCmd.AddCommand(bucketEncryptionCmd)
57+
bucketEncryptionCmd.Flags().String("method", "", "put/get/delete")
58+
bucketEncryptionCmd.Flags().String("sse-algorithm", "", "Support enumeration values: AES256, SM4, KMS. AES256 represents the use of the SSE-COS mode with the AES256 encryption algorithm. SM4 represents the use of the SSE-COS mode with the SM4 encryption algorithm. KMS represents the SSE-KMS mode.")
59+
bucketEncryptionCmd.Flags().String("kms-master-key-id", "", "When the value of SSEAlgorithm is KMS, it is used to specify the user's Customer Master Key (CMK) for KMS. If not specified, the default CMK created by COS will be used.")
60+
bucketEncryptionCmd.Flags().String("kms-algorithm", "", "When the value of SSEAlgorithm is KMS, it is used to specify the encryption algorithm for KMS. Supported enumeration values are AES256 and SM4. If not specified, the default value is AES256. AES256 represents the encryption algorithm AES256. SM4 represents the encryption algorithm SM4.")
61+
}

cmd/bucket_encryption_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package cmd
2+
3+
import (
4+
"coscli/util"
5+
"fmt"
6+
. "github.com/agiledragon/gomonkey/v2"
7+
. "github.com/smartystreets/goconvey/convey"
8+
"github.com/tencentyun/cos-go-sdk-v5"
9+
"testing"
10+
)
11+
12+
func TestBucketEncryptionCmd(t *testing.T) {
13+
fmt.Println("TestBucketEncryptionCmd")
14+
testBucket = randStr(8)
15+
testAlias = testBucket + "-alias"
16+
setUp(testBucket, testAlias, testEndpoint, false, false)
17+
defer tearDown(testBucket, testAlias, testEndpoint, false)
18+
clearCmd()
19+
cmd := rootCmd
20+
cmd.SilenceErrors = true
21+
cmd.SilenceUsage = true
22+
genDir(testDir, 3)
23+
defer delDir(testDir)
24+
25+
Convey("test coscli bucket_encryption", t, func() {
26+
Convey("success", func() {
27+
Convey("put", func() {
28+
clearCmd()
29+
cmd := rootCmd
30+
args := []string{"bucket-encryption", "--method", "put",
31+
fmt.Sprintf("cos://%s", testAlias), "--sse-algorithm", "AES256"}
32+
cmd.SetArgs(args)
33+
e := cmd.Execute()
34+
So(e, ShouldBeNil)
35+
})
36+
Convey("get", func() {
37+
clearCmd()
38+
cmd := rootCmd
39+
args := []string{"bucket-encryption", "--method", "get",
40+
fmt.Sprintf("cos://%s", testAlias)}
41+
cmd.SetArgs(args)
42+
e := cmd.Execute()
43+
So(e, ShouldBeNil)
44+
})
45+
Convey("delete", func() {
46+
clearCmd()
47+
cmd := rootCmd
48+
args := []string{"bucket-encryption", "--method", "delete",
49+
fmt.Sprintf("cos://%s", testAlias)}
50+
cmd.SetArgs(args)
51+
e := cmd.Execute()
52+
So(e, ShouldBeNil)
53+
})
54+
})
55+
Convey("fail", func() {
56+
Convey("clinet err", func() {
57+
clearCmd()
58+
cmd := rootCmd
59+
patches := ApplyFunc(util.NewClient, func(config *util.Config, param *util.Param, bucketName string) (client *cos.Client, err error) {
60+
return nil, fmt.Errorf("test put client error")
61+
})
62+
defer patches.Reset()
63+
args := []string{"bucket-encryption", "--method", "get",
64+
fmt.Sprintf("cos://%s", testAlias)}
65+
cmd.SetArgs(args)
66+
e := cmd.Execute()
67+
fmt.Printf(" : %v", e)
68+
So(e, ShouldBeError)
69+
})
70+
Convey("cos path error", func() {
71+
clearCmd()
72+
cmd := rootCmd
73+
74+
args := []string{"bucket-encryption", "--method", "get",
75+
fmt.Sprintf("cos:/%s", testAlias)}
76+
cmd.SetArgs(args)
77+
e := cmd.Execute()
78+
fmt.Printf(" : %v", e)
79+
So(e, ShouldBeError)
80+
})
81+
Convey("invalid method", func() {
82+
clearCmd()
83+
cmd := rootCmd
84+
85+
args := []string{"bucket-encryption", "--method", "add",
86+
fmt.Sprintf("cos://%s", testAlias)}
87+
cmd.SetArgs(args)
88+
e := cmd.Execute()
89+
fmt.Printf(" : %v", e)
90+
So(e, ShouldBeError)
91+
})
92+
})
93+
})
94+
}

0 commit comments

Comments
 (0)