/
stat-main.go
167 lines (139 loc) · 5.48 KB
/
stat-main.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
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"strings"
"time"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/pkg/v2/console"
)
// stat specific flags.
var (
statFlags = []cli.Flag{
cli.StringFlag{
Name: "rewind",
Usage: "stat on older version(s)",
},
cli.BoolFlag{
Name: "versions",
Usage: "stat all versions",
},
cli.StringFlag{
Name: "version-id, vid",
Usage: "stat a specific object version",
},
cli.BoolFlag{
Name: "recursive, r",
Usage: "stat all objects recursively",
},
}
)
// show object metadata
var statCmd = cli.Command{
Name: "stat",
Usage: "show object metadata",
Action: mainStat,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: append(append(statFlags, encCFlag), globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] TARGET [TARGET ...]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Stat all contents of mybucket on Amazon S3 cloud storage.
{{.Prompt}} {{.HelpName}} s3/mybucket/
2. Stat all contents of mybucket on Amazon S3 cloud storage on Microsoft Windows.
{{.Prompt}} {{.HelpName}} s3\mybucket\
3. Stat files recursively on a local filesystem on Microsoft Windows.
{{.Prompt}} {{.HelpName}} --recursive C:\Users\mydocuments\
4. Stat encrypted files on Amazon S3 cloud storage. In case the encryption key contains non-printable character like tab, pass the
base64 encoded string as key.
{{.Prompt}} {{.HelpName}} --enc-c "s3/personal-document/=MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDA" s3/personal-document/2019-account_report.docx
5. Stat a specific object version.
{{.Prompt}} {{.HelpName}} --version-id "CL3sWgdSN2pNntSf6UnZAuh2kcu8E8si" s3/personal-docs/2018-account_report.docx
6. Stat all objects versions recursively created before 1st January 2020.
{{.Prompt}} {{.HelpName}} --versions --rewind 2020.01.01T00:00 s3/personal-docs/
`,
}
// parseAndCheckStatSyntax - parse and validate all the passed arguments
func parseAndCheckStatSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[string][]prefixSSEPair) ([]string, bool, string, time.Time, bool) {
if !cliCtx.Args().Present() {
showCommandHelpAndExit(cliCtx, 1) // last argument is exit code
}
args := cliCtx.Args()
for _, arg := range args {
if strings.TrimSpace(arg) == "" {
fatalIf(errInvalidArgument().Trace(args...), "Unable to validate empty argument.")
}
}
recursive := cliCtx.Bool("recursive")
versionID := cliCtx.String("version-id")
withVersions := cliCtx.Bool("versions")
rewind := parseRewindFlag(cliCtx.String("rewind"))
// extract URLs.
URLs := cliCtx.Args()
if versionID != "" && len(args) > 1 {
fatalIf(errInvalidArgument().Trace(args...), "You cannot specify --version-id with multiple arguments.")
}
if versionID != "" && (recursive || withVersions || !rewind.IsZero()) {
fatalIf(errInvalidArgument().Trace(args...), "You cannot specify --version-id with either --rewind, --versions or --recursive.")
}
for _, url := range URLs {
_, _, err := url2Stat(ctx, url2StatOptions{urlStr: url, versionID: versionID, fileAttr: false, encKeyDB: encKeyDB, timeRef: rewind, isZip: false, ignoreBucketExistsCheck: false})
if err != nil {
fatalIf(err.Trace(url), "Unable to stat `"+url+"`.")
}
}
return URLs, recursive, versionID, rewind, withVersions
}
// mainStat - is a handler for mc stat command
func mainStat(cliCtx *cli.Context) error {
ctx, cancelStat := context.WithCancel(globalContext)
defer cancelStat()
// Additional command specific theme customization.
console.SetColor("Name", color.New(color.Bold, color.FgCyan))
console.SetColor("Date", color.New(color.FgWhite))
console.SetColor("Size", color.New(color.FgWhite))
console.SetColor("ETag", color.New(color.FgWhite))
console.SetColor("Metadata", color.New(color.FgWhite))
// theme specific to stat bucket
console.SetColor("Key", color.New(color.FgCyan))
console.SetColor("Value", color.New(color.FgYellow))
console.SetColor("Unset", color.New(color.FgRed))
console.SetColor("Set", color.New(color.FgGreen))
console.SetColor("Title", color.New(color.Bold, color.FgBlue))
console.SetColor("Count", color.New(color.FgGreen))
// Parse encryption keys per command.
encKeyDB, err := validateAndCreateEncryptionKeys(cliCtx)
fatalIf(err, "Unable to parse encryption keys.")
// check 'stat' cli arguments.
args, isRecursive, versionID, rewind, withVersions := parseAndCheckStatSyntax(ctx, cliCtx, encKeyDB)
// mimic operating system tool behavior.
if len(args) == 0 {
args = []string{"."}
}
for _, targetURL := range args {
fatalIf(statURL(ctx, targetURL, versionID, rewind, withVersions, false, isRecursive, encKeyDB), "Unable to stat `"+targetURL+"`.")
}
return nil
}