-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitlab.go
62 lines (51 loc) · 1.43 KB
/
gitlab.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
package registry
import (
"context"
"docker.io/go-docker/api/types"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/buildtool/build-tools/pkg/docker"
"io"
"strings"
)
type Gitlab struct {
dockerRegistry
Registry string `yaml:"registry" env:"CI_REGISTRY"`
Repository string `yaml:"repository" env:"CI_REGISTRY_IMAGE"`
Token string `yaml:"token" env:"CI_BUILD_TOKEN"`
}
var _ Registry = &Gitlab{}
func (r Gitlab) Name() string {
return "Gitlab"
}
func (r Gitlab) Configured() bool {
return len(r.Repository) > 0 || len(r.Registry) > 0
}
func (r Gitlab) Login(client docker.Client, out io.Writer) error {
if ok, err := client.RegistryLogin(context.Background(), types.AuthConfig{Username: "gitlab-ci-token", Password: r.Token, ServerAddress: r.Registry}); err == nil {
_, _ = fmt.Fprintln(out, ok.Status)
return nil
} else {
return err
}
}
func (r Gitlab) GetAuthConfig() types.AuthConfig {
return types.AuthConfig{Username: "gitlab-ci-token", Password: r.Token, ServerAddress: r.Registry}
}
func (r Gitlab) GetAuthInfo() string {
authBytes, _ := json.Marshal(r.GetAuthConfig())
return base64.URLEncoding.EncodeToString(authBytes)
}
func (r Gitlab) RegistryUrl() string {
if len(r.Repository) != 0 {
if strings.Contains(r.Repository, "/") {
return r.Repository[:strings.LastIndex(r.Repository, "/")]
}
return r.Repository
}
return r.Registry
}
func (r *Gitlab) Create(repository string) error {
return nil
}