Skip to content

Commit 94a133b

Browse files
committed
Fix golang ci linting issues
Signed-off-by: Edmund Ochieng <[email protected]>
1 parent 2ad868d commit 94a133b

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

internal/shared/util/helm/chart.go

+43-23
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,71 @@ import (
1414
"helm.sh/helm/v3/pkg/registry"
1515
)
1616

17-
func IsChart(ctx context.Context, chartUri string) (chart, oci bool, err error) {
18-
addr, err := url.Parse(chartUri)
17+
type HelmCheckResponse struct {
18+
// Chart returns true if helm chart
19+
Chart bool
20+
// Oci returns true if resource is stored
21+
// in an OCI registry
22+
Oci bool
23+
}
24+
25+
func IsChart(ctx context.Context, chartURI string) (HelmCheckResponse, error) {
26+
addr, err := url.Parse(chartURI)
1927
if err != nil {
20-
return chart, oci, err
28+
return HelmCheckResponse{}, err
2129
}
2230

2331
if addr.Scheme != "" {
2432
if !strings.HasPrefix(addr.Scheme, "http") {
25-
err = fmt.Errorf("unexpected Url scheme; %s\n", addr.Scheme)
26-
return
33+
return HelmCheckResponse{}, fmt.Errorf("unexpected scheme; %s", addr.Scheme)
2734
}
2835

29-
oci = false
3036
helmchart, err := validateHelmChart(addr.String())
3137
if err != nil {
32-
chart = false
33-
return chart, oci, err
38+
return HelmCheckResponse{}, err
3439
}
3540

3641
if helmchart != nil &&
3742
helmchart.Metadata != nil &&
3843
helmchart.Metadata.Name != "" {
39-
chart = true
44+
return HelmCheckResponse{
45+
Chart: true,
46+
Oci: false,
47+
}, err
4048
}
41-
42-
return chart, oci, err
4349
}
4450

4551
ociRe := regexp.MustCompile("^(?P<host>[a-zA-Z0-9-_.:]+)([/]?)(?P<org>[a-zA-Z0-9-_/]+)?([/](?P<chart>[a-zA-Z0-9-_.:@]+))$")
46-
if ociRe.MatchString(chartUri) {
47-
oci = true
52+
if !ociRe.MatchString(chartURI) {
53+
return HelmCheckResponse{
54+
Chart: false,
55+
Oci: false,
56+
}, fmt.Errorf("does not conform to OCI url format")
57+
}
4858

49-
chart, err = helmOciCheck(ctx, chartUri)
50-
if err != nil {
51-
return chart, oci, err
52-
}
59+
ociCheck, err := helmOciCheck(ctx, chartURI)
60+
if err != nil {
61+
return HelmCheckResponse{
62+
Chart: false,
63+
Oci: true,
64+
}, err
5365
}
5466

55-
return
67+
return HelmCheckResponse{
68+
Chart: ociCheck,
69+
Oci: true,
70+
}, nil
5671
}
5772

58-
// helmOciCheck() pull a helm chart using the provided chartUri from an
73+
// helmOciCheck() pull a helm chart using the provided chartURI from an
5974
// OCI registiry and inspects its media type to determine if a Helm chart
60-
func helmOciCheck(ctx context.Context, chartUri string) (bool, error) {
75+
func helmOciCheck(_ context.Context, chartURI string) (bool, error) {
6176
helmclient, err := registry.NewClient()
6277
if err != nil {
6378
return false, err
6479
}
6580

66-
summary, err := helmclient.Pull(chartUri,
81+
summary, err := helmclient.Pull(chartURI,
6782
registry.PullOptWithProv(false),
6883
registry.PullOptWithChart(true),
6984
registry.PullOptIgnoreMissingProv(true),
@@ -75,9 +90,14 @@ func helmOciCheck(ctx context.Context, chartUri string) (bool, error) {
7590
return summary != nil && summary.Ref != "", nil
7691
}
7792

78-
func validateHelmChart(chartUri string) (*chart.Chart, error) {
93+
func validateHelmChart(chartURI string) (*chart.Chart, error) {
7994
// Download helm chart from HTTP
80-
resp, err := http.Get(chartUri)
95+
req, err := http.NewRequest(http.MethodGet, chartURI, nil)
96+
if err != nil {
97+
return nil, fmt.Errorf("creating request failed; %w", err)
98+
}
99+
100+
resp, err := http.DefaultClient.Do(req)
81101
if err != nil {
82102
return nil, fmt.Errorf("loading URL failed; %w", err)
83103
}

internal/shared/util/helm/chart_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"context"
55
"testing"
66

7-
helmutils "github.com/operator-framework/operator-controller/internal/shared/util/helm"
87
"github.com/stretchr/testify/assert"
8+
9+
helmutils "github.com/operator-framework/operator-controller/internal/shared/util/helm"
910
)
1011

1112
func TestIsChart(t *testing.T) {
@@ -78,12 +79,12 @@ func TestIsChart(t *testing.T) {
7879

7980
for _, tc := range tt {
8081
t.Run(tc.name, func(t *testing.T) {
81-
chart, oci, err := helmutils.IsChart(context.Background(), tc.url)
82-
assert.Equal(t, oci, tc.want.Oci)
83-
assert.Equal(t, chart, tc.want.Chart)
82+
response, err := helmutils.IsChart(context.Background(), tc.url)
83+
assert.Equal(t, response.Oci, tc.want.Oci)
84+
assert.Equal(t, response.Chart, tc.want.Chart)
8485

8586
if testing.Verbose() {
86-
t.Logf("IsChart() is verifying if %s is a helm chart.\n The result should be %t but, got %t\n", tc.url, chart, tc.want.Chart)
87+
t.Logf("IsChart() is checking if %s is a helm chart.\n", tc.url)
8788
}
8889

8990
if !tc.wantErr {

0 commit comments

Comments
 (0)