Skip to content

Commit 78dc305

Browse files
committed
refactor: streamline version handling in HostData
- Simplified version extraction by using strings.TrimPrefix for various version fields in the HostData structure. - Removed redundant checks for "v" prefix in version strings, ensuring cleaner and more efficient code. - Enhanced the buildXClientHeader function to consistently handle version formatting. These changes improve code readability and maintainability while ensuring consistent version handling across the SDK.
1 parent a805009 commit 78dc305

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

pkg/qase-go/clients/host_data.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ func GetHostInfo() *HostData {
4242
arch := runtime.GOARCH
4343

4444
// Get Go version and remove "go" prefix
45-
goVersion := runtime.Version()
46-
goVersion = strings.TrimPrefix(goVersion, "go")
45+
goVersion := strings.TrimPrefix(runtime.Version(), "go")
4746

4847
// Try to get module versions from go.mod
4948
apiClientV1Version, apiClientV2Version := getModuleVersionsFromGoMod()
@@ -130,10 +129,8 @@ func getModuleVersionsFromGoMod() (string, string) {
130129
version := req.Mod.Version
131130

132131
if strings.Contains(modulePath, "qase-api-client") && !strings.Contains(modulePath, "qase-api-v2-client") {
133-
// Remove "v" prefix if present, we'll add it later in buildXClientHeader
134132
apiClientV1Version = strings.TrimPrefix(version, "v")
135133
} else if strings.Contains(modulePath, "qase-api-v2-client") {
136-
// Remove "v" prefix if present, we'll add it later in buildXClientHeader
137134
apiClientV2Version = strings.TrimPrefix(version, "v")
138135
}
139136
}
@@ -155,10 +152,7 @@ func buildXClientHeader(hostData *HostData) string {
155152
}
156153

157154
if hostData.ReporterVersion != "" {
158-
version := hostData.ReporterVersion
159-
if !strings.HasPrefix(version, "v") {
160-
version = "v" + version
161-
}
155+
version := strings.TrimPrefix(hostData.ReporterVersion, "v")
162156
parts = append(parts, "reporter_version="+version)
163157
}
164158

@@ -167,31 +161,28 @@ func buildXClientHeader(hostData *HostData) string {
167161
}
168162

169163
if hostData.FrameworkVersion != "" {
170-
version := hostData.FrameworkVersion
171-
if !strings.HasPrefix(version, "v") {
172-
version = "v" + version
173-
}
164+
version := strings.TrimPrefix(hostData.FrameworkVersion, "v")
174165
parts = append(parts, "framework_version="+version)
175166
}
176167

177168
if hostData.APIClientV1 != "" {
178-
version := hostData.APIClientV1
169+
version := strings.TrimPrefix(hostData.APIClientV1, "v")
179170
if !strings.HasPrefix(version, "v") {
180171
version = "v" + version
181172
}
182173
parts = append(parts, "client_version_v1="+version)
183174
}
184175

185176
if hostData.APIClientV2 != "" {
186-
version := hostData.APIClientV2
177+
version := strings.TrimPrefix(hostData.APIClientV2, "v")
187178
if !strings.HasPrefix(version, "v") {
188179
version = "v" + version
189180
}
190181
parts = append(parts, "client_version_v2="+version)
191182
}
192183

193184
if hostData.Commons != "" {
194-
version := hostData.Commons
185+
version := strings.TrimPrefix(hostData.Commons, "v")
195186
if !strings.HasPrefix(version, "v") {
196187
version = "v" + version
197188
}

0 commit comments

Comments
 (0)