head: Add support of --version-id and --rewind (#3354)

This commit is contained in:
Anis Elleuch
2020-08-26 10:38:35 -07:00
committed by GitHub
parent a104aa1cb2
commit 35632bc1af
3 changed files with 47 additions and 14 deletions
+12 -5
View File
@@ -141,14 +141,21 @@ func isAliasURLDir(ctx context.Context, aliasURL string, keys map[string][]prefi
}
// getSourceStreamMetadataFromURL gets a reader from URL.
func getSourceStreamMetadataFromURL(ctx context.Context, urlStr string, encKeyDB map[string][]prefixSSEPair) (reader io.ReadCloser,
func getSourceStreamMetadataFromURL(ctx context.Context, aliasedURL, versionID string, timeRef time.Time, encKeyDB map[string][]prefixSSEPair) (reader io.ReadCloser,
metadata map[string]string, err *probe.Error) {
alias, urlStrFull, _, err := expandAlias(urlStr)
alias, urlStrFull, _, err := expandAlias(aliasedURL)
if err != nil {
return nil, nil, err.Trace(urlStr)
return nil, nil, err.Trace(aliasedURL)
}
sseKey := getSSE(urlStr, encKeyDB[alias])
return getSourceStream(ctx, alias, urlStrFull, "", true, sseKey, false)
if !timeRef.IsZero() {
_, content, err := url2Stat(ctx, aliasedURL, "", false, nil, timeRef)
if err != nil {
return nil, nil, err
}
versionID = content.VersionID
}
sseKey := getSSE(aliasedURL, encKeyDB[alias])
return getSourceStream(ctx, alias, urlStrFull, versionID, true, sseKey, false)
}
// getSourceStreamFromURL gets a reader from URL.
+34 -8
View File
@@ -26,6 +26,7 @@ import (
"os"
"strings"
"syscall"
"time"
"github.com/minio/cli"
"github.com/minio/mc/pkg/probe"
@@ -38,6 +39,14 @@ var (
Usage: "print the first 'n' lines",
Value: 10,
},
cli.StringFlag{
Name: "rewind",
Usage: "Move back in time",
},
cli.StringFlag{
Name: "version-id, vid",
Usage: "Select an object version to display",
},
}
)
@@ -77,7 +86,7 @@ EXAMPLES:
}
// headURL displays contents of a URL to stdout.
func headURL(sourceURL string, encKeyDB map[string][]prefixSSEPair, nlines int64) *probe.Error {
func headURL(sourceURL, sourceVersion string, timeRef time.Time, encKeyDB map[string][]prefixSSEPair, nlines int64) *probe.Error {
var reader io.ReadCloser
switch sourceURL {
case "-":
@@ -85,7 +94,7 @@ func headURL(sourceURL string, encKeyDB map[string][]prefixSSEPair, nlines int64
default:
var err *probe.Error
var metadata map[string]string
if reader, metadata, err = getSourceStreamMetadataFromURL(context.Background(), sourceURL, encKeyDB); err != nil {
if reader, metadata, err = getSourceStreamMetadataFromURL(context.Background(), sourceURL, sourceVersion, timeRef, encKeyDB); err != nil {
return err.Trace(sourceURL)
}
ctype := metadata["Content-Type"]
@@ -150,17 +159,34 @@ func headOut(r io.Reader, nlines int64) *probe.Error {
return nil
}
// parseHeadSyntax performs command-line input validation for head command.
func parseHeadSyntax(ctx *cli.Context) (args []string, versionID string, timeRef time.Time) {
args = ctx.Args()
versionID = ctx.String("version-id")
rewind := ctx.String("rewind")
if versionID != "" && rewind != "" {
fatalIf(errInvalidArgument().Trace(), "You cannot specify --version-id and --rewind at the same time")
}
if versionID != "" && len(args) != 1 {
fatalIf(errInvalidArgument().Trace(), "You need to pass at least one argument if --version-id is specified")
}
timeRef = parseRewindFlag(rewind)
return
}
// mainHead is the main entry point for head command.
func mainHead(ctx *cli.Context) error {
// Parse encryption keys per command.
encKeyDB, err := getEncKeys(ctx)
fatalIf(err, "Unable to parse encryption keys.")
// Set command flags from context.
stdinMode := false
if !ctx.Args().Present() {
stdinMode = true
}
args, versionID, timeRef := parseHeadSyntax(ctx)
stdinMode := len(args) == 0
// handle std input data.
if stdinMode {
@@ -170,7 +196,7 @@ func mainHead(ctx *cli.Context) error {
// Convert arguments to URLs: expand alias, fix format.
for _, url := range ctx.Args() {
fatalIf(headURL(url, encKeyDB, ctx.Int64("lines")).Trace(url), "Unable to read from `"+url+"`.")
fatalIf(headURL(url, versionID, timeRef, encKeyDB, ctx.Int64("lines")).Trace(url), "Unable to read from `"+url+"`.")
}
return nil
+1 -1
View File
@@ -310,7 +310,7 @@ func getCSVHeader(sourceURL string, encKeyDB map[string][]prefixSSEPair) ([]stri
default:
var err *probe.Error
var metadata map[string]string
if r, metadata, err = getSourceStreamMetadataFromURL(globalContext, sourceURL, encKeyDB); err != nil {
if r, metadata, err = getSourceStreamMetadataFromURL(globalContext, sourceURL, "", time.Time{}, encKeyDB); err != nil {
return nil, err.Trace(sourceURL)
}
ctype := metadata["Content-Type"]