forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
67 lines (56 loc) · 1.42 KB
/
helpers.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
package credential
import (
"encoding/base64"
"fmt"
"os"
"runtime"
"strings"
)
func encodeAuth(username, password string) string {
if username == "" || password == "" {
return ""
}
str := username + ":" + password
msgBytes := []byte(str)
encodedBytes := make([]byte, base64.StdEncoding.EncodedLen(len(msgBytes)))
base64.StdEncoding.Encode(encodedBytes, msgBytes)
return string(encodedBytes)
}
func decodeAuth(authStr string) (string, string, error) {
if authStr == "" {
return "", "", nil
}
decodeLen := base64.StdEncoding.DecodedLen(len(authStr))
decodeBytes := make([]byte, decodeLen)
authBytes := []byte(authStr)
n, err := base64.StdEncoding.Decode(decodeBytes, authBytes)
if err != nil {
return "", "", err
}
if n > decodeLen {
return "", "", fmt.Errorf("failed to decode auth %s", authStr)
}
splits := strings.Split(string(decodeBytes), ":")
if len(splits) != 2 {
return "", "", fmt.Errorf("invalid credential config")
}
password := strings.Trim(splits[1], "\x00")
return splits[0], password, nil
}
func homedir() string {
env := "HOME"
if runtime.GOOS == "windows" {
env = "USERPROFILE"
}
return os.Getenv(env)
}
func convertHost(addr string) string {
if strings.HasPrefix(addr, "http://") {
addr = strings.TrimPrefix(addr, "http://")
}
if strings.HasPrefix(addr, "https://") {
addr = strings.TrimPrefix(addr, "https://")
}
splits := strings.SplitN(addr, "/", 2)
return splits[0]
}