-
Notifications
You must be signed in to change notification settings - Fork 99
/
bucket_tagging.go
216 lines (174 loc) · 6.14 KB
/
bucket_tagging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package lib
import (
"fmt"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
var specChineseBucketTag = SpecText{
synopsisText: "设置、查询或者删除bucket的tag配置",
paramText: "bucket_url [tag_parameter] [options]",
syntaxText: `
ossutil bucket-tagging --method put oss://bucket key#value
ossutil bucket-tagging --method get oss://bucket
ossuitl bucket-tagging --method delete oss://bucket
`,
detailHelpText: `
bucket-tagging命令通过设置method选项值为put、get、delete,可以设置、查询或者删除bucket的tag配置
每个tag的key和value必须以字符'#'分隔,最多可以连续输入20个tag信息
用法:
该命令有三种用法:
1) ossutil bucket-tagging --method put oss://bucket tagkey#tagvalue
这个命令设置bucket的tag配置,key和value分别为tagkey、tagvalue
2) ossutil bucket-tagging --method get oss://bucket
这个命令查询bucket的tag配置
3) ossutil bucket-tagging --method delete oss://bucket
这个命令删除bucket的tag配置
`,
sampleText: `
1) 设置bucket的tag配置
ossutil bucket-tagging --method put oss://bucket tagkey#tagvalue
2) 设置bucket的多个tag配置
ossutil bucket-tagging --method put oss://bucket tagkey1#tagvalue1 tagkey2#tagvalue2
3) 查询bucket的tag配置
ossutil bucket-tagging --method get oss://bucket
4) 删除bucket的tag配置
ossutil bucket-tagging --method delete oss://bucket
`,
}
var specEnglishBucketTag = SpecText{
synopsisText: "Set, get or delete bucket tag configuration",
paramText: "bucket_url [tag_parameter] [options]",
syntaxText: `
ossutil bucket-tagging --method put oss://bucket key#value
ossutil bucket-tagging --method get oss://bucket
ossuitl bucket-tagging --method delete oss://bucket
`,
detailHelpText: `
bucket-tagging command can set, get and delete the tag configuration of the oss bucket by set method option value to put, get, delete
the key and value of each tag must be separated by the character '#', you can enter up to 20 tag parameters.
Usage:
There are three usages for this command:
1) ossutil bucket-tagging --method put oss://bucket tagkey#tagvalue
The command sets the tag configuration of the bucket. The key and value are tagkey and tagvalue
2) ossutil bucket-tagging --method get oss://bucket
The command gets the tag configuration of bucket
3) ossutil bucket-tagging --method delete oss://bucket
The command deletes the tag configuration of bucket
`,
sampleText: `
1) set bucket tag configuration with one tag
ossutil bucket-tagging --method put oss://bucket tagkey#tagvalue
2) set bucket tag configuration with serveral tags
ossutil bucket-tagging --method put oss://bucket tagkey1#tagvalue1 tagkey2#tagvalue2
3) get bucket tag configuration
ossutil bucket-tagging --method get oss://bucket
4) delete bucket tag configuration
ossutil bucket-tagging --method delete oss://bucket
`,
}
type BucketTagCommand struct {
command Command
bucketName string
tagResult oss.GetBucketTaggingResult
}
var bucketTagCommand = BucketTagCommand{
command: Command{
name: "bucket-tagging",
nameAlias: []string{"bucket-tagging"},
minArgc: 1,
maxArgc: 21,
specChinese: specChineseBucketTag,
specEnglish: specEnglishBucketTag,
group: GroupTypeNormalCommand,
validOptionNames: []string{
OptionConfigFile,
OptionEndpoint,
OptionAccessKeyID,
OptionAccessKeySecret,
OptionSTSToken,
OptionMethod,
OptionLogLevel,
},
},
}
// function for FormatHelper interface
func (btc *BucketTagCommand) formatHelpForWhole() string {
return btc.command.formatHelpForWhole()
}
func (btc *BucketTagCommand) formatIndependHelp() string {
return btc.command.formatIndependHelp()
}
// Init simulate inheritance, and polymorphism
func (btc *BucketTagCommand) Init(args []string, options OptionMapType) error {
return btc.command.Init(args, options, btc)
}
// RunCommand simulate inheritance, and polymorphism
func (btc *BucketTagCommand) RunCommand() error {
strMethod, _ := GetString(OptionMethod, btc.command.options)
if strMethod == "" {
return fmt.Errorf("--method value is empty")
}
strMethod = strings.ToLower(strMethod)
if strMethod != "put" && strMethod != "get" && strMethod != "delete" {
return fmt.Errorf("--method value is not in the optional value:put|get|delete")
}
srcBucketUrL, err := GetCloudUrl(btc.command.args[0], "")
if err != nil {
return err
}
btc.bucketName = srcBucketUrL.bucket
if strMethod == "put" {
err = btc.PutBucketTag()
} else if strMethod == "get" {
err = btc.GetBucketTag()
} else if strMethod == "delete" {
err = btc.DeleteBucketTag()
}
return err
}
func (btc *BucketTagCommand) PutBucketTag() error {
if len(btc.command.args) < 2 {
return fmt.Errorf("missing parameter,the tag value is empty")
}
var tagging oss.Tagging
tagList := btc.command.args[1:len(btc.command.args)]
for _, tag := range tagList {
pSlice := strings.Split(tag, "#")
if len(pSlice) != 2 {
return fmt.Errorf("%s error,tag name and tag value must be separated by #", tag)
}
tagging.Tags = append(tagging.Tags, oss.Tag{Key: pSlice[0], Value: pSlice[1]})
}
// put bucket tag
client, err := btc.command.ossClient(btc.bucketName)
if err != nil {
return err
}
return client.SetBucketTagging(btc.bucketName, tagging)
}
func (btc *BucketTagCommand) GetBucketTag() error {
client, err := btc.command.ossClient(btc.bucketName)
if err != nil {
return err
}
btc.tagResult, err = client.GetBucketTagging(btc.bucketName)
if err != nil {
return err
}
if len(btc.tagResult.Tags) > 0 {
fmt.Printf("%-10s%s\t\t%s\n", "index", "tag key", "tag value")
fmt.Printf("---------------------------------------------------\n")
}
for index, tag := range btc.tagResult.Tags {
fmt.Printf("%-10d\"%s\"\t\t\"%s\"\n", index, tag.Key, tag.Value)
}
fmt.Printf("\n\n")
return nil
}
func (btc *BucketTagCommand) DeleteBucketTag() error {
client, err := btc.command.ossClient(btc.bucketName)
if err != nil {
return err
}
return client.DeleteBucketTagging(btc.bucketName)
}