/
charts.go
107 lines (87 loc) · 2.66 KB
/
charts.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
// Copyright 2023 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package pkg
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"github.com/vmware-labs/marketplace-cli/v2/internal/models"
helmChart "helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
)
//go:generate counterfeiter . ChartLoaderFunc
type ChartLoaderFunc func(name string) (*helmChart.Chart, error)
var ChartLoader ChartLoaderFunc = loader.Load
func LoadChart(chartPath string) (*models.ChartVersion, error) {
chart, err := ChartLoader(chartPath)
if err != nil {
return nil, fmt.Errorf("failed to read chart: %w", err)
}
return &models.ChartVersion{
Version: chart.Metadata.Version,
Repo: &models.Repo{
Name: chart.Name(),
},
IsExternalUrl: false,
}, nil
}
func (m *Marketplace) DownloadChart(chartURL *url.URL) (*models.ChartVersion, error) {
req, err := http.NewRequest("GET", chartURL.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to make request to download chart: %w", err)
}
resp, err := m.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to download chart: %w", err)
}
chartFile, err := os.CreateTemp("", "chart-*.tgz")
if err != nil {
return nil, fmt.Errorf("failed to create temporary local chart: %w", err)
}
_, err = io.Copy(chartFile, resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to save local chart: %w", err)
}
_ = resp.Body.Close()
_ = chartFile.Close()
chart, err := LoadChart(chartFile.Name())
if err != nil {
return nil, err
}
chart.IsExternalUrl = true
chart.HelmTarUrl = chartURL.String()
return chart, nil
}
func (m *Marketplace) AttachLocalChart(chartPath, instructions string, product *models.Product, version *models.Version) (*models.Product, error) {
chart, err := LoadChart(chartPath)
if err != nil {
return nil, err
}
uploader, err := m.GetUploader(product.PublisherDetails.OrgId)
if err != nil {
return nil, err
}
_, uploadedChartUrl, err := uploader.UploadProductFile(chartPath)
if err != nil {
return nil, err
}
chart.HelmTarUrl = uploadedChartUrl
chart.AppVersion = version.Number
chart.Readme = instructions
product.PrepForUpdate()
product.ChartVersions = []*models.ChartVersion{chart}
return m.PutProduct(product, version.IsNewVersion)
}
func (m *Marketplace) AttachPublicChart(chartPath *url.URL, instructions string, product *models.Product, version *models.Version) (*models.Product, error) {
chart, err := m.DownloadChart(chartPath)
if err != nil {
return nil, err
}
chart.AppVersion = version.Number
chart.Readme = instructions
product.PrepForUpdate()
product.ChartVersions = []*models.ChartVersion{chart}
return m.PutProduct(product, version.IsNewVersion)
}