-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
90 lines (81 loc) · 2.1 KB
/
utils.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
package main
import (
"fmt"
"os"
"strings"
s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
)
const kiB = 1024
const MiB = 1024 * kiB
const GiB = 1024 * MiB
const TiB = 1024 * GiB
func parseS3Uri(s string) (string, string) {
if !strings.HasPrefix(s, "s3://") {
return "", ""
}
parts := strings.SplitN(s[5:], "/", 2)
if len(parts) == 0 {
return "", ""
} else if len(parts) == 1 {
return parts[0], ""
} else {
return parts[0], parts[1]
}
}
// The S3 docs state GB and TB but they actually mean GiB and TiB
// For consistency, format filesizes in GiB and TiB
func formatFilesize(size uint64) string {
if size < kiB {
return fmt.Sprintf("%d bytes", size)
} else if size < MiB {
return fmt.Sprintf("%.1f kiB (%d bytes)", float64(size)/float64(kiB), size)
} else if size < GiB {
return fmt.Sprintf("%.1f MiB (%d bytes)", float64(size)/float64(MiB), size)
} else if size < TiB {
return fmt.Sprintf("%.1f GiB (%d bytes)", float64(size)/float64(GiB), size)
} else {
return fmt.Sprintf("%.1f TiB (%d bytes)", float64(size)/float64(TiB), size)
}
}
func formatResumeCommand(encodedState, arg string) string {
cmd := []string{os.Args[0], "--resume", encodedState}
for i := 1; i < len(os.Args); i++ {
if os.Args[i] == "--resume" {
i++
continue
}
if strings.HasPrefix(os.Args[i], "s3://") {
continue
}
cmd = append(cmd, os.Args[i])
}
cmd = append(cmd, arg)
return strings.Join(cmd, " ")
}
// https://github.com/aws/aws-sdk-go/blob/e2d6cb448883e4f4fcc5246650f89bde349041ec/service/s3/bucket_location.go#L15-L32
// Would be nice if aws-sdk-go-v2 supported this.
func normalizeBucketLocation(loc s3Types.BucketLocationConstraint) string {
if loc == "" {
return "us-east-1"
}
return string(loc)
}
func isNumeric(s string) bool {
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return true
}
func mfaTokenProvider() (string, error) {
for {
fmt.Printf("Assume Role MFA token code: ")
var code string
_, err := fmt.Scanln(&code)
if len(code) == 6 && isNumeric(code) {
return code, err
}
fmt.Println("Code must consist of 6 digits. Please try again.")
}
}