-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathvalidation.go
75 lines (65 loc) · 2.45 KB
/
validation.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
package command
import (
"fmt"
urlpkg "net/url"
"github.com/peak/s5cmd/v2/storage"
"github.com/peak/s5cmd/v2/storage/url"
"github.com/urfave/cli/v2"
)
const (
versioningNotSupportedWarning = "versioning related features are not supported with the given endpoint %q"
allVersionsFlagName = "all-versions"
versionIDFlagName = "version-id"
)
// checkVersinoningURLRemote checks if the versioning related flags are used with
// local objects. Because the versioning is only supported with s3.
func checkVersinoningURLRemote(url *url.URL) error {
if !url.IsRemote() && url.IsVersioned() {
return fmt.Errorf("%q, and %q flags can only be used with remote objects", allVersionsFlagName, versionIDFlagName)
}
return nil
}
// checkVersioningFlagCompatibility checks if the incompatible versioning flags
// are used together. Because it is not allowed to refer to both "all versions" and
// a specific version of an object together.
func checkVersioningFlagCompatibility(ctx *cli.Context) error {
if ctx.Bool(allVersionsFlagName) && ctx.String(versionIDFlagName) != "" {
return fmt.Errorf("it is not allowed to combine %q and %q flags", allVersionsFlagName, versionIDFlagName)
}
return nil
}
// checkVersioningWithGoogleEndpoint checks if the versioning flags are used with
// the Google Endpoint. Because the s3 versioning operations are not compatible with
// GCS's versioning API.
func checkVersioningWithGoogleEndpoint(ctx *cli.Context) error {
endpoint := ctx.String("endpoint-url")
if endpoint == "" {
return nil
}
u, err := urlpkg.Parse(endpoint)
if err != nil {
return err
}
if storage.IsGoogleEndpoint(*u) && (ctx.Bool(allVersionsFlagName) || ctx.String(versionIDFlagName) != "") {
return fmt.Errorf(versioningNotSupportedWarning, endpoint)
}
return nil
}
// checkNumberOfArguments checks if the number of the arguments is valid.
// if the max is negative then there is no upper limit of arguments.
func checkNumberOfArguments(ctx *cli.Context, min, max int) error {
l := ctx.Args().Len()
if min == 1 && max == 1 && l != 1 {
return fmt.Errorf("expected only one argument")
}
if min == 2 && max == 2 && l != 2 {
return fmt.Errorf("expected source and destination arguments")
}
if l < min {
return fmt.Errorf("expected at least %d arguments but was given %d: %q", min, l, ctx.Args().Slice())
}
if max >= 0 && l > max {
return fmt.Errorf("expected at most %d arguments but was given %d: %q", min, l, ctx.Args().Slice())
}
return nil
}