forked from genuinetools/reg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (109 loc) · 3.32 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/genuinetools/pkg/cli"
"github.com/genuinetools/reg/registry"
"github.com/genuinetools/reg/repoutils"
"github.com/genuinetools/reg/version"
"github.com/sirupsen/logrus"
)
var (
insecure bool
forceNonSSL bool
skipPing bool
timeout time.Duration
authURL string
username string
password string
debug bool
)
//go:generate go run internal/binutils/generate.go
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "reg"
p.Description = "Docker registry v2 client"
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Build the list of available commands.
p.Commands = []cli.Command{
&digestCommand{},
&layerCommand{},
&listCommand{},
&manifestCommand{},
&removeCommand{},
&serverCommand{},
&tagsCommand{},
&vulnsCommand{},
}
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.BoolVar(&insecure, "insecure", false, "do not verify tls certificates")
p.FlagSet.BoolVar(&insecure, "k", false, "do not verify tls certificates")
p.FlagSet.BoolVar(&forceNonSSL, "force-non-ssl", false, "force allow use of non-ssl")
p.FlagSet.BoolVar(&forceNonSSL, "f", false, "force allow use of non-ssl")
p.FlagSet.BoolVar(&skipPing, "skip-ping", false, "skip pinging the registry while establishing connection")
p.FlagSet.DurationVar(&timeout, "timeout", time.Minute, "timeout for HTTP requests")
p.FlagSet.StringVar(&authURL, "auth-url", "", "alternate URL for registry authentication (ex. auth.docker.io)")
p.FlagSet.StringVar(&username, "username", "", "username for the registry")
p.FlagSet.StringVar(&username, "u", "", "username for the registry")
p.FlagSet.StringVar(&password, "password", "", "password for the registry")
p.FlagSet.StringVar(&password, "p", "", "password for the registry")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
// Set the before function.
p.Before = func(ctx context.Context) error {
// On ^C, or SIGTERM handle exit.
signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt)
signal.Notify(signals, syscall.SIGTERM)
_, cancel := context.WithCancel(ctx)
go func() {
for sig := range signals {
cancel()
logrus.Infof("Received %s, exiting.", sig.String())
os.Exit(0)
}
}()
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
// Run our program.
p.Run()
}
func createRegistryClient(ctx context.Context, domain string) (*registry.Registry, error) {
// Use the auth-url domain if provided.
authDomain := authURL
if authDomain == "" {
authDomain = domain
}
auth, err := repoutils.GetAuthConfig(username, password, authDomain)
if err != nil {
return nil, err
}
// Prevent non-ssl unless explicitly forced
if !forceNonSSL && strings.HasPrefix(auth.ServerAddress, "http:") {
return nil, fmt.Errorf("attempted to use insecure protocol! Use force-non-ssl option to force")
}
// Create the registry client.
logrus.Infof("domain: %s", domain)
logrus.Infof("server address: %s", auth.ServerAddress)
return registry.New(ctx, auth, registry.Opt{
Domain: domain,
Insecure: insecure,
Debug: debug,
SkipPing: skipPing,
NonSSL: forceNonSSL,
Timeout: timeout,
})
}