/
util.go
106 lines (92 loc) · 2.33 KB
/
util.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package update
import (
"crypto/rand"
"encoding/base32"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
)
func urlStringForKey(key string, bucketName string, prefix string) (string, string) {
name := key[len(prefix):]
return fmt.Sprintf("https://s3.amazonaws.com/%s/%s%s", bucketName, prefix, url.QueryEscape(name)), name
}
func urlString(bucketName string, prefix string, name string) string {
if prefix == "" {
return fmt.Sprintf("https://s3.amazonaws.com/%s/%s", bucketName, url.QueryEscape(name))
}
return fmt.Sprintf("https://s3.amazonaws.com/%s/%s%s", bucketName, prefix, url.QueryEscape(name))
}
func urlStringNoEscape(bucketName string, name string) string {
return fmt.Sprintf("https://s3.amazonaws.com/%s/%s", bucketName, name)
}
func makeParentDirs(filename string) error {
dir, _ := filepath.Split(filename)
exists, err := fileExists(dir)
if err != nil {
return err
}
if !exists {
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
}
return nil
}
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// CombineErrors returns a single error for multiple errors, or nil if none
func CombineErrors(errs ...error) error {
errs = RemoveNilErrors(errs)
if len(errs) == 0 {
return nil
} else if len(errs) == 1 {
return errs[0]
}
msgs := []string{}
for _, err := range errs {
msgs = append(msgs, err.Error())
}
return fmt.Errorf("There were multiple errors: %s", strings.Join(msgs, "; "))
}
// RemoveNilErrors returns error slice with nil errors removed
func RemoveNilErrors(errs []error) []error {
var r []error
for _, err := range errs {
if err != nil {
r = append(r, err)
}
}
return r
}
// RandomID returns a random identifier
func RandomID() (string, error) {
buf, err := RandBytes(32)
if err != nil {
return "", err
}
str := base32.StdEncoding.EncodeToString(buf)
str = strings.ReplaceAll(str, "=", "")
return str, nil
}
var randRead = rand.Read
// RandBytes returns random bytes of length
func RandBytes(length int) ([]byte, error) {
buf := make([]byte, length)
if _, err := randRead(buf); err != nil {
return nil, err
}
return buf, nil
}