-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstaller.go
More file actions
244 lines (193 loc) · 6.16 KB
/
installer.go
File metadata and controls
244 lines (193 loc) · 6.16 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package github
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"path/filepath"
"sync"
"github.com/bool64/ctxd"
"github.com/google/go-github/v35/github"
_ "github.com/nhatthm/plugin-registry-fs" // Add filesystem installer.
fsCtx "github.com/nhatthm/plugin-registry/context"
"github.com/nhatthm/plugin-registry/installer"
"github.com/nhatthm/plugin-registry/plugin"
"github.com/spf13/afero"
)
const githubHostname = "github.com"
var (
// ErrNotGithub indicates the url is not github.
ErrNotGithub = errors.New("not a github url")
// ErrMissingOwner indicates that there is no owner in the url.
ErrMissingOwner = errors.New("missing github owner")
// ErrMissingRepository indicates that there is no repository in the url.
ErrMissingRepository = errors.New("missing github repository")
)
func init() { //nolint: gochecknoinits
RegisterInstaller()
}
type contextKey string
// Option is option to configure Installer.
type Option func(i *Installer)
// Installer installs plugin from github.
type Installer struct {
fs afero.Fs
service RepositoryService
baseURL *url.URL
mu sync.Mutex
}
// Install installs the plugin.
// The installer will download the archive or binary from github, then uses the filesystem installers to install the
// plugin.
func (i *Installer) Install(ctx context.Context, dest, source string) (*plugin.Plugin, error) {
i.mu.Lock()
defer i.mu.Unlock()
owner, repository, version, err := parseURL(source)
if err != nil {
return nil, parseError(err, source) //nolint: contextcheck
}
ctx = context.WithValue(ctx, contextKey("source"), source)
ctx = context.WithValue(ctx, contextKey("owner"), owner)
ctx = context.WithValue(ctx, contextKey("repository"), repository)
return i.install(ctx, dest, owner, repository, version)
}
func (i *Installer) install(ctx context.Context, dest, owner, repository, version string) (*plugin.Plugin, error) {
if version == "" || version == "latest" {
r, _, err := i.service.GetLatestRelease(ctx, owner, repository)
if err != nil {
return nil, ctxd.WrapError(ctx, err, "could not find latest release")
}
if r.TagName == nil {
return nil, ctxd.NewError(ctx, "latest release has no tag name")
}
return i.installRelease(ctx, dest, owner, repository, r)
}
r, _, err := i.service.GetReleaseByTag(ctx, owner, repository, version)
if err != nil {
return nil, ctxd.WrapError(ctx, err, "could not get release")
}
return i.installRelease(ctx, dest, owner, repository, r)
}
func (i *Installer) installRelease(ctx context.Context, dest string, owner, repository string, release *github.RepositoryRelease) (*plugin.Plugin, error) {
r, _, err := i.service.DownloadContents(ctx, owner, repository, plugin.MetadataFile, &github.RepositoryContentGetOptions{
Ref: *release.TagName,
})
if err != nil {
return nil, ctxd.WrapError(ctx, err, "could not get plugin metadata", "version", *release.TagName)
}
p, err := loadMetadata(r)
if err != nil {
return nil, ctxd.WrapError(ctx, err, "could not load plugin metadata")
}
p.Version = trimVersion(*release.TagName)
p.URL = fmt.Sprintf("https://github.com/%s/%s", owner, repository)
return i.installPluginRelease(ctx, dest, owner, repository, p, release)
}
func (i *Installer) installPluginRelease(
ctx context.Context,
dest string,
owner, repository string,
p *plugin.Plugin,
release *github.RepositoryRelease,
) (*plugin.Plugin, error) {
artifact := p.ResolveArtifact(p.RuntimeArtifact())
asset, err := findAsset(release, artifact.File)
if err != nil {
return nil, ctxd.WrapError(ctx, err, "could not find artifact")
}
return i.installPluginReleaseAsset(ctx, dest, owner, repository, p, asset)
}
func (i *Installer) installPluginReleaseAsset(
ctx context.Context,
dest string,
owner, repository string,
p *plugin.Plugin,
asset *github.ReleaseAsset,
) (*plugin.Plugin, error) {
ctx = context.WithValue(ctx, contextKey("asset"), asset)
r, _, err := i.service.DownloadReleaseAsset(ctx, owner, repository, *asset.ID, http.DefaultClient)
if err != nil {
return nil, ctxd.WrapError(ctx, err, "could not download artifact")
}
tmpDir, err := afero.TempDir(i.fs, "", "plugin-registry-github-")
if err != nil {
return nil, ctxd.WrapError(ctx, err, "could not create temp dir")
}
defer func() {
_ = i.fs.RemoveAll(tmpDir) //nolint: errcheck
}()
assetFile := filepath.Join(tmpDir, *asset.Name)
if err := writeFile(i.fs, assetFile, r); err != nil {
return nil, ctxd.WrapError(ctx, err, "could not write artifact")
}
if err := chmod(i.fs, asset.ContentType, assetFile, 0o755); err != nil {
return nil, ctxd.WrapError(ctx, err, "could not chmod artifact")
}
if err := writeMetadata(i.fs, tmpDir, p); err != nil {
return nil, ctxd.WrapError(ctx, err, "could not write plugin metadata")
}
source := assetFile
if filepath.Ext(assetFile) == "" {
source = tmpDir
}
ctx = fsCtx.WithFs(ctx, i.fs)
pkgInstaller, err := installer.Find(ctx, source)
if err != nil {
return nil, err
}
return pkgInstaller.Install(ctx, dest, source)
}
// WithService sets the repository service.
func (i *Installer) WithService(service RepositoryService) *Installer {
i.mu.Lock()
defer i.mu.Unlock()
i.service = service
return i
}
// NewInstaller initiates a new github installer.
func NewInstaller(options ...Option) *Installer {
i := &Installer{
fs: afero.NewOsFs(),
}
for _, o := range options {
o(i)
}
if i.service == nil {
c := github.NewClient(nil)
if i.baseURL != nil {
c.BaseURL = i.baseURL
}
i.service = c.Repositories
}
return i
}
// WithFs sets the file system.
func WithFs(fs afero.Fs) Option {
return func(i *Installer) {
i.fs = fs
}
}
// WithService sets the repository service.
func WithService(service RepositoryService) Option {
return func(i *Installer) {
i.WithService(service)
}
}
// WithBaseURL sets the github base url.
func WithBaseURL(url *url.URL) Option {
return func(i *Installer) {
i.baseURL = url
}
}
// RegisterInstaller registers the installer.
func RegisterInstaller(options ...Option) {
installer.Register(githubHostname,
func(_ context.Context, pluginURL string) bool {
return isPlugin(pluginURL)
},
func(fs afero.Fs) installer.Installer {
return NewInstaller(append(options, WithFs(fs))...)
},
)
}