-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
artifactory.go
75 lines (65 loc) · 1.61 KB
/
artifactory.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
package artifactory
import (
"io"
"net/http"
"os"
"github.com/argoproj/argo/v3/errors"
wfv1 "github.com/argoproj/argo/v3/pkg/apis/workflow/v1alpha1"
)
type ArtifactoryArtifactDriver struct {
Username string
Password string
}
// Download artifact from an artifactory URL
func (a *ArtifactoryArtifactDriver) Load(artifact *wfv1.Artifact, path string) error {
lf, err := os.Create(path)
if err != nil {
return err
}
defer func() {
_ = lf.Close()
}()
req, err := http.NewRequest(http.MethodGet, artifact.Artifactory.URL, nil)
if err != nil {
return err
}
req.SetBasicAuth(a.Username, a.Password)
res, err := (&http.Client{}).Do(req)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode == 404 {
return errors.New(errors.CodeNotFound, res.Status)
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
return errors.InternalErrorf("loading file from artifactory failed with reason:%s", res.Status)
}
_, err = io.Copy(lf, res.Body)
return err
}
// UpLoad artifact to an artifactory URL
func (a *ArtifactoryArtifactDriver) Save(path string, artifact *wfv1.Artifact) error {
f, err := os.Open(path)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPut, artifact.Artifactory.URL, f)
if err != nil {
return err
}
req.SetBasicAuth(a.Username, a.Password)
res, err := (&http.Client{}).Do(req)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode < 200 || res.StatusCode >= 300 {
return errors.InternalErrorf("saving file %s to artifactory failed with reason:%s", path, res.Status)
}
return nil
}