-
Notifications
You must be signed in to change notification settings - Fork 100
/
cat.go
147 lines (120 loc) · 3.12 KB
/
cat.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
package lib
import (
"fmt"
"io"
"os"
)
var specChineseCat = SpecText{
synopsisText: "将文件内容输出到标准输出",
paramText: "object [options]",
syntaxText: `
ossutil cat oss://bucket/object
`,
detailHelpText: `
cat命令可以将oss的object内容输出到标准输出,object内容最好是文本格式
用法:
该命令仅有一种用法:
1) ossutil cat oss://bucket/object
将object内容输出到标准输出
`,
sampleText: `
1) 将object内容输出到标准输出
ossutil cat oss://bucket/object
`,
}
var specEnglishCat = SpecText{
synopsisText: "Output object content to standard output",
paramText: "object [options]",
syntaxText: `
ossutil cat oss://bucket/object
`,
detailHelpText: `
The cat command can output the object content of oss to standard output
The object content is preferably text format
Usage:
There is only one usage for this command:
1) ossutil cat oss://bucket/object
The command output object content to standard output
`,
sampleText: `
1) output object content to standard output
ossutil cat oss://bucket/object
`,
}
type catOptionType struct {
bucketName string
objectName string
encodingType string
}
type CatCommand struct {
command Command
catOption catOptionType
}
var catCommand = CatCommand{
command: Command{
name: "cat",
nameAlias: []string{"cat"},
minArgc: 1,
maxArgc: 1,
specChinese: specChineseCat,
specEnglish: specEnglishCat,
group: GroupTypeNormalCommand,
validOptionNames: []string{
OptionConfigFile,
OptionEndpoint,
OptionAccessKeyID,
OptionAccessKeySecret,
OptionSTSToken,
OptionEncodingType,
OptionLogLevel,
},
},
}
// function for FormatHelper interface
func (catc *CatCommand) formatHelpForWhole() string {
return catc.command.formatHelpForWhole()
}
func (catc *CatCommand) formatIndependHelp() string {
return catc.command.formatIndependHelp()
}
// Init simulate inheritance, and polymorphism
func (catc *CatCommand) Init(args []string, options OptionMapType) error {
return catc.command.Init(args, options, catc)
}
// RunCommand simulate inheritance, and polymorphism
func (catc *CatCommand) RunCommand() error {
catc.catOption.encodingType, _ = GetString(OptionEncodingType, catc.command.options)
srcBucketUrL, err := GetCloudUrl(catc.command.args[0], catc.catOption.encodingType)
if err != nil {
return err
}
if srcBucketUrL.object == "" {
return fmt.Errorf("object key is empty")
}
catc.catOption.bucketName = srcBucketUrL.bucket
catc.catOption.objectName = srcBucketUrL.object
// check object exist or not
client, err := catc.command.ossClient(catc.catOption.bucketName)
if err != nil {
return err
}
bucket, err := client.Bucket(catc.catOption.bucketName)
if err != nil {
return err
}
isExist, err := bucket.IsObjectExist(catc.catOption.objectName)
if err != nil {
return err
}
if !isExist {
return fmt.Errorf("oss object is not exist")
}
body, err := bucket.GetObject(catc.catOption.objectName)
if err != nil {
return err
}
defer body.Close()
io.Copy(os.Stdout, body)
fmt.Printf("\n")
return err
}