Skip to content

Commit 8c1f73a

Browse files
refactor: extract major version logic into a separate function for improved code clarity
This update introduces a new function, getMajorVersion, to encapsulate the logic for extracting the major version from a version string. This change reduces code duplication in the getFileName and getDownloadURL functions, enhancing maintainability and readability. Additionally, a new rule has been added to the cursor.mdc file to encourage avoiding code duplication.
1 parent d88da96 commit 8c1f73a

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

.cursor/rules/cursor.mdc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ alwaysApply: true
77
# Your rule content
88

99
## Key Rules
10+
- avoid code copy pasting and duplication, refactor to function eagerly when possible
1011
- use fulnames like e.g. feature, instaed of feat
1112
- run go build after each code modification to see if app compiles
1213
- remove dead unused code

plugins/runtime-utils.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ func (p *runtimePlugin) getExtension(goos string) string {
196196
return p.Config.Download.Extension.Default
197197
}
198198

199+
// getMajorVersion extracts the major version from a version string (e.g. "17.0.10" -> "17")
200+
func (p *runtimePlugin) getMajorVersion(version string) string {
201+
if idx := strings.Index(version, "."); idx != -1 {
202+
return version[:idx]
203+
}
204+
return version
205+
}
206+
199207
// GetFileName generates the filename based on the template in plugin.yaml
200208
func (p *runtimePlugin) getFileName(version string) string {
201209
goos := runtime.GOOS
@@ -206,16 +214,10 @@ func (p *runtimePlugin) getFileName(version string) string {
206214
mappedOS := p.getMappedOS(goos)
207215
releaseVersion := p.getReleaseVersion()
208216

209-
// Extract major version from version string (e.g. "17.0.10" -> "17")
210-
majorVersion := version
211-
if idx := strings.Index(version, "."); idx != -1 {
212-
majorVersion = version[:idx]
213-
}
214-
215217
// Prepare template data
216218
data := templateData{
217219
Version: version,
218-
MajorVersion: majorVersion,
220+
MajorVersion: p.getMajorVersion(version),
219221
OS: mappedOS,
220222
Arch: mappedArch,
221223
ReleaseVersion: releaseVersion,
@@ -258,16 +260,10 @@ func (p *runtimePlugin) getDownloadURL(version string) string {
258260

259261
releaseVersion := p.getReleaseVersion()
260262

261-
// Extract major version from version string (e.g. "17.0.10" -> "17")
262-
majorVersion := version
263-
if idx := strings.Index(version, "."); idx != -1 {
264-
majorVersion = version[:idx]
265-
}
266-
267263
// Prepare template data
268264
data := templateData{
269265
Version: version,
270-
MajorVersion: majorVersion,
266+
MajorVersion: p.getMajorVersion(version),
271267
FileName: fileName,
272268
OS: mappedOS,
273269
Arch: mappedArch,

0 commit comments

Comments
 (0)