@@ -11,6 +11,7 @@ import (
11
11
"net/url"
12
12
"os"
13
13
"os/exec"
14
+ "strings"
14
15
"time"
15
16
16
17
"github.com/goharbor/go-client/pkg/harbor"
@@ -19,6 +20,7 @@ import (
19
20
"github.com/goharbor/go-client/pkg/sdk/v2.0/client"
20
21
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/project"
21
22
"github.com/pkg/errors"
23
+ "github.com/rogpeppe/go-internal/semver"
22
24
"github.com/schollz/progressbar/v3"
23
25
)
24
26
@@ -39,6 +41,8 @@ const (
39
41
helmBinaryPath = "helm"
40
42
timeout = 5 * time .Second
41
43
defaultPageSize = 10
44
+ // Define minimum required version
45
+ helmMinVersion = "v3.19.0"
42
46
)
43
47
44
48
var (
52
56
destPath string //nolint:gochecknoglobals
53
57
projectsToMigrate ProjectsToMigrateList //nolint:gochecknoglobals
54
58
55
- insecure bool //nolint:gochecknoglobals
56
- plainHttp bool //nolint:gochecknoglobals
59
+ insecure bool //nolint:gochecknoglobals
60
+ plainHttp bool //nolint:gochecknoglobals
61
+ showVersion bool //nolint:gochecknoglobals
62
+
63
+ // Version information set during build
64
+ version = "dev"
65
+ commit = "unknown"
66
+ buildDate = "unknown"
57
67
)
58
68
59
69
func init () { //nolint:gochecknoinits
@@ -70,8 +80,16 @@ func initFlags() {
70
80
flag .Var (& projectsToMigrate , "project" , "Name of the project(s) to migrate" )
71
81
flag .BoolVar (& insecure , "insecure" , false , "Skip TLS verification for helm operations" )
72
82
flag .BoolVar (& plainHttp , "plain-http" , false , "Use plain HTTP for helm operations" )
83
+ flag .BoolVar (& showVersion , "version" , false , "Show version information" )
73
84
flag .Parse ()
74
85
86
+ if showVersion {
87
+ fmt .Printf ("chartmuseum2oci version %s\n " , version )
88
+ fmt .Printf ("commit: %s\n " , commit )
89
+ fmt .Printf ("build date: %s\n " , buildDate )
90
+ os .Exit (0 )
91
+ }
92
+
75
93
if harborURL == "" {
76
94
log .Fatal (errors .New ("Missing required --url flag" ))
77
95
}
@@ -117,7 +135,70 @@ func initHarborHost() {
117
135
harborHost = u .Host
118
136
}
119
137
138
+ // checkHelmVersion checks if Helm version meets the minimum requirement
139
+
140
+ func checkHelmVersion () error {
141
+ cmd := exec .Command (helmBinaryPath , "version" , "--short" ) //nolint:gosec
142
+
143
+ var stdOut , stdErr bytes.Buffer
144
+ cmd .Stdout = & stdOut
145
+ cmd .Stderr = & stdErr
146
+
147
+ if err := cmd .Run (); err != nil {
148
+ return errors .Wrapf (err , "fail to execute helm version command: %s" , stdErr .String ())
149
+ }
150
+
151
+ versionOutput := strings .TrimSpace (stdOut .String ())
152
+
153
+ // Extract version from output like "v3.19.0+gce43812"
154
+ versionStr := extractVersionFromOutput (versionOutput )
155
+ if versionStr == "" {
156
+ return errors .Errorf ("unable to extract version from Helm output: %s" , versionOutput )
157
+ }
158
+
159
+ // Validate version format using semver library
160
+ if ! semver .IsValid ("v" + versionStr ) {
161
+ return errors .Errorf ("invalid Helm version format: %s" , versionStr )
162
+ }
163
+
164
+ // Check if version meets minimum requirement
165
+ if semver .Compare ("v" + versionStr , minVersion ) < 0 {
166
+ return errors .Errorf ("Helm version %s is too old, requires version >= %s" , versionStr , minVersion )
167
+ }
168
+
169
+ log .Printf ("Helm version check passed: %s" , versionStr )
170
+ return nil
171
+ }
172
+
173
+ // extractVersionFromOutput extracts version string from Helm version output
174
+ func extractVersionFromOutput (output string ) string {
175
+ // Handle different Helm version output formats:
176
+ // - "v3.19.0+gce43812" (with git commit)
177
+ // - "v3.19.0" (without git commit)
178
+ // - "3.19.0" (without 'v' prefix)
179
+
180
+ // Remove 'v' prefix if present
181
+ version := strings .TrimPrefix (output , "v" )
182
+
183
+ // Find the first '+' or end of string to get clean version
184
+ if plusIndex := strings .Index (version , "+" ); plusIndex != - 1 {
185
+ version = version [:plusIndex ]
186
+ }
187
+
188
+ // Validate that it looks like a semantic version
189
+ parts := strings .Split (version , "." )
190
+ if len (parts ) != 3 {
191
+ return ""
192
+ }
193
+
194
+ return version
195
+ }
196
+
120
197
func main () {
198
+ if err := checkHelmVersion (); err != nil {
199
+ log .Fatal (errors .Wrapf (err , "fail to check Helm version" ))
200
+ }
201
+
121
202
if err := helmLogin (); err != nil {
122
203
log .Fatal (errors .Wrapf (err , "fail to login to Helm" ))
123
204
}
0 commit comments