-
Notifications
You must be signed in to change notification settings - Fork 2
/
ghcr.go
49 lines (39 loc) · 1.04 KB
/
ghcr.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
package ghcr
import (
"context"
"os"
"strings"
"github.com/cresta/magehelper/docker/registry"
"github.com/cresta/magehelper/env"
"github.com/cresta/magehelper/pipe"
)
type Ghcr struct {
Env env.Env
}
var Instance = &Ghcr{}
var _ registry.Registry = Instance
func (e *Ghcr) Password() string {
return e.Env.GetDefault("GHCR_PAT", "PLEASE-SET-PASSWORD")
}
func (e *Ghcr) Username() string {
if d := e.Env.Get("DOCKER_USERNAME"); d != "" {
return d
}
if d := e.Env.Get("GITHUB_REPOSITORY"); d != "" {
parts := strings.SplitN(d, "/", 2)
if len(parts) == 2 {
return parts[0]
}
}
return "PLEASE-SET-USERNAME"
}
func (e *Ghcr) ContainerRegistry() string {
return "ghcr.io"
}
func (e *Ghcr) Login(ctx context.Context) error {
return pipe.NewPiped("docker", "login", "--username", e.Username(), "--password-stdin", e.ContainerRegistry()).Execute(ctx, strings.NewReader(e.Password()), os.Stdout, os.Stderr)
}
// Login will log into GHCR using password inside GHCR_PAT
func Login(ctx context.Context) error {
return Instance.Login(ctx)
}