├── .gitignore ├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── osversion.go ├── osversion ├── .gitignore ├── Makefile ├── main.go └── main_android.go ├── osversion_android.go ├── osversion_darwin.go ├── osversion_linux.go ├── osversion_test.go └── osversion_windows.go /.gitignore: -------------------------------------------------------------------------------- 1 | cmd/cmd 2 | cmd/osversion 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2018 Brave New Software Project, Inc. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go library for OS version discovery 2 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/getlantern/osversion 2 | 3 | go 1.22.0 4 | 5 | require ( 6 | github.com/blang/semver v3.5.1+incompatible 7 | golang.org/x/mobile v0.0.0-20240404231514-09dbf07665ed 8 | ) 9 | 10 | require ( 11 | golang.org/x/exp/shiny v0.0.0-20240416160154-fe59bbe5cc7f // indirect 12 | golang.org/x/image v0.15.0 // indirect 13 | golang.org/x/sys v0.19.0 // indirect 14 | ) 15 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ= 2 | github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= 3 | golang.org/x/exp/shiny v0.0.0-20240416160154-fe59bbe5cc7f h1:W11kcexeK9nBV2PQVuWu7jFf0rIyI9jb+5AH1IxP7Xc= 4 | golang.org/x/exp/shiny v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:3F+MieQB7dRYLTmnncoFbb1crS5lfQoTfDgQy6K4N0o= 5 | golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8= 6 | golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= 7 | golang.org/x/mobile v0.0.0-20240404231514-09dbf07665ed h1:vZhAhVr5zF1IJaVKTawyTq78WSspLnK53iuMJ1fJgLc= 8 | golang.org/x/mobile v0.0.0-20240404231514-09dbf07665ed/go.mod h1:z041I2NhLjANgIfD0XbB2AmUZ8sLUcSgyLaSNGEP50M= 9 | golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= 10 | golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 11 | -------------------------------------------------------------------------------- /osversion.go: -------------------------------------------------------------------------------- 1 | package osversion 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/blang/semver" 7 | ) 8 | 9 | func GetSemanticVersion() (semver.Version, error) { 10 | str, err := GetString() 11 | if err != nil { 12 | return semver.Version{}, err 13 | } 14 | 15 | ver, err := semver.Make(str) 16 | if err != nil { 17 | return semver.Version{}, fmt.Errorf("invalid semantic version in %s: %w", str, err) 18 | } 19 | return ver, nil 20 | } 21 | -------------------------------------------------------------------------------- /osversion/.gitignore: -------------------------------------------------------------------------------- 1 | osversion.exe 2 | -------------------------------------------------------------------------------- /osversion/Makefile: -------------------------------------------------------------------------------- 1 | linux: 2 | CGO_ENABLED=1 GOOS=linux GOARCH=386 go build github.com/getlantern/osversion/osversion 3 | 4 | windows: 5 | CGO_ENABLED=1 GOOS=windows GOARCH=386 go build github.com/getlantern/osversion/osversion 6 | 7 | android: 8 | GOOS=android GOARCH=arm gomobile build -target=android github.com/getlantern/osversion/osversion && \ 9 | adb install -r osversion.apk 10 | -------------------------------------------------------------------------------- /osversion/main.go: -------------------------------------------------------------------------------- 1 | // +build !android 2 | 3 | package main 4 | 5 | import ( 6 | "log" 7 | 8 | "github.com/getlantern/osversion" 9 | ) 10 | 11 | func main() { 12 | str, err := osversion.GetString() 13 | if err != nil { 14 | log.Fatalf("Error getting OS version: %v", err) 15 | } 16 | log.Println(str) 17 | 18 | version, err := osversion.GetSemanticVersion() 19 | if err != nil { 20 | log.Fatalf("Error getting OS version: %v", err) 21 | } 22 | 23 | log.Println(version) 24 | 25 | hstr, err := osversion.GetHumanReadable() 26 | if err != nil { 27 | log.Fatalf("Error getting OS version: %v", err) 28 | } 29 | 30 | log.Println(hstr) 31 | } 32 | -------------------------------------------------------------------------------- /osversion/main_android.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/getlantern/osversion" 7 | 8 | "golang.org/x/mobile/app" 9 | ) 10 | 11 | func main() { 12 | // checkNetwork runs only once when the app first loads. 13 | app.Main(func(a app.App) { 14 | str, err := osversion.GetString() 15 | if err != nil { 16 | log.Printf("Error in osversion.GetString: %v", err) 17 | } 18 | log.Println(str) 19 | 20 | semVer, err := osversion.GetSemanticVersion() 21 | if err != nil { 22 | log.Printf("Error in osversion.GetSemanticVersion: %v", err) 23 | } 24 | log.Println(semVer.String()) 25 | 26 | str, err = osversion.GetHumanReadable() 27 | if err != nil { 28 | log.Printf("Error in osversion.GetHumanReadable: %v", err) 29 | } 30 | log.Println(str) 31 | }) 32 | } 33 | -------------------------------------------------------------------------------- /osversion_android.go: -------------------------------------------------------------------------------- 1 | package osversion 2 | 3 | /* 4 | #include 5 | #include 6 | #include 7 | 8 | int android_get_prop_value_max() { 9 | return PROP_VALUE_MAX; 10 | } 11 | 12 | int android_get_release(char *sdk_ver_str) { 13 | int result = __system_property_get("ro.build.version.release", sdk_ver_str); 14 | return result; 15 | } 16 | 17 | int android_get_api(char *sdk_ver_str) { 18 | int result = __system_property_get("ro.build.version.sdk", sdk_ver_str); 19 | return result; 20 | } 21 | */ 22 | import "C" 23 | 24 | import ( 25 | "errors" 26 | "fmt" 27 | "strconv" 28 | "unsafe" 29 | ) 30 | 31 | func GetString() (string, error) { 32 | strSize := int(C.android_get_prop_value_max()) 33 | verStr := (*C.char)(C.malloc(C.size_t(strSize))) 34 | defer C.free(unsafe.Pointer(verStr)) 35 | C.android_get_release(verStr) 36 | return C.GoString(verStr), nil 37 | } 38 | 39 | func GetHumanReadable() (string, error) { 40 | // First get the semantic versioning scheme 41 | verStr, err := GetString() 42 | if err != nil { 43 | return verStr, err 44 | } 45 | 46 | // Then get the release name 47 | strSize := int(C.android_get_prop_value_max()) 48 | apiStr := (*C.char)(C.malloc(C.size_t(strSize))) 49 | defer C.free(unsafe.Pointer(apiStr)) 50 | C.android_get_api(apiStr) 51 | 52 | i, err := strconv.ParseUint(C.GoString(apiStr), 10, 16) 53 | if err != nil { 54 | return verStr, err 55 | } 56 | 57 | if i > 22 { 58 | return verStr, errors.New("Unknown Android API version") 59 | } 60 | 61 | return fmt.Sprintf("%s %s", versions[i-1], verStr), nil 62 | } 63 | 64 | var versions = []string{ 65 | "", // 1 66 | "", // 2 67 | "Cupcake", // 3 68 | "Donut", // 4 69 | "Eclair", // 5 70 | "Eclair", // 6 71 | "Eclair", // 7 72 | "Froyo", // 8 73 | "Gingerbread", // 9 74 | "Gingerbread", // 10 75 | "Honeycomb", // 11 76 | "Honeycomb", // 12 77 | "Honeycomb", // 13 78 | "Ice Cream Sandwhich", // 14 79 | "Ice Cream Sandwhich", // 15 80 | "Jelly Bean", // 16 81 | "Jelly Bean", // 17 82 | "Jelly Bean", // 18 83 | "KitKat", // 19 84 | "KitKat", // 20 85 | "Lollipop", // 21 86 | "Lollipop", // 22 87 | } 88 | -------------------------------------------------------------------------------- /osversion_darwin.go: -------------------------------------------------------------------------------- 1 | package osversion 2 | 3 | /* 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int darwin_get_os(char* str, size_t size) { 11 | return sysctlbyname("kern.osrelease", str, &size, NULL, 0); 12 | } 13 | */ 14 | import "C" 15 | 16 | import ( 17 | "errors" 18 | "fmt" 19 | "strconv" 20 | "strings" 21 | "unsafe" 22 | ) 23 | 24 | func GetString() (string, error) { 25 | bufferSize := C.size_t(256) 26 | str := (*C.char)(C.malloc(bufferSize)) 27 | defer C.free(unsafe.Pointer(str)) 28 | 29 | err := C.darwin_get_os(str, bufferSize) 30 | if err == -1 { 31 | return "", errors.New(fmt.Sprintf("Error running sysctl: %v", err)) 32 | } 33 | return C.GoString(str), nil 34 | } 35 | 36 | func GetHumanReadable() (string, error) { 37 | version, err := GetSemanticVersion() 38 | if err != nil { 39 | return "", err 40 | } 41 | if version.Major < 4 || version.Major > 24 { 42 | return fmt.Sprintf("Unknown OS X version: %s", version.String()), nil 43 | } 44 | 45 | decimal := strconv.FormatUint(version.Patch, 10) 46 | if version.Major >= 18 { 47 | decimal = strconv.FormatUint(version.Minor-1, 10) 48 | } 49 | return strings.Replace(versions[version.Major-4], 50 | "{decimal}", 51 | decimal, 52 | 1), nil 53 | } 54 | 55 | var versions = []string{ 56 | "OS X 10.0.{decimal} Cheetah", 57 | "OS X 10.1.{decimal} Puma", 58 | "OS X 10.2.{decimal} Jaguar", 59 | "OS X 10.3.{decimal} Panther", 60 | "OS X 10.4.{decimal} Tiger", 61 | "OS X 10.5.{decimal} Leopard", 62 | "OS X 10.6.{decimal} Snow Leopard", 63 | "OS X 10.7.{decimal} Lion", 64 | "OS X 10.8.{decimal} Mountain Lion", 65 | "OS X 10.9.{decimal} Mavericks", 66 | "OS X 10.10.{decimal} Yosemite", 67 | "OS X 10.11.{decimal} El Capitan", 68 | "macOS 10.12.{decimal} Sierra", 69 | "macOS 10.13.{decimal} High Sierra", 70 | "macOS 10.14.{decimal} Mojave", 71 | "macOS 10.15.{decimal} Catalina", 72 | "macOS 11.{decimal} Big Sur", 73 | "macOS 12.{decimal} Monterey", 74 | "macOS 13.{decimal} Ventura", 75 | "macOS 14.{decimal} Sonoma", 76 | "macOS 15.{decimal} Sequoia", 77 | } 78 | -------------------------------------------------------------------------------- /osversion_linux.go: -------------------------------------------------------------------------------- 1 | // +build !android 2 | 3 | package osversion 4 | 5 | import ( 6 | "errors" 7 | "fmt" 8 | "io/ioutil" 9 | "regexp" 10 | "syscall" 11 | ) 12 | 13 | func GetString() (string, error) { 14 | var uts syscall.Utsname 15 | err := syscall.Uname(&uts) 16 | if err != nil { 17 | return "", errors.New(fmt.Sprintf("Error calling system function 'uname': %s", err)) 18 | } 19 | 20 | // Due to a mismatch in the uts.Release types depending on the architecture, we are 21 | // forced to implement it right here to bypass Go's type checking of slices 22 | utsRelease := uts.Release[:] 23 | s := make([]byte, len(utsRelease)) 24 | strpos := 0 25 | for strpos < len(utsRelease) { 26 | if utsRelease[strpos] == 0 { 27 | break 28 | } 29 | s[strpos] = uint8(utsRelease[strpos]) 30 | strpos++ 31 | } 32 | 33 | return fmt.Sprintf("%s", string(s[:strpos])), nil 34 | } 35 | 36 | func GetHumanReadable() (string, error) { 37 | // Kernel version 38 | kernel, err := GetString() 39 | if err != nil { 40 | return "", err 41 | } 42 | 43 | // Try to get the distribution info 44 | fData, err := ioutil.ReadFile("/etc/os-release") 45 | if err != nil { 46 | return fmt.Sprintf("kernel: %s", kernel), nil 47 | } 48 | 49 | // At least Fedora, Debian, Ubuntu and Arch support this approach 50 | // and provide the PRETTY_NAME field 51 | reg1 := regexp.MustCompile("PRETTY_NAME=\".+\"") 52 | reg2 := regexp.MustCompile("\".+\"") 53 | dstrBytes := reg2.Find(reg1.Find(fData)) 54 | distribution := string(dstrBytes[1 : len(dstrBytes)-1]) 55 | 56 | return fmt.Sprintf("%s kernel: %s", distribution, kernel), nil 57 | } 58 | -------------------------------------------------------------------------------- /osversion_test.go: -------------------------------------------------------------------------------- 1 | package osversion 2 | 3 | import ( 4 | "regexp" 5 | "runtime" 6 | "testing" 7 | ) 8 | 9 | func TestString(t *testing.T) { 10 | reg := regexp.MustCompile("^([0-9])+\\.([0-9])+\\.([0-9])+.*") 11 | str, err := GetString() 12 | if err != nil { 13 | t.Fatal("Error getting string") 14 | } 15 | if !reg.MatchString(str) { 16 | t.Fatalf("Improper string format: %s", str) 17 | } 18 | } 19 | 20 | func TestHumanReadable(t *testing.T) { 21 | switch runtime.GOOS { 22 | case "darwin": 23 | str, err := GetHumanReadable() 24 | if err != nil { 25 | t.Fatal("Error getting string") 26 | } 27 | reg := regexp.MustCompile("OS X 10\\..+") 28 | reg2 := regexp.MustCompile("macOS 10\\..+") 29 | if !reg.MatchString(str) && !reg2.MatchString(str) { 30 | t.Fatalf("Improper human readable format: %s", str) 31 | } 32 | case "linux": 33 | str, err := GetHumanReadable() 34 | if err != nil { 35 | t.Fatal("Error getting string") 36 | } 37 | reg := regexp.MustCompile(".*kernel.+") 38 | if !reg.MatchString(str) { 39 | t.Fatalf("Improper human readable format: %s", str) 40 | } 41 | case "windows": 42 | str, err := GetHumanReadable() 43 | if err != nil { 44 | t.Fatal("Error getting string") 45 | } 46 | reg := regexp.MustCompile("Windows .+") 47 | if !reg.MatchString(str) { 48 | t.Fatalf("Improper human readable format: %s", str) 49 | } 50 | default: 51 | t.Fatalf("Unsupported OS detected: %s", runtime.GOOS) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /osversion_windows.go: -------------------------------------------------------------------------------- 1 | package osversion 2 | 3 | import ( 4 | "fmt" 5 | "os/exec" 6 | "regexp" 7 | "syscall" 8 | ) 9 | 10 | var ( 11 | verRegex = regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+`) 12 | ) 13 | 14 | func GetString() (string, error) { 15 | cmd := exec.Command("cmd", "ver") 16 | cmd.SysProcAttr = &syscall.SysProcAttr{ 17 | HideWindow: true, 18 | } 19 | _text, err := cmd.Output() 20 | if err != nil { 21 | return "", fmt.Errorf("Unable to run ver: %v", err) 22 | } 23 | text := string(_text) 24 | if err != nil { 25 | return "", fmt.Errorf("Unable to run ver.exe: %v", err) 26 | } 27 | matches := verRegex.FindAllString(text, -1) 28 | if len(matches) != 1 { 29 | return "", fmt.Errorf("Version string not found: %v", text) 30 | } 31 | return matches[0], nil 32 | } 33 | 34 | func GetHumanReadable() (string, error) { 35 | version, err := GetSemanticVersion() 36 | if err != nil { 37 | return "", err 38 | } 39 | 40 | vstr := fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch) 41 | 42 | // Try to find the specific build first 43 | if str, ok := specificVersions[vstr]; ok { 44 | return str, nil 45 | } 46 | 47 | // Otherwise try with the generic list 48 | vstr = fmt.Sprintf("%d.%d", version.Major, version.Minor) 49 | if str, ok := versions[vstr]; ok { 50 | return str, nil 51 | } else { 52 | return "", fmt.Errorf("Unknown Windows version: %v", vstr) 53 | } 54 | } 55 | 56 | var specificVersions = map[string]string{ 57 | "5.1.2505": "Windows XP (RC 1)", 58 | "5.1.2600": "Windows XP", 59 | "5.2.3541": "Windows .NET Server interim", 60 | "5.2.3590": "Windows .NET Server Beta 3", 61 | "5.2.3660": "Windows .NET Server Release Candidate 1 (RC1)", 62 | "5.2.3718": "Windows .NET Server 2003 RC2", 63 | "5.2.3763": "Windows Server 2003", 64 | "5.2.3790": "Windows Server 2003 / Windows Home Server", 65 | "6.0.5048": "Windows Longhorn", 66 | "6.0.5112": "Windows Vista, Beta 1", 67 | "6.0.5219": "Windows Vista, CTP", 68 | "6.0.5259": "Windows Vista, TAP Preview", 69 | "6.0.5270": "Windows Vista, TAP Dec", 70 | "6.0.5308": "Windows Vista, TAP Feb", 71 | "6.0.5342": "Windows Vista, TAP Refresh", 72 | "6.0.5365": "Windows Vista, April EWD", 73 | "6.0.5381": "Windows Vista, Beta 2 Preview", 74 | "6.0.5384": "Windows Vista, Beta 2", 75 | "6.0.5456": "Windows Vista, Pre-RC1", 76 | "6.0.5472": "Windows Vista, Pre-RC1, Build 5472", 77 | "6.0.5536": "Windows Vista, Pre-RC1, Build 5536", 78 | "6.0.5600": "Windows Vista, RC1", 79 | "6.0.5700": "Windows Vista, Pre-RC2", 80 | "6.0.5728": "Windows Vista, Pre-RC2, Build 5728", 81 | "6.0.5744": "Windows Vista, RC2", 82 | "6.0.5808": "Windows Vista, Pre-RTM, Build 5808", 83 | "6.0.5824": "Windows Vista, Pre-RTM, Build 5824", 84 | "6.0.5840": "Windows Vista, Pre-RTM, Build 5840", 85 | "6.0.6000": "Windows Vista", 86 | "6.0.6002": "Windows Vista, Service Pack 2", 87 | "6.0.6001": "Windows Server 2008", 88 | "6.1.7600": "Windows 7, RTM / Windows Server 2008 R2, RTM", 89 | "6.1.7601": "Windows 7 / Windows Server 2008 R2, SP1", 90 | "6.1.8400": "Windows Home Server 2011", 91 | "6.2.9200": "Windows 8 / Windows Server 2012", 92 | "6.2.10211": "Windows Phone 8", 93 | "6.3.9200": "Windows 8.1 / Windows Server 2012 R2", 94 | "6.3.9600": "Windows 8.1, Update 1", 95 | "10.0.10240": "Windows 10 RTM", 96 | "10.0.10586": "Windows 10 1511 / Windows Server 2016 Technical Preview 4", 97 | "10.0.14393": "Windows 10 1607 / Windows Server 2016", 98 | "10.0.15063": "Windows 10 1703", 99 | "6.4.9841": "Windows Server 2016 Technical Preview", 100 | "10.0.10074": "Windows Server 2016 Technical Preview 2", 101 | "10.0.10514": "Windows Server 2016 Technical Preview 3", 102 | "10.0.14300": "Windows Server 2016 Technical Preview 5", 103 | } 104 | 105 | var versions = map[string]string{ 106 | "5.0": "Windows 2000 Professional / Windows 2000 Server (unknown build)", 107 | "5.1": "Windows XP (unknown build)", 108 | "5.2": "Windows XP Professional x64 / Windows Server 2003 (unknown build)", 109 | "6.0": "Windows Vista / Windows Server 2008 (unknown build)", 110 | "6.1": "Windows 7 / Windows Server 2008 R2 (unknown build)", 111 | "6.2": "Windows 8 / Windows Server 2012 (unknown build)", 112 | "6.3": "Windows 8.1 / Windows Server 2012 R2 (unknown build)", 113 | "10.0": "Windows 10 / Windows Server 2016 (unknown build)", 114 | } 115 | --------------------------------------------------------------------------------