-
Notifications
You must be signed in to change notification settings - Fork 66
/
root.go
157 lines (138 loc) · 4.46 KB
/
root.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2020 Docker Hub Tool authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"context"
"fmt"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/docker/hub-tool/internal"
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/commands/account"
"github.com/docker/hub-tool/internal/commands/org"
"github.com/docker/hub-tool/internal/commands/repo"
"github.com/docker/hub-tool/internal/commands/tag"
"github.com/docker/hub-tool/internal/commands/token"
"github.com/docker/hub-tool/internal/login"
"github.com/docker/hub-tool/pkg/credentials"
"github.com/docker/hub-tool/pkg/hub"
)
type options struct {
showVersion bool
trace bool
verbose bool
}
var (
anonCmds = []string{"version", "help", "login", "logout"}
)
// NewRootCmd returns the main command
func NewRootCmd(streams command.Streams, hubClient *hub.Client, store credentials.Store, name string) *cobra.Command {
var flags options
cmd := &cobra.Command{
Use: name,
Short: "Docker Hub Tool",
Long: `A tool to manage your Docker Hub images`,
Annotations: map[string]string{},
SilenceUsage: true,
DisableFlagsInUseLine: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if flags.trace {
log.SetLevel(log.TraceLevel)
} else if flags.verbose {
log.SetLevel(log.DebugLevel)
}
if flags.showVersion {
return nil
}
if contains(anonCmds, cmd.Name()) {
return nil
}
ac, err := store.GetAuth()
if err != nil {
return err
}
if ac.Username == "" {
log.Fatal(ansi.Error(`You need to be logged in to Docker Hub to use this tool.
Please login to Docker Hub using the "hub-tool login" command.`))
}
if cmd.Annotations["sudo"] == "true" {
if err := tryLogin(cmd.Context(), streams, hubClient, ac, store); err != nil {
return err
}
return nil
}
if ac.TokenExpired() {
return tryLogin(cmd.Context(), streams, hubClient, ac, store)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if flags.showVersion {
fmt.Fprintf(streams.Out(), "Docker Hub Tool %s, build %s\n", internal.Version, internal.GitCommit[:7])
return nil
}
return cmd.Help()
},
}
cmd.Flags().BoolVar(&flags.showVersion, "version", false, "Display the version of this tool")
cmd.PersistentFlags().BoolVar(&flags.verbose, "verbose", false, "Print logs")
cmd.PersistentFlags().BoolVar(&flags.trace, "trace", false, "Print trace logs")
_ = cmd.PersistentFlags().MarkHidden("trace")
cmd.AddCommand(
newLoginCmd(streams, store, hubClient),
newLogoutCmd(streams, store),
account.NewAccountCmd(streams, hubClient),
token.NewTokenCmd(streams, hubClient),
org.NewOrgCmd(streams, hubClient),
repo.NewRepoCmd(streams, hubClient),
tag.NewTagCmd(streams, hubClient),
newVersionCmd(streams),
)
return cmd
}
func contains(haystack []string, needle string) bool {
for _, v := range haystack {
if needle == v {
return true
}
}
return false
}
func newVersionCmd(streams command.Streams) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Version information about this tool",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
_, err := fmt.Fprintf(streams.Out(), "Version: %s\nGit commit: %s\n", internal.Version, internal.GitCommit)
return err
},
}
}
func tryLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, ac *credentials.Auth, store credentials.Store) error {
token, refreshToken, err := login.Login(ctx, streams, hubClient, ac.Username, ac.Password)
if err != nil {
return err
}
if err := hubClient.Update(hub.WithHubToken(token)); err != nil {
return err
}
return store.Store(credentials.Auth{
Username: ac.Username,
Password: ac.Password,
Token: token,
RefreshToken: refreshToken,
})
}