-
Notifications
You must be signed in to change notification settings - Fork 13
/
stat.go
193 lines (162 loc) · 5.37 KB
/
stat.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
package main
import (
"io"
"strconv"
"strings"
"github.com/aos-dev/go-service-qingstor"
typ "github.com/aos-dev/go-storage/v2/types"
"github.com/aos-dev/go-storage/v2/types/info"
"github.com/c2h5oh/datasize"
"github.com/qingstor/noah/pkg/types"
"github.com/qingstor/noah/task"
"github.com/spf13/cobra"
"github.com/qingstor/qsctl/v2/cmd/qsctl/taskutils"
"github.com/qingstor/qsctl/v2/constants"
"github.com/qingstor/qsctl/v2/pkg/i18n"
"github.com/qingstor/qsctl/v2/utils"
)
type statFlags struct {
format string
}
var statFlag = statFlags{}
// StatCommand will handle stat command.
var StatCommand = &cobra.Command{
Use: "stat qs://<bucket_name>/<object_key>",
Short: i18n.Sprintf("stat a remote object"),
Long: i18n.Sprintf("qsctl stat show the detailed info of this object"),
Example: utils.AlignPrintWithColon(
i18n.Sprintf("Stat object: qsctl stat qs://prefix/a"),
i18n.Sprintf("Stat bucket: qsctl stat qs://bucket-name"),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true // handle runtime errors with i18n, do not show error
if err := statRun(cmd, args); err != nil {
i18n.Fprintf(cmd.OutOrStderr(), "Execute %s command error: %s\n", "stat", err.Error())
return err
}
return nil
},
}
func statRun(c *cobra.Command, args []string) (err error) {
silenceUsage(c) // silence usage when handled error returns
rootTask := taskutils.NewAtStorageTask(10)
workDir, err := utils.ParseAtStorageInput(rootTask, args[0])
if err != nil {
return
}
// work dir is root path and path blank, handle it as stat bucket
if workDir == "/" && rootTask.GetPath() == "" {
t := task.NewStatStorage(rootTask)
t.Run(c.Context())
if t.GetFault().HasError() {
return t.GetFault()
}
sm, err := t.GetStorage().Metadata()
if err != nil {
return types.NewErrUnhandled(err)
}
statStorageOutput(c.OutOrStdout(), sm, t.GetStorageInfo(), statFlag.format)
return nil
}
t := task.NewStatFile(rootTask)
t.Run(c.Context())
if t.GetFault().HasError() {
return t.GetFault()
}
statFileOutput(c.OutOrStdout(), t.GetObject(), statFlag.format)
return
}
func initStatFlag() {
StatCommand.Flags().StringVar(&statFlag.format, constants.FormatFlag, "",
i18n.Sprintf(`use the specified FORMAT instead of the default;
output a newline after each use of FORMAT
The valid format sequences for files:
%F file type
%h content etag of the file
%n file name
%s total size, in bytes
%y time of last data modification, human-readable, e.g: 2006-01-02 15:04:05 +0000 UTC
%Y time of last data modification, seconds since Epoch
The valid format sequences for buckets:
%n bucket name
%l bucket location
%s total size, in bytes
%c count of files in this bucket
`),
)
}
func statFileFormat(input string, om *typ.Object) string {
input = strings.ReplaceAll(input, "%n", om.ID)
if v, ok := om.GetContentType(); ok {
input = strings.ReplaceAll(input, "%F", v)
}
if v, ok := om.GetETag(); ok {
input = strings.ReplaceAll(input, "%h", v)
}
input = strings.ReplaceAll(input, "%s", strconv.FormatInt(om.Size, 10))
input = strings.ReplaceAll(input, "%y", om.UpdatedAt.String())
input = strings.ReplaceAll(input, "%Y", strconv.FormatInt(om.UpdatedAt.Unix(), 10))
return input
}
func statStorageFormat(input string, sm info.StorageMeta, ss info.StorageStatistic) string {
input = strings.ReplaceAll(input, "%n", sm.Name)
if v, ok := sm.GetLocation(); ok {
input = strings.ReplaceAll(input, "%l", v)
}
if v, ok := ss.GetSize(); ok {
input = strings.ReplaceAll(input, "%s", strconv.FormatInt(v, 10))
}
if v, ok := ss.GetCount(); ok {
input = strings.ReplaceAll(input, "%c", strconv.FormatInt(v, 10))
}
return input
}
func statFileOutput(w io.Writer, om *typ.Object, format string) {
// if format string was set, print result as format string
if format != "" {
i18n.Fprintf(w, "%s\n", statFileFormat(format, om))
return
}
var content []string
content = append(content, i18n.Sprintf("Key: %s", om.ID))
content = append(content, i18n.Sprintf("Size: %s", datasize.ByteSize(om.Size).String()))
if v, ok := om.GetContentType(); ok {
content = append(content, i18n.Sprintf("Type: %s", v))
}
if v, ok := om.GetETag(); ok {
content = append(content, i18n.Sprintf("ETag: %s", v))
}
if v, ok := om.ObjectMeta.Get(qingstor.InfoObjectMetaStorageClass); ok {
content = append(content, i18n.Sprintf("StorageClass: %s", v))
}
content = append(content, i18n.Sprintf("UpdatedAt: %s", om.UpdatedAt.String()))
i18n.Fprintf(w, "%s\n", utils.AlignPrintWithColon(content...))
}
func statStorageOutput(w io.Writer, sm info.StorageMeta, ss info.StorageStatistic, format string) {
if format != "" {
i18n.Fprintf(w, "%s\n", statStorageFormat(format, sm, ss))
return
}
var content []string
content = append(content, i18n.Sprintf("Name: %s", sm.Name))
if v, ok := sm.GetLocation(); ok {
content = append(content, i18n.Sprintf("Location: %s", v))
}
if v, ok := ss.GetSize(); ok {
content = append(content, i18n.Sprintf("Size: %s", datasize.ByteSize(v).String()))
}
if v, ok := ss.GetCount(); ok {
content = append(content, i18n.Sprintf("Count: %s", strconv.FormatInt(v, 10)))
}
i18n.Fprintf(w, "%s\n", utils.AlignPrintWithColon(content...))
}
func resetStatFlag() {
statFlag = statFlags{}
}
type statShellHandler struct {
blankShellHandler
}
func (h statShellHandler) postRun(_ error) {
resetStatFlag()
}