-
Notifications
You must be signed in to change notification settings - Fork 1
/
os.go
92 lines (80 loc) · 1.46 KB
/
os.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 sparkle
import (
"errors"
"fmt"
"path"
"github.com/dlclark/regexp2"
)
type OS uint8
const (
Unknown OS = iota
MacOS
Windows
Windows64
Windows32
)
var ErrUnknownOS = errors.New("unknown os")
func (os OS) String() string {
switch os {
default:
return ""
case MacOS:
return "macos"
case Windows:
return "windows"
case Windows64:
return "windows-x64"
case Windows32:
return "windows-x86"
}
}
func (os OS) MarshalText() ([]byte, error) {
if os > Windows32 {
return nil, ErrUnknownOS
}
return []byte(os.String()), nil
}
func (os *OS) UnmarshalText(text []byte) error {
s := string(text)
for i := Unknown; i <= Windows32; i++ {
if i.String() == s {
*os = i
return nil
}
}
return fmt.Errorf("%w: %s", ErrUnknownOS, text)
}
func isOS(os, target OS) bool {
switch target {
case os, Unknown:
return true
case Windows:
return os > Windows
}
return false
}
//nolint:gochecknoglobals
var (
reWin64 = regexp2.MustCompile(`amd64|x64|x86[\W_]?64|64[\W_]?bit`, regexp2.IgnoreCase)
reWin32 = regexp2.MustCompile(`386|x86(?![\W_]?64)|ia32|32[\W_]?bit`, regexp2.IgnoreCase)
)
func detectOS(name string) OS {
ext := path.Ext(name)
switch ext {
case "":
case ".dmg", ".pkg", ".mpkg":
return MacOS
case ".exe", ".msi":
is64, _ := reWin64.MatchString(name)
is32, _ := reWin32.MatchString(name)
switch {
case is64 == is32:
case is64:
return Windows64
case is32:
return Windows32
}
return Windows
}
return Unknown
}