-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
195 lines (162 loc) · 5.41 KB
/
plugin.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
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
// Package plasmactlpublish implements a publish launchr plugin
package plasmactlpublish
import (
"errors"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"github.com/launchrctl/keyring"
"github.com/launchrctl/launchr"
)
func init() {
launchr.RegisterPlugin(&Plugin{})
}
// Plugin is [launchr.Plugin] providing publish action.
type Plugin struct {
k keyring.Keyring
}
// PluginInfo implements [launchr.Plugin] interface.
func (p *Plugin) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{
Weight: 10,
}
}
// OnAppInit implements [launchr.OnAppInitPlugin] interface.
func (p *Plugin) OnAppInit(app launchr.App) error {
app.GetService(&p.k)
return nil
}
// CobraAddCommands implements [launchr.CobraPlugin] interface to provide bump functionality.
func (p *Plugin) CobraAddCommands(rootCmd *launchr.Command) error {
var username string
var password string
var pblCmd = &launchr.Command{
Use: "publish",
Short: "Upload local artifact archive to private repository",
RunE: func(cmd *launchr.Command, _ []string) error {
// Don't show usage help on a runtime error.
cmd.SilenceUsage = true
return publish(username, password, p.k)
},
}
pblCmd.Flags().StringVarP(&username, "username", "", "", "Username for artifact repository")
pblCmd.Flags().StringVarP(&password, "password", "", "", "Password for artifact repository")
rootCmd.AddCommand(pblCmd)
return nil
}
func publish(username, password string, k keyring.Keyring) error {
// Get repository information
repoName, lastCommitShortSHA, err := getRepoInfo()
if err != nil {
launchr.Log().Error("error", "error", err)
return errors.New("error getting repository information")
}
// Construct artifact file name
archiveFile := fmt.Sprintf("%s-%s-plasma-src.tar.gz", repoName, lastCommitShortSHA)
// Variables
artifactDir := ".compose/artifacts"
artifactPath := filepath.Join(artifactDir, archiveFile)
artifactsRepositoryDomain := "https://repositories.skilld.cloud"
// Check if the other repository is accessible
var accessibilityCode int
if isURLAccessible("http://repositories.interaction.svc.skilld:8081", &accessibilityCode) {
artifactsRepositoryDomain = "http://repositories.interaction.svc.skilld:8081"
}
artifactArchiveURL := fmt.Sprintf("%s/repository/%s-artifacts/%s", artifactsRepositoryDomain, repoName, archiveFile)
launchr.Log().Info("artifact info",
"ARTIFACT_DIR", artifactDir,
"ARTIFACT_FILE", archiveFile,
"ARTIFACTS_REPOSITORY_DOMAIN", artifactsRepositoryDomain,
"ARTIFACT_ARCHIVE_URL", artifactArchiveURL,
"URL Accessibility Code", accessibilityCode,
)
err = listFiles(artifactDir)
if err != nil {
return err
}
// Check if artifact file exists
if _, err = os.Stat(artifactPath); os.IsNotExist(err) {
return fmt.Errorf("artifact %s not found in %s. Execute 'plasmactl package' before", archiveFile, artifactDir)
}
launchr.Term().Printfln("Looking for artifact %s in %s", archiveFile, artifactDir)
file, err := os.Open(path.Clean(artifactPath))
if err != nil {
launchr.Log().Error("error", "error", err)
return errors.New("error opening artifact file")
}
defer file.Close()
client := &http.Client{}
launchr.Term().Println("Getting credentials")
ci, save, err := getCredentials(artifactsRepositoryDomain, username, password, k)
if err != nil {
return err
}
authRequest, err := http.NewRequest(http.MethodHead, artifactsRepositoryDomain, http.NoBody)
if err != nil {
launchr.Log().Error("error", "error", err)
return errors.New("error creating HTTP request")
}
authRequest.SetBasicAuth(ci.Username, ci.Password)
respAuth, err := client.Do(authRequest)
if respAuth.StatusCode != http.StatusOK {
return fmt.Errorf("failed to upload artifact: %s", respAuth.Status)
}
uploadRequest, err := http.NewRequest("PUT", artifactArchiveURL, file)
if err != nil {
return err
}
uploadRequest.SetBasicAuth(ci.Username, ci.Password)
launchr.Term().Printfln("Publishing artifact %s/%s to %s...", artifactDir, archiveFile, artifactArchiveURL)
respUpload, err := client.Do(uploadRequest)
if err != nil {
launchr.Log().Error("error", "error", err)
return errors.New("error uploading artifact")
}
defer respUpload.Body.Close()
if respUpload.StatusCode != http.StatusOK && respUpload.StatusCode != http.StatusCreated {
return fmt.Errorf("failed to upload artifact: %s", respUpload.Status)
}
launchr.Term().Success().Println("Artifact successfully uploaded")
defer func() {
if save {
err = k.Save()
if err != nil {
launchr.Log().Error("error during saving keyring file", "error", err)
}
}
}()
return nil
}
func getCredentials(url, username, password string, k keyring.Keyring) (keyring.CredentialsItem, bool, error) {
ci, err := k.GetForURL(url)
save := false
if err != nil {
if errors.Is(err, keyring.ErrEmptyPass) {
return ci, false, err
} else if !errors.Is(err, keyring.ErrNotFound) {
launchr.Log().Error("error", "error", err)
return ci, false, errors.New("the keyring is malformed or wrong passphrase provided")
}
ci = keyring.CredentialsItem{}
ci.URL = url
ci.Username = username
ci.Password = password
if ci.Username == "" || ci.Password == "" {
if ci.URL != "" {
launchr.Term().Info().Printfln("Please add login and password for URL - %s", ci.URL)
}
err = keyring.RequestCredentialsFromTty(&ci)
if err != nil {
return ci, false, err
}
}
err = k.AddItem(ci)
if err != nil {
return ci, false, err
}
save = true
}
return ci, save, nil
}