-
Notifications
You must be signed in to change notification settings - Fork 90
/
fetch.go
91 lines (81 loc) · 2.45 KB
/
fetch.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
package upstream
import (
"net/url"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/upstream/types"
"github.com/replicatedhq/kots/pkg/util"
)
func FetchUpstream(upstreamURI string, fetchOptions *types.FetchOptions) (*types.Upstream, error) {
upstream, err := downloadUpstream(upstreamURI, fetchOptions)
if err != nil {
return nil, errors.Wrap(err, "download upstream failed")
}
return upstream, nil
}
func downloadUpstream(upstreamURI string, fetchOptions *types.FetchOptions) (*types.Upstream, error) {
if !util.IsURL(upstreamURI) {
return readFilesFromPath(upstreamURI)
}
var cipher *crypto.AESCipher
if fetchOptions.EncryptionKey != "" {
c, err := crypto.AESCipherFromString(fetchOptions.EncryptionKey)
if err != nil {
return nil, errors.Wrap(err, "failed to create cipher")
}
cipher = c
}
u, err := url.ParseRequestURI(upstreamURI)
if err != nil {
return nil, errors.Wrap(err, "parse request uri failed")
}
if u.Scheme == "helm" {
return downloadHelm(u, fetchOptions.HelmRepoURI)
}
if u.Scheme == "replicated" {
return downloadReplicated(
u,
fetchOptions.LocalPath,
fetchOptions.RootDir,
fetchOptions.UseAppDir,
fetchOptions.License,
fetchOptions.ConfigValues,
fetchOptions.IdentityConfig,
pickCursor(fetchOptions),
pickVersionLabel(fetchOptions),
cipher,
fetchOptions.AppSlug,
fetchOptions.AppSequence,
fetchOptions.Airgap != nil,
fetchOptions.LocalRegistry,
fetchOptions.ReportingInfo,
)
}
if u.Scheme == "git" {
return downloadGit(upstreamURI)
}
if u.Scheme == "http" || u.Scheme == "https" {
return downloadHttp(upstreamURI)
}
return nil, errors.Errorf("unknown protocol scheme %q", u.Scheme)
}
func pickVersionLabel(fetchOptions *types.FetchOptions) string {
if fetchOptions.Airgap != nil && fetchOptions.Airgap.Spec.VersionLabel != "" {
return fetchOptions.Airgap.Spec.VersionLabel
}
return fetchOptions.CurrentVersionLabel
}
func pickCursor(fetchOptions *types.FetchOptions) ReplicatedCursor {
if fetchOptions.Airgap != nil && fetchOptions.Airgap.Spec.UpdateCursor != "" {
return ReplicatedCursor{
ChannelID: fetchOptions.Airgap.Spec.ChannelID,
ChannelName: fetchOptions.Airgap.Spec.ChannelName,
Cursor: fetchOptions.Airgap.Spec.UpdateCursor,
}
}
return ReplicatedCursor{
ChannelID: fetchOptions.CurrentChannelID,
ChannelName: fetchOptions.CurrentChannelName,
Cursor: fetchOptions.CurrentCursor,
}
}