/
command.go
138 lines (116 loc) · 3.67 KB
/
command.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
package harbor
import (
"context"
"os"
"github.com/foomo/posh/pkg/command/tree"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/prompt/goprompt"
"github.com/foomo/posh/pkg/readline"
"github.com/foomo/posh/pkg/shell"
"github.com/foomo/posh/pkg/util"
"github.com/foomo/posh/pkg/util/browser"
"github.com/google/go-github/v47/github"
"github.com/pterm/pterm"
"golang.org/x/oauth2"
)
type (
Command struct {
l log.Logger
name string
harbor *Harbor
commandTree tree.Root
}
CommandOption func(*Command)
)
// ------------------------------------------------------------------------------------------------
// ~ Options
// ------------------------------------------------------------------------------------------------
func CommandWithName(v string) CommandOption {
return func(o *Command) {
o.name = v
}
}
// ------------------------------------------------------------------------------------------------
// ~ Constructor
// ------------------------------------------------------------------------------------------------
func NewCommand(l log.Logger, harbor *Harbor, opts ...CommandOption) *Command {
inst := &Command{
l: l.Named("harbor"),
name: "harbor",
harbor: harbor,
}
for _, opt := range opts {
if opt != nil {
opt(inst)
}
}
inst.commandTree = tree.New(&tree.Node{
Name: inst.name,
Description: "Run harbor",
Execute: inst.auth,
Nodes: tree.Nodes{
{
Name: "auth",
Args: nil,
Description: "Sign in to Harbor",
Execute: inst.auth,
},
{
Name: "docker",
Args: nil,
Description: "Configure docker to be able to access registry.",
Execute: inst.docker,
},
},
})
return inst
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (c *Command) Name() string {
return c.commandTree.Node().Name
}
func (c *Command) Description() string {
return c.commandTree.Node().Description
}
func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
return c.commandTree.Complete(ctx, r)
}
func (c *Command) Execute(ctx context.Context, r *readline.Readline) error {
return c.commandTree.Execute(ctx, r)
}
func (c *Command) Help(ctx context.Context, r *readline.Readline) string {
return c.commandTree.Help(ctx, r)
}
// ------------------------------------------------------------------------------------------------
// ~ Private methods
// ------------------------------------------------------------------------------------------------
func (c *Command) auth(ctx context.Context, r *readline.Readline) error {
return browser.OpenRawURL(c.harbor.Config().AuthURL)
}
func (c *Command) docker(ctx context.Context, r *readline.Readline) error {
client := github.NewClient(
oauth2.NewClient(
ctx,
oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
),
),
)
var username string
user, _, err := client.Users.Get(ctx, "")
if err == nil && user != nil && user.Login != nil {
username = *user.Login
} else if username, err = util.Prompt("github username"); err != nil {
return err
}
pterm.Info.Println("registry: " + c.harbor.Config().DockerRegistry())
pterm.Info.Println("username: " + username)
pterm.Info.Println("please enter your CLI secret as password provided on your profile settings...")
_ = browser.OpenRawURL(c.harbor.Config().AuthURL)
return shell.New(ctx, c.l, "docker", "login", c.harbor.Config().URL, "-u", username).
Args(r.AdditionalArgs()...).
Args(r.AdditionalFlags()...).
Run()
}