-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
62 lines (53 loc) · 1.32 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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"runtime"
"syscall"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
"golang.org/x/crypto/ssh/terminal"
)
func errOut(err error) {
if err == nil {
return
}
if wd, err2 := os.Getwd(); err2 == nil {
_, f, l, _ := runtime.Caller(1)
if rel, err2 := filepath.Rel(filepath.Dir(f), wd); err2 == nil {
f = filepath.Join(rel, filepath.Base(f))
fmt.Fprintf(os.Stderr, "%s:%d %s\n", f, l, err.Error())
os.Exit(1)
}
}
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
func terminalCreds(host string) (string, string, error) {
fmt.Fprintf(os.Stderr, "Username: ")
stdin := bufio.NewReader(os.Stdin)
user, _, err := stdin.ReadLine()
if err != nil {
return "", "", err
}
fmt.Fprintf(os.Stderr, "Password: ")
pass, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", "", err
}
return string(user), string(pass), nil
}
func getResolver(credsFunc func(string) (string, string, error)) remotes.Resolver {
return docker.NewResolver(docker.ResolverOptions{
Hosts: docker.ConfigureDefaultRegistries(
docker.WithPlainHTTP(func(string) (bool, error) {
return allowHTTP, nil
}),
docker.WithAuthorizer(docker.NewDockerAuthorizer(
docker.WithAuthCreds(credsFunc),
)),
),
})
}