This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
forked from Azure/draft-classic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vcs_installer.go
162 lines (137 loc) · 3.51 KB
/
vcs_installer.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package installer
import (
"os"
"sort"
"github.com/Masterminds/semver"
"github.com/Masterminds/vcs"
"k8s.io/helm/pkg/plugin/cache"
"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/Azure/draft/pkg/plugin"
)
//VCSInstaller installs plugins from a remote repository
type VCSInstaller struct {
Repo vcs.Repo
Version string
base
}
// NewVCSInstaller creates a new VCSInstaller.
func NewVCSInstaller(source, version string, home draftpath.Home) (*VCSInstaller, error) {
// create a system safe cache key
key, err := cache.Key(source)
if err != nil {
return nil, err
}
cachedpath := home.Path("cache", "plugins", key)
repo, err := vcs.NewRepo(source, cachedpath)
if err != nil {
return nil, err
}
i := &VCSInstaller{
Repo: repo,
Version: version,
base: newBase(source, home),
}
return i, err
}
// Install clones a remote repository and creates a symlink to the plugin directory in DRAFT_HOME
//
// Implements Installer
func (i *VCSInstaller) Install() error {
if err := i.sync(i.Repo); err != nil {
return err
}
ref, err := i.solveVersion(i.Repo)
if err != nil {
return err
}
if err := i.setVersion(i.Repo, ref); err != nil {
return err
}
if !isPlugin(i.Repo.LocalPath()) {
return plugin.ErrMissingMetadata
}
return i.link(i.Repo.LocalPath())
}
// Update updates a remote repository
func (i *VCSInstaller) Update() error {
debug("updating %s", i.Repo.Remote())
if i.Repo.IsDirty() {
return plugin.ErrRepoDirty
}
if err := i.Repo.Update(); err != nil {
return err
}
if !isPlugin(i.Repo.LocalPath()) {
return plugin.ErrMissingMetadata
}
return nil
}
func existingVCSRepo(location string, home draftpath.Home) (Installer, error) {
repo, err := vcs.NewRepo("", location)
if err != nil {
return nil, err
}
i := &VCSInstaller{
Repo: repo,
base: newBase(repo.Remote(), home),
}
return i, err
}
// Filter a list of versions to only included semantic versions. The response
// is a mapping of the original version to the semantic version.
func getSemVers(refs []string) []*semver.Version {
var sv []*semver.Version
for _, r := range refs {
if v, err := semver.NewVersion(r); err == nil {
sv = append(sv, v)
}
}
return sv
}
// setVersion attempts to checkout the version
func (i *VCSInstaller) setVersion(repo vcs.Repo, ref string) error {
debug("setting version to %q", i.Version)
return repo.UpdateVersion(ref)
}
func (i *VCSInstaller) solveVersion(repo vcs.Repo) (string, error) {
if i.Version == "" {
return "", nil
}
if repo.IsReference(i.Version) {
return i.Version, nil
}
// Create the constraint first to make sure it's valid before
// working on the repo.
constraint, err := semver.NewConstraint(i.Version)
if err != nil {
return "", err
}
// Get the tags
refs, err := repo.Tags()
if err != nil {
return "", err
}
debug("found refs: %s", refs)
// Convert and filter the list to semver.Version instances
semvers := getSemVers(refs)
// Sort semver list
sort.Sort(sort.Reverse(semver.Collection(semvers)))
for _, v := range semvers {
if constraint.Check(v) {
// If the constraint passes get the original reference
ver := v.Original()
debug("setting to %s", ver)
return ver, nil
}
}
return "", plugin.ErrVersionDoesNotExist
}
// sync will clone or update a remote repo.
func (i *VCSInstaller) sync(repo vcs.Repo) error {
if _, err := os.Stat(repo.LocalPath()); os.IsNotExist(err) {
debug("cloning %s to %s", repo.Remote(), repo.LocalPath())
return repo.Get()
}
debug("updating %s", repo.Remote())
return repo.Update()
}