mirror of
https://github.com/foomo/posh-providers.git
synced 2025-10-16 12:35:41 +00:00
feat: add harbor checker
This commit is contained in:
parent
0f461077d1
commit
c0bad3a392
19
goharbor/harbor/checker.go
Normal file
19
goharbor/harbor/checker.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package harbor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/foomo/posh/pkg/log"
|
||||||
|
"github.com/foomo/posh/pkg/prompt/check"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AuthChecker(h *Harbor) check.Checker {
|
||||||
|
return func(ctx context.Context, l log.Logger) check.Info {
|
||||||
|
name := "Harbor"
|
||||||
|
if h.IsAuthenticated(ctx) {
|
||||||
|
return check.NewSuccessInfo(name, "Authenticated")
|
||||||
|
} else {
|
||||||
|
return check.NewFailureInfo(name, "Run `harbor auth` to sign into docker")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -13,16 +13,14 @@ import (
|
|||||||
"github.com/foomo/posh/pkg/util/browser"
|
"github.com/foomo/posh/pkg/util/browser"
|
||||||
"github.com/google/go-github/v47/github"
|
"github.com/google/go-github/v47/github"
|
||||||
"github.com/pterm/pterm"
|
"github.com/pterm/pterm"
|
||||||
"github.com/spf13/viper"
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Command struct {
|
Command struct {
|
||||||
l log.Logger
|
l log.Logger
|
||||||
cfg Config
|
|
||||||
name string
|
name string
|
||||||
configKey string
|
harbor *Harbor
|
||||||
commandTree tree.Root
|
commandTree tree.Root
|
||||||
}
|
}
|
||||||
CommandOption func(*Command)
|
CommandOption func(*Command)
|
||||||
@ -38,30 +36,21 @@ func CommandWithName(v string) CommandOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithConfigKey(v string) CommandOption {
|
|
||||||
return func(o *Command) {
|
|
||||||
o.configKey = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// ~ Constructor
|
// ~ Constructor
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
func NewCommand(l log.Logger, opts ...CommandOption) *Command {
|
func NewCommand(l log.Logger, harbor *Harbor, opts ...CommandOption) *Command {
|
||||||
inst := &Command{
|
inst := &Command{
|
||||||
l: l.Named("harbor"),
|
l: l.Named("harbor"),
|
||||||
name: "harbor",
|
name: "harbor",
|
||||||
configKey: "harbor",
|
harbor: harbor,
|
||||||
}
|
}
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
if opt != nil {
|
if opt != nil {
|
||||||
opt(inst)
|
opt(inst)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := viper.UnmarshalKey(inst.configKey, &inst.cfg); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
inst.commandTree = tree.New(&tree.Node{
|
inst.commandTree = tree.New(&tree.Node{
|
||||||
Name: inst.name,
|
Name: inst.name,
|
||||||
@ -115,7 +104,7 @@ func (c *Command) Help(ctx context.Context, r *readline.Readline) string {
|
|||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
func (c *Command) auth(ctx context.Context, r *readline.Readline) error {
|
func (c *Command) auth(ctx context.Context, r *readline.Readline) error {
|
||||||
return browser.OpenRawURL(c.cfg.AuthURL)
|
return browser.OpenRawURL(c.harbor.Config().AuthURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) docker(ctx context.Context, r *readline.Readline) error {
|
func (c *Command) docker(ctx context.Context, r *readline.Readline) error {
|
||||||
@ -136,11 +125,11 @@ func (c *Command) docker(ctx context.Context, r *readline.Readline) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
pterm.Info.Println("registry: " + c.cfg.URL)
|
pterm.Info.Println("registry: " + c.harbor.Config().DockerRegistry())
|
||||||
pterm.Info.Println("username: " + username)
|
pterm.Info.Println("username: " + username)
|
||||||
pterm.Info.Println("please enter your CLI secret as password...")
|
pterm.Info.Println("please enter your CLI secret as password...")
|
||||||
|
|
||||||
return shell.New(ctx, c.l, "docker", "login", c.cfg.URL, "-u", username).
|
return shell.New(ctx, c.l, "docker", "login", c.harbor.Config().URL, "-u", username).
|
||||||
Args(r.AdditionalArgs()...).
|
Args(r.AdditionalArgs()...).
|
||||||
Args(r.AdditionalFlags()...).
|
Args(r.AdditionalFlags()...).
|
||||||
Run()
|
Run()
|
||||||
|
|||||||
@ -1,6 +1,15 @@
|
|||||||
package harbor
|
package harbor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
URL string `json:"url" yaml:"url"`
|
URL string `json:"url" yaml:"url"`
|
||||||
AuthURL string `json:"authUrl" yaml:"authUrl"`
|
AuthURL string `json:"authUrl" yaml:"authUrl"`
|
||||||
|
Project string `json:"project" yaml:"project"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) DockerRegistry() string {
|
||||||
|
return strings.TrimPrefix(c.URL, "https://")
|
||||||
}
|
}
|
||||||
|
|||||||
85
goharbor/harbor/harbor.go
Normal file
85
goharbor/harbor/harbor.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package harbor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/foomo/posh/pkg/log"
|
||||||
|
"github.com/foomo/posh/pkg/shell"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Harbor command
|
||||||
|
type (
|
||||||
|
Harbor struct {
|
||||||
|
l log.Logger
|
||||||
|
cfg Config
|
||||||
|
configKey string
|
||||||
|
signedIn bool
|
||||||
|
signedInTime time.Time
|
||||||
|
}
|
||||||
|
Option func(*Harbor) error
|
||||||
|
)
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// ~ Options
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func CommandWithConfigKey(v string) Option {
|
||||||
|
return func(o *Harbor) error {
|
||||||
|
o.configKey = v
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// ~ Constructor
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// New command
|
||||||
|
func New(l log.Logger, opts ...Option) (*Harbor, error) {
|
||||||
|
inst := &Harbor{
|
||||||
|
l: l,
|
||||||
|
configKey: "harbor",
|
||||||
|
}
|
||||||
|
for _, opt := range opts {
|
||||||
|
if opt != nil {
|
||||||
|
if err := opt(inst); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := viper.UnmarshalKey(inst.configKey, &inst.cfg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return inst, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// ~ Public methods
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func (t *Harbor) Config() Config {
|
||||||
|
return t.cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Harbor) IsAuthenticated(ctx context.Context) bool {
|
||||||
|
if t.signedIn && time.Since(t.signedInTime) < time.Hour {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
out, _ := shell.New(ctx, t.l,
|
||||||
|
"docker",
|
||||||
|
"pull",
|
||||||
|
fmt.Sprintf("%s/%s/null:null", t.cfg.DockerRegistry(), t.cfg.Project),
|
||||||
|
).Quiet().CombinedOutput()
|
||||||
|
if strings.HasPrefix(string(out), "Error response from daemon: unknown") {
|
||||||
|
t.signedIn = true
|
||||||
|
t.signedInTime = time.Now()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
t.signedIn = false
|
||||||
|
return false
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user