Skip to content

Commit 6d75707

Browse files
committed
Version struct is made of integers
changed every reference and use of Version struct, moved to xPrintf to concatenate numbers with string, undefined minor and patch value is associated with -1.
1 parent 22a8747 commit 6d75707

File tree

5 files changed

+77
-61
lines changed

5 files changed

+77
-61
lines changed

commands/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func Help(notFoundError bool) {
99
theme.Title("pvm: PHP Version Manager")
10-
theme.Info("Version 1.1")
10+
theme.Info("Version 1.1.1")
1111

1212
if notFoundError {
1313
theme.Error("Command not found")

commands/install.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
)
1616

1717
type Version struct {
18-
Major string
19-
Minor string
20-
Patch string
18+
Major int
19+
Minor int
20+
Patch int
2121
Url string
2222
ThreadSafe bool
2323
}
@@ -138,24 +138,24 @@ func Install(args []string) {
138138
// find desired version
139139
var desiredVersion Version
140140

141-
if desiredMajorVersion != "" && desiredMinorVersion != "" && desiredPatchVersion != "" {
141+
if desiredMajorVersion > -1 && desiredMinorVersion > -1 && desiredPatchVersion > -1 {
142142
desiredVersion = FindExactVersion(versions, desiredMajorVersion, desiredMinorVersion, desiredPatchVersion, desireThreadSafe)
143143
}
144144

145-
if desiredMajorVersion != "" && desiredMinorVersion != "" && desiredPatchVersion == "" {
145+
if desiredMajorVersion > -1 && desiredMinorVersion > -1 && desiredPatchVersion == -1 {
146146
desiredVersion = FindLatestPatch(versions, desiredMajorVersion, desiredMinorVersion, desireThreadSafe)
147147
}
148148

149-
if desiredMajorVersion != "" && desiredMinorVersion == "" && desiredPatchVersion == "" {
149+
if desiredMajorVersion > -1 && desiredMinorVersion == -1 && desiredPatchVersion == -1 {
150150
desiredVersion = FindLatestMinor(versions, desiredMajorVersion, desireThreadSafe)
151151
}
152152

153153
if desiredVersion == (Version{}) {
154-
theme.Error("Could not find the desired version: " + args[1] + " " + threadSafeString)
154+
theme.Error(fmt.Sprintf("Could not find the desired version: %s %s", args[1], threadSafeString))
155155
return
156156
}
157157

158-
fmt.Println("Installing PHP " + desiredVersion.Major + "." + desiredVersion.Minor + "." + desiredVersion.Patch + " " + threadSafeString)
158+
fmt.Printf("Installing PHP %d.%d.%d %s\n", desiredVersion.Major, desiredVersion.Minor, desiredVersion.Patch, threadSafeString)
159159

160160
homeDir, err := os.UserHomeDir()
161161

@@ -190,7 +190,7 @@ func Install(args []string) {
190190

191191
// check if zip already exists
192192
if _, err := os.Stat(homeDir + "/.pvm/versions/" + zipFileName); err == nil {
193-
theme.Error("PHP " + desiredVersion.Major + "." + desiredVersion.Minor + "." + desiredVersion.Patch + " " + threadSafeString + " already exists")
193+
theme.Error(fmt.Sprintf("PHP %d.%d.%d %s already exists", desiredVersion.Major, desiredVersion.Minor, desiredVersion.Patch, threadSafeString))
194194
return
195195
}
196196

@@ -222,7 +222,7 @@ func Install(args []string) {
222222
log.Fatalln(err)
223223
}
224224

225-
theme.Success("Finished installing PHP " + desiredVersion.Major + "." + desiredVersion.Minor + "." + desiredVersion.Patch + " " + threadSafeString)
225+
theme.Success(fmt.Sprintf("Finished installing PHP %d.%d.%d %s", desiredVersion.Major, desiredVersion.Minor, desiredVersion.Patch, threadSafeString))
226226
}
227227

228228
func Unzip(src, dest string) error {
@@ -289,7 +289,7 @@ func Unzip(src, dest string) error {
289289
return nil
290290
}
291291

292-
func FindExactVersion(versions []Version, major string, minor string, patch string, threadSafe bool) Version {
292+
func FindExactVersion(versions []Version, major int, minor int, patch int, threadSafe bool) Version {
293293
for _, version := range versions {
294294
if version.ThreadSafe != threadSafe {
295295
continue
@@ -302,15 +302,15 @@ func FindExactVersion(versions []Version, major string, minor string, patch stri
302302
return Version{}
303303
}
304304

305-
func FindLatestPatch(versions []Version, major string, minor string, threadSafe bool) Version {
305+
func FindLatestPatch(versions []Version, major int, minor int, threadSafe bool) Version {
306306
latestPatch := Version{}
307307

308308
for _, version := range versions {
309309
if version.ThreadSafe != threadSafe {
310310
continue
311311
}
312312
if version.Major == major && version.Minor == minor {
313-
if latestPatch.Patch == "" || version.Patch > latestPatch.Patch {
313+
if latestPatch.Patch == -1 || version.Patch > latestPatch.Patch {
314314
latestPatch = version
315315
}
316316
}
@@ -319,16 +319,16 @@ func FindLatestPatch(versions []Version, major string, minor string, threadSafe
319319
return latestPatch
320320
}
321321

322-
func FindLatestMinor(versions []Version, major string, threadSafe bool) Version {
322+
func FindLatestMinor(versions []Version, major int, threadSafe bool) Version {
323323
latestMinor := Version{}
324324

325325
for _, version := range versions {
326326
if version.ThreadSafe != threadSafe {
327327
continue
328328
}
329329
if version.Major == major {
330-
if latestMinor.Minor == "" || version.Minor > latestMinor.Minor {
331-
if latestMinor.Patch == "" || version.Patch > latestMinor.Patch {
330+
if latestMinor.Minor == -1 || version.Minor > latestMinor.Minor {
331+
if latestMinor.Patch == -1 || version.Patch > latestMinor.Patch {
332332
latestMinor = version
333333
}
334334
}

commands/use.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func Use(args []string) {
6969
// check if version exists
7070
var selectedVersion *versionMeta
7171
for _, version := range availableVersions {
72-
if version.number.Major+"."+version.number.Minor+"."+version.number.Patch == args[0] {
72+
if fmt.Sprintf("%d.%d.%d", version.number.Major, version.number.Minor, version.number.Patch) == args[0] {
7373
if threadSafe && !strings.Contains(version.folder.Name(), "nts") {
7474
selectedVersion = &versionMeta{
7575
number: version.number,
@@ -90,7 +90,7 @@ func Use(args []string) {
9090
availableVersions = sortVersions(availableVersions)
9191

9292
for _, version := range availableVersions {
93-
if version.number.Major+"."+version.number.Minor == args[0] {
93+
if fmt.Sprintf("%d.%d", version.number.Major, version.number.Minor) == args[0] {
9494
if threadSafe && !strings.Contains(version.folder.Name(), "nts") {
9595
selectedVersion = &versionMeta{
9696
number: version.number,

commands/use_test.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,49 @@ func Test_sortVersions_sortsVersionsDescending(t *testing.T) {
3131
input := []versionMeta{
3232
{
3333
number: common.Version{
34-
Major: "7",
35-
Minor: "4",
36-
Patch: "1",
34+
Major: 7,
35+
Minor: 4,
36+
Patch: 1,
3737
},
3838
folder: fakeDirEntry{},
3939
},
4040
{
4141
number: common.Version{
42-
Major: "7",
43-
Minor: "4",
44-
Patch: "2",
42+
Major: 7,
43+
Minor: 4,
44+
Patch: 2,
4545
},
4646
folder: fakeDirEntry{},
4747
},
4848
{
4949
number: common.Version{
50-
Major: "8",
51-
Minor: "3",
52-
Patch: "0",
50+
Major: 8,
51+
Minor: 3,
52+
Patch: 0,
5353
},
5454
folder: fakeDirEntry{},
5555
},
5656
{
5757
number: common.Version{
58-
Major: "8",
59-
Minor: "3",
60-
Patch: "1",
58+
Major: 8,
59+
Minor: 3,
60+
Patch: 1,
6161
},
6262
folder: fakeDirEntry{},
6363
},
6464
{
6565
number: common.Version{
66-
Major: "8",
67-
Minor: "2",
68-
Patch: "0",
66+
Major: 8,
67+
Minor: 2,
68+
Patch: 0,
6969
},
7070
folder: fakeDirEntry{},
7171
},
7272
{
7373
number: common.Version{
74-
Major: "8",
75-
Minor: "2",
76-
Patch: "5",
74+
Major: 8,
75+
Minor: 2,
76+
Patch: 5,
7777
},
7878
folder: fakeDirEntry{},
7979
},
@@ -84,49 +84,49 @@ func Test_sortVersions_sortsVersionsDescending(t *testing.T) {
8484
assert.Equal(t, []versionMeta{
8585
{
8686
number: common.Version{
87-
Major: "8",
88-
Minor: "3",
89-
Patch: "1",
87+
Major: 8,
88+
Minor: 3,
89+
Patch: 1,
9090
},
9191
folder: fakeDirEntry{},
9292
},
9393
{
9494
number: common.Version{
95-
Major: "8",
96-
Minor: "3",
97-
Patch: "0",
95+
Major: 8,
96+
Minor: 3,
97+
Patch: 0,
9898
},
9999
folder: fakeDirEntry{},
100100
},
101101
{
102102
number: common.Version{
103-
Major: "8",
104-
Minor: "2",
105-
Patch: "5",
103+
Major: 8,
104+
Minor: 2,
105+
Patch: 5,
106106
},
107107
folder: fakeDirEntry{},
108108
},
109109
{
110110
number: common.Version{
111-
Major: "8",
112-
Minor: "2",
113-
Patch: "0",
111+
Major: 8,
112+
Minor: 2,
113+
Patch: 0,
114114
},
115115
folder: fakeDirEntry{},
116116
},
117117
{
118118
number: common.Version{
119-
Major: "7",
120-
Minor: "4",
121-
Patch: "2",
119+
Major: 7,
120+
Minor: 4,
121+
Patch: 2,
122122
},
123123
folder: fakeDirEntry{},
124124
},
125125
{
126126
number: common.Version{
127-
Major: "7",
128-
Minor: "4",
129-
Patch: "1",
127+
Major: 7,
128+
Minor: 4,
129+
Patch: 1,
130130
},
131131
folder: fakeDirEntry{},
132132
},

common/helpers.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package common
33
import (
44
"fmt"
55
"regexp"
6+
"strconv"
67
)
78

89
type Version struct {
9-
Major string
10-
Minor string
11-
Patch string
10+
Major int
11+
Minor int
12+
Patch int
1213
}
1314

1415
func (v Version) String() string {
@@ -22,9 +23,24 @@ func GetVersion(text string) Version {
2223
return Version{}
2324
}
2425

26+
major, err := strconv.Atoi(matches[0][1])
27+
if err != nil {
28+
major = -1
29+
}
30+
31+
minor, err := strconv.Atoi(matches[0][2])
32+
if err != nil {
33+
minor = -1
34+
}
35+
36+
patch, err := strconv.Atoi(matches[0][3])
37+
if err != nil {
38+
patch = -1
39+
}
40+
2541
return Version{
26-
Major: matches[0][1],
27-
Minor: matches[0][2],
28-
Patch: matches[0][3],
42+
Major: major,
43+
Minor: minor,
44+
Patch: patch,
2945
}
3046
}

0 commit comments

Comments
 (0)