-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
92 lines (79 loc) · 1.78 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/Masterminds/semver"
)
//CountOf: Count the number of times a boolean occurs in a boolean slice. Used to count the number of flags.
func CountOf(val bool, slice []bool) int {
count := 0
for _, el := range slice {
if val == el {
count = count + 1
}
}
return count
}
//GetLatestVer: Find the folder name with the highest version in the iOS DeviceSupport folder.
func getLatestVer(dir string) (*semver.Version, error) {
//open file
d, err := os.Open(dir)
if err != nil {
return nil, err
}
defer d.Close()
//get all file names
names, err := d.Readdirnames(-1)
if err != nil {
return nil, err
}
latest := latestVer(names)
return latest, nil
}
//latestVer: Find the latest semantic version in an array of versions
func latestVer(names []string) *semver.Version {
//convert names to semver
//get latest version
var latest *semver.Version
for _, name := range names {
verStr := strings.Split(name, " ")[0]
ver, err := semver.NewVersion(verStr)
if err != nil {
continue
}
if latest == nil || latest.LessThan(ver) {
latest = ver
}
}
return latest
}
//Asks user for confirmation before proceeding.
func askForConfirmation() bool {
var response string
_, err := fmt.Scanln(&response)
if err != nil {
log.Fatal(err)
}
switch strings.ToLower(response) {
case "y", "yes":
return true
case "n", "no":
return false
default:
fmt.Println("I'm sorry but I didn't get what you meant, please type (y)es or (n)o and then press enter:")
return askForConfirmation()
}
}
// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}