This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
add.go
165 lines (142 loc) · 4.52 KB
/
add.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
163
164
165
package plugins
import (
"fmt"
"net/url"
"strings"
"github.com/KablamoOSS/go-cli-printer"
"github.com/KablamoOSS/kombustion/config"
"github.com/KablamoOSS/kombustion/internal/core"
manifestType "github.com/KablamoOSS/kombustion/internal/manifest"
"github.com/KablamoOSS/kombustion/internal/plugins/lock"
"github.com/google/go-github/github"
)
var githubClient *github.Client
func init() {
githubClient = github.NewClient(nil)
}
// AddPluginsToManifest - Add all new plugin to the manifest
// update it if it's already there
// then write the manifest to disk
func AddPluginsToManifest(objectStore core.ObjectStore, manifest *manifestType.Manifest, pluginLocations []string, manifestLocation string) (*manifestType.Manifest, error) {
printer.Progress("Kombusting")
// Get the lockFile
lockFile, err := lock.GetLockObject(objectStore, "kombustion.lock")
// Add all the plugins to the manifest and lockfile
manifest, lockFile, err = addPluginsToManifestAndLock(manifest, lockFile, pluginLocations)
if err != nil {
printer.Error(err, config.ErrorHelpInfo, "")
return manifest, err
}
printer.Progress("Updating manifest")
err = manifestType.WriteManifestObject(objectStore, manifest, manifestLocation)
if err != nil {
printer.Error(err, config.ErrorHelpInfo, "")
return manifest, err
}
err = lockFile.Save(objectStore)
if err != nil {
printer.Error(err, fmt.Sprintf("%s\n%s", "There was an error updating lockfile. Try your previous command again, and if that fails you may need to remove kombustion.lock entirely.", config.ErrorHelpInfo), "")
return manifest, err
}
return manifest, nil
}
func addPluginsToManifestAndLock(
manifest *manifestType.Manifest,
lockFile *lock.Lock,
pluginLocations []string,
) (
*manifestType.Manifest,
*lock.Lock,
error,
) {
for _, pluginLocation := range pluginLocations {
plugin, pluginLock, err := constructGithubPlugin(manifest, pluginLocation)
printer.SubStep(fmt.Sprintf("Adding plugin: %s", plugin.Name), 2, true, false)
if err != nil {
return manifest, lockFile, err
}
if manifest.Plugins == nil {
manifest.Plugins = make(map[string]manifestType.Plugin)
}
if lockFile.Plugins == nil {
lockFile.Plugins = make(map[string]lock.Plugin)
}
manifest.Plugins[fmt.Sprintf("%s@%s", plugin.Name, plugin.Version)] = plugin
lockFile.Plugins[fmt.Sprintf("%s@%s", plugin.Name, plugin.Version)] = pluginLock
}
return manifest, lockFile, nil
}
// constructGithubPlugin - Create a plugin based on a github url
func constructGithubPlugin(
manifest *manifestType.Manifest, pluginURI string,
) (
plugin manifestType.Plugin,
pluginLock lock.Plugin,
err error,
) {
var pluginURL *url.URL
// Parse the plugin url
pluginURL, err = url.Parse(pluginURI)
if err != nil {
return plugin, pluginLock, err
}
path := strings.Split(pluginURL.Path, "/")
if path[0] != "github.com" {
return plugin, pluginLock, fmt.Errorf("Plugin must be start with github.com")
}
githubOrg := path[1]
githubProject := path[2]
plugin.Name = strings.Join([]string{
pluginURL.Host,
pluginURL.Path,
},
"",
)
pluginLock.Name = plugin.Name
latestRelease, latestReleaseErr := core.GetLatestRelease(githubClient, githubOrg, githubProject)
if latestReleaseErr != nil {
// TODO: Make this error more helpful
printer.Fatal(
latestReleaseErr,
"",
"",
)
}
if latestRelease != nil {
printer.SubStep(
fmt.Sprintf("Found release %s for %s/%s", *latestRelease.TagName, githubOrg, githubProject),
1,
true,
false,
)
// TODO: handle no release
// TODO: handle release with no files
if err != nil {
return plugin, pluginLock, latestReleaseErr
}
if pluginLock.Resolved == nil {
pluginLock.Resolved = make(map[string]lock.PluginResolution)
}
plugin.Version = *latestRelease.TagName
pluginLock.Version = plugin.Version
// Loop through the assets we found and try to create a resolution lock for them
if latestRelease.Assets != nil {
for _, release := range latestRelease.Assets {
operatingSystem, architecture, valid := core.GetOSArchFromFilename(githubProject, *release.Name)
// If the file is a valid plugin file create a resolution lock
if valid {
pluginLock.Resolved[strings.Join([]string{operatingSystem, architecture}, "-")] = lock.PluginResolution{
URL: *release.BrowserDownloadURL,
OperatingSystem: operatingSystem,
Architecture: architecture,
PathOnDisk: "",
Hash: "",
ArchiveHash: "",
ArchiveName: *release.Name,
}
}
}
}
}
return plugin, pluginLock, err
}