-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.go
147 lines (109 loc) · 3.71 KB
/
git.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
package git
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"sync"
"github.com/alexfalkowski/go-service/file"
"github.com/alexfalkowski/go-service/meta"
source "github.com/alexfalkowski/konfig/source/configurator"
cerrors "github.com/alexfalkowski/konfig/source/configurator/errors"
"github.com/alexfalkowski/konfig/source/configurator/git/opentracing"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
gclient "github.com/go-git/go-git/v5/plumbing/transport/client"
ghttp "github.com/go-git/go-git/v5/plumbing/transport/http"
)
// NewConfigurator for git.
func NewConfigurator(cfg Config, tracer opentracing.Tracer, client *http.Client) *Configurator {
c := ghttp.NewClient(client)
gclient.InstallProtocol("http", c)
gclient.InstallProtocol("https", c)
return &Configurator{cfg: cfg, tracer: tracer}
}
// Configurator for git.
type Configurator struct {
cfg Config
repo *git.Repository
mux sync.Mutex
tracer opentracing.Tracer
}
// GetConfig for git.
func (c *Configurator) GetConfig(ctx context.Context, params source.ConfigParams) (*source.Config, error) {
c.mux.Lock()
defer c.mux.Unlock()
if err := c.clone(ctx); err != nil {
meta.WithAttribute(ctx, "git.clone_error", err.Error())
return nil, err
}
if err := c.pull(ctx); err != nil {
meta.WithAttribute(ctx, "git.pull_error", err.Error())
return nil, err
}
if err := c.checkout(ctx, params.Application, params.Version); err != nil {
meta.WithAttribute(ctx, "git.checkout_error", err.Error())
if errors.Is(err, plumbing.ErrReferenceNotFound) {
return nil, cerrors.ErrNotFound
}
return nil, err
}
p := c.path(params.Application, params.Environment, params.Continent, params.Country, params.Command, params.Kind)
path := filepath.Join(c.cfg.Dir, p)
data, err := os.ReadFile(filepath.Clean(path))
if err != nil {
meta.WithAttribute(ctx, "git.file_error", err.Error())
if os.IsNotExist(err) {
return nil, cerrors.ErrNotFound
}
return nil, err
}
return &source.Config{Kind: file.Extension(path), Data: data}, nil
}
func (c *Configurator) checkout(ctx context.Context, app, ver string) error {
tag := fmt.Sprintf("%s/%s", app, ver)
_, span := opentracing.StartSpanFromContext(ctx, c.tracer, "checkout", tag)
defer span.Finish()
tree, _ := c.repo.Worktree()
return tree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewTagReferenceName(tag)})
}
func (c *Configurator) pull(ctx context.Context) error {
ctx, span := opentracing.StartSpanFromContext(ctx, c.tracer, "pull", plumbing.Master.String())
defer span.Finish()
tree, _ := c.repo.Worktree()
if err := tree.Checkout(&git.CheckoutOptions{Branch: plumbing.Master}); err != nil {
return err
}
if err := tree.PullContext(ctx, &git.PullOptions{RemoteName: "origin"}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return err
}
return nil
}
func (c *Configurator) clone(ctx context.Context) error {
ctx, span := opentracing.StartSpanFromContext(ctx, c.tracer, "clone", c.cfg.URL)
defer span.Finish()
if c.repo != nil {
return nil
}
if err := os.RemoveAll(c.cfg.Dir); err != nil {
return err
}
opts := &git.CloneOptions{Auth: &ghttp.BasicAuth{Username: "a", Password: c.cfg.Token()}, URL: c.cfg.URL}
r, err := git.PlainCloneContext(ctx, c.cfg.Dir, false, opts)
if err != nil {
return err
}
c.repo = r
return nil
}
func (c *Configurator) path(app, env, continent, country, cmd, kind string) string {
if continent == "*" && country == "*" {
return fmt.Sprintf("%s/%s/%s.%s", app, env, cmd, kind)
}
if continent != "*" && country == "*" {
return fmt.Sprintf("%s/%s/%s/%s.%s", app, env, continent, cmd, kind)
}
return fmt.Sprintf("%s/%s/%s/%s/%s.%s", app, env, continent, country, cmd, kind)
}