├── go.mod ├── .gitignore ├── goInfo_darwin.go ├── goInfo_linux.go ├── goInfo_freebsd.go ├── goInfo_netbsd.go ├── goInfo_openbsd.go ├── goInfo.go ├── LICENSE ├── README.md ├── goInfo_windows.go └── common.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/matishsiao/goInfo 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | -------------------------------------------------------------------------------- /goInfo_darwin.go: -------------------------------------------------------------------------------- 1 | package goInfo 2 | 3 | import ( 4 | "runtime" 5 | ) 6 | 7 | func GetInfo() (GoInfoObject, error) { 8 | kernel, core, platform, _, err := _getInfo("-srm") 9 | gio := GoInfoObject{ 10 | Kernel: kernel, 11 | Core: core, 12 | Platform: platform, 13 | OS: kernel, 14 | GoOS: runtime.GOOS, 15 | CPUs: runtime.NumCPU(), 16 | Hostname: _hostname(), 17 | } 18 | return gio, err 19 | } 20 | -------------------------------------------------------------------------------- /goInfo_linux.go: -------------------------------------------------------------------------------- 1 | package goInfo 2 | 3 | import ( 4 | "runtime" 5 | ) 6 | 7 | func GetInfo() (GoInfoObject, error) { 8 | kernel, core, platform, osname, err := _getInfo("-srio") 9 | gio := GoInfoObject{ 10 | Kernel: kernel, 11 | Core: core, 12 | Platform: platform, 13 | OS: osname, 14 | GoOS: runtime.GOOS, 15 | CPUs: runtime.NumCPU(), 16 | Hostname: _hostname(), 17 | } 18 | return gio, err 19 | } 20 | -------------------------------------------------------------------------------- /goInfo_freebsd.go: -------------------------------------------------------------------------------- 1 | package goInfo 2 | 3 | import ( 4 | "runtime" 5 | ) 6 | 7 | func GetInfo() (GoInfoObject, error) { 8 | kernel, core, platform, _, err := _getInfo("-sri") 9 | gio := GoInfoObject{ 10 | Kernel: kernel, 11 | Core: core, 12 | Platform: runtime.GOARCH, 13 | OS: platform, 14 | GoOS: runtime.GOOS, 15 | CPUs: runtime.NumCPU(), 16 | Hostname: _hostname(), 17 | } 18 | return gio, err 19 | } 20 | -------------------------------------------------------------------------------- /goInfo_netbsd.go: -------------------------------------------------------------------------------- 1 | package goInfo 2 | 3 | import ( 4 | "runtime" 5 | ) 6 | 7 | func GetInfo() (GoInfoObject, error) { 8 | kernel, core, platform, _, err := _getInfo("-srm") 9 | gio := GoInfoObject{ 10 | Kernel: kernel, 11 | Core: core, 12 | Platform: runtime.GOARCH, 13 | OS: platform, 14 | GoOS: runtime.GOOS, 15 | CPUs: runtime.NumCPU(), 16 | Hostname: _hostname(), 17 | } 18 | return gio, err 19 | } 20 | -------------------------------------------------------------------------------- /goInfo_openbsd.go: -------------------------------------------------------------------------------- 1 | package goInfo 2 | 3 | import ( 4 | "runtime" 5 | ) 6 | 7 | func GetInfo() (GoInfoObject, error) { 8 | kernel, core, platform, _, err := _getInfo("-srm") 9 | gio := GoInfoObject{ 10 | Kernel: kernel, 11 | Core: core, 12 | Platform: runtime.GOARCH, 13 | OS: platform, 14 | GoOS: runtime.GOOS, 15 | CPUs: runtime.NumCPU(), 16 | Hostname: _hostname(), 17 | } 18 | return gio, err 19 | } 20 | -------------------------------------------------------------------------------- /goInfo.go: -------------------------------------------------------------------------------- 1 | package goInfo 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type GoInfoObject struct { 8 | GoOS string 9 | Kernel string 10 | Core string 11 | Platform string 12 | OS string 13 | Hostname string 14 | CPUs int 15 | } 16 | 17 | func (gi *GoInfoObject) VarDump() { 18 | fmt.Println("GoOS:", gi.GoOS) 19 | fmt.Println("Kernel:", gi.Kernel) 20 | fmt.Println("Core:", gi.Core) 21 | fmt.Println("Platform:", gi.Platform) 22 | fmt.Println("OS:", gi.OS) 23 | fmt.Println("Hostname:", gi.Hostname) 24 | fmt.Println("CPUs:", gi.CPUs) 25 | } 26 | 27 | func (gi *GoInfoObject) String() string { 28 | return fmt.Sprintf("GoOS:%v,Kernel:%v,Core:%v,Platform:%v,OS:%v,Hostname:%v,CPUs:%v", gi.GoOS, gi.Kernel, gi.Core, gi.Platform, gi.OS, gi.Hostname, gi.CPUs) 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Matis Hsiao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction: 2 | GoInfo is get os platform information coding by Golang. 3 | 4 | It can help you to know os information. 5 | 6 | 7 | ## Version: 8 | 9 | version:0.0.1 10 | 11 | ## Futures 12 | 13 | get linux information 14 | 15 | get windows information 16 | 17 | get osx information 18 | 19 | get freebsd information 20 | 21 | 22 | ## Install: 23 | ```sh 24 | go get github.com/matishsiao/goInfo 25 | go build 26 | ``` 27 | 28 | ## Struct: 29 | ```go 30 | type GoInfoObject struct { 31 | GoOS string 32 | Kernel string 33 | Core string 34 | Platform string 35 | OS string 36 | Hostname string 37 | CPUs int 38 | } 39 | ``` 40 | 41 | ## Example: 42 | 43 | ```go 44 | package main 45 | 46 | import ( 47 | "github.com/matishsiao/goInfo" 48 | ) 49 | 50 | func main() { 51 | gi, _ := goInfo.GetInfo() 52 | gi.VarDump() 53 | } 54 | } 55 | ``` 56 | 57 | It's will show: 58 | 59 | ``` 60 | GoOS: linux 61 | Kernel: Linux 62 | Core: 3.13.0-27-generic 63 | Platform: x86_64 64 | OS: GNU/Linux 65 | Hostname: ubuntu 66 | CPUs: 1 67 | ``` 68 | 69 | ##License and Copyright 70 | This software is Copyright 2012-2014 Matis Hsiao. 71 | -------------------------------------------------------------------------------- /goInfo_windows.go: -------------------------------------------------------------------------------- 1 | package goInfo 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "os" 7 | "os/exec" 8 | "runtime" 9 | "strings" 10 | ) 11 | 12 | func GetInfo() (GoInfoObject, error) { 13 | cmd := exec.Command("cmd", "ver") 14 | cmd.Stdin = strings.NewReader("some input") 15 | var out bytes.Buffer 16 | var stderr bytes.Buffer 17 | cmd.Stdout = &out 18 | cmd.Stderr = &stderr 19 | err := cmd.Run() 20 | if err != nil { 21 | gio := GoInfoObject{Kernel: "windows", Core: "unknown", Platform: platform(), OS: "windows", GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} 22 | gio.Hostname, _ = os.Hostname() 23 | return gio, fmt.Errorf("getInfo: %s", err) 24 | } 25 | ver := "unknown" 26 | outStr := out.String() 27 | if strings.Contains(outStr, "[") { 28 | parts := strings.Split(strings.ReplaceAll(outStr, "\r\n\t", ""), "[") 29 | if len(parts) >= 2 { 30 | x := parts[1] 31 | x = strings.Split(x, "]")[0] 32 | parts = strings.Split(x, " ") 33 | ver = parts[len(parts)-1] 34 | } 35 | } 36 | gio := GoInfoObject{Kernel: "windows", Core: ver, Platform: platform(), OS: "windows", GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} 37 | gio.Hostname, _ = os.Hostname() 38 | return gio, nil 39 | } 40 | 41 | func platform() string { 42 | p := runtime.GOARCH 43 | switch p { 44 | case "386": 45 | return "i386" 46 | case "amd64": 47 | return "x86_64" 48 | default: 49 | return p 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /common.go: -------------------------------------------------------------------------------- 1 | package goInfo 2 | 3 | import ( 4 | "bytes" 5 | "os" 6 | "os/exec" 7 | "strings" 8 | "time" 9 | ) 10 | 11 | func _getInfo(flags string) (string, string, string, string, error) { 12 | out, err := _uname(flags) 13 | tries := 0 14 | for strings.Contains(out, "broken pipe") && tries < 3 { 15 | out, err = _uname(flags) 16 | time.Sleep(500 * time.Millisecond) 17 | tries++ 18 | } 19 | if strings.Contains(out, "broken pipe") { 20 | out = "" 21 | } 22 | kernel, core, platform, osname := _expandInfo(out) 23 | return kernel, core, platform, osname, err 24 | } 25 | 26 | func _uname(flags string) (string, error) { 27 | cmd := exec.Command("uname", flags) 28 | cmd.Stdin = strings.NewReader("some input") 29 | var out bytes.Buffer 30 | var stderr bytes.Buffer 31 | cmd.Stdout = &out 32 | cmd.Stderr = &stderr 33 | err := cmd.Run() 34 | return out.String(), err 35 | } 36 | 37 | func _expandInfo(osStr string) (string, string, string, string) { 38 | osStr = strings.Replace(osStr, "\n", "", -1) 39 | osStr = strings.Replace(osStr, "\r\n", "", -1) 40 | osInfo := strings.Split(osStr, " ") 41 | kernel := "Unknown" 42 | core := "Unknown" 43 | platform := "Unknown" 44 | osname := "Unknown" 45 | if len(osInfo) > 0 { 46 | kernel = osInfo[0] 47 | } 48 | if len(osInfo) > 1 { 49 | core = osInfo[1] 50 | } 51 | if len(osInfo) > 2 { 52 | platform = osInfo[2] 53 | } 54 | if len(osInfo) > 3 { 55 | osname = osInfo[3] 56 | } 57 | return kernel, core, platform, osname 58 | } 59 | 60 | func _hostname() string { 61 | host, _ := os.Hostname() 62 | return host 63 | } 64 | --------------------------------------------------------------------------------