-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (55 loc) · 1.65 KB
/
main.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
package main
import (
"bytes"
"encoding/base64"
"fmt"
flag "github.com/spf13/pflag"
"io/ioutil"
"os"
)
const version = "1.1.0"
func main() {
username := flag.StringP("username", "u", "", "username for docker registry")
password := flag.StringP("password", "p", "", "password for docker registry")
passwordFile := flag.StringP("password-file", "f", "", "password file for docker registry")
server := flag.StringP("server", "s", "", "docker registry server")
base64Output := flag.BoolP("base64", "b", false, "output result base64 encoded")
printVersion := flag.BoolP("version", "v", false, "Print the current version and exit")
flag.Parse()
if *printVersion {
fmt.Println(version)
os.Exit(0)
}
if *username == "" {
fmt.Println("username cannot be empty!")
flag.Usage()
os.Exit(1)
}
if *password == "" && *passwordFile == "" {
fmt.Println("password cannot be empty!")
flag.Usage()
os.Exit(1)
}
if *server == "" {
fmt.Println("server cannot be empty!")
flag.Usage()
os.Exit(1)
}
if *passwordFile != "" {
passwordBytes, err := ioutil.ReadFile(*passwordFile)
if err != nil {
fmt.Println("read password file error:", err)
os.Exit(1)
}
passwordBytes = bytes.Replace(passwordBytes, []byte("\n"), []byte(""), -1)
*password = string(passwordBytes)
}
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", *username, *password)))
result := fmt.Sprintf("{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"auth\":\"%s\"}}}", *server, *username, *password, auth)
switch *base64Output {
case true:
fmt.Println(base64.StdEncoding.EncodeToString([]byte(result)))
default:
fmt.Println(result)
}
}