-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from wakatime/master
Prevent panic from index out of range
- Loading branch information
Showing
7 changed files
with
123 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package goInfo | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func _getInfo(flags string) (string, string, string, string, error) { | ||
out, err := _uname(flags) | ||
tries := 0 | ||
for strings.Contains(out, "broken pipe") && tries < 3 { | ||
out, err = _uname(flags) | ||
time.Sleep(500 * time.Millisecond) | ||
tries++ | ||
} | ||
if strings.Contains(out, "broken pipe") { | ||
out = "" | ||
} | ||
kernel, core, platform, osname := _expandInfo(out) | ||
return kernel, core, platform, osname, err | ||
} | ||
|
||
func _uname(flags string) (string, error) { | ||
cmd := exec.Command("uname", flags) | ||
cmd.Stdin = strings.NewReader("some input") | ||
var out bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd.Stdout = &out | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
return out.String(), err | ||
} | ||
|
||
func _expandInfo(osStr string) (string, string, string, string) { | ||
osStr = strings.Replace(osStr, "\n", "", -1) | ||
osStr = strings.Replace(osStr, "\r\n", "", -1) | ||
osInfo := strings.Split(osStr, " ") | ||
kernel := "Unknown" | ||
core := "Unknown" | ||
platform := "Unknown" | ||
osname := "Unknown" | ||
if len(osInfo) > 0 { | ||
kernel = osInfo[0] | ||
} | ||
if len(osInfo) > 1 { | ||
core = osInfo[1] | ||
} | ||
if len(osInfo) > 2 { | ||
platform = osInfo[2] | ||
} | ||
if len(osInfo) > 3 { | ||
osname = osInfo[3] | ||
} | ||
return kernel, core, platform, osname | ||
} | ||
|
||
func _hostname() string { | ||
host, _ := os.Hostname() | ||
return host | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,19 @@ | ||
package goInfo | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func GetInfo() (GoInfoObject, error) { | ||
out, err := _getInfo() | ||
for strings.Index(out, "broken pipe") != -1 { | ||
out, err = _getInfo() | ||
time.Sleep(500 * time.Millisecond) | ||
kernel, core, platform, _, err := _getInfo("-srm") | ||
gio := GoInfoObject{ | ||
Kernel: kernel, | ||
Core: core, | ||
Platform: platform, | ||
OS: kernel, | ||
GoOS: runtime.GOOS, | ||
CPUs: runtime.NumCPU(), | ||
Hostname: _hostname(), | ||
} | ||
osStr := strings.Replace(out, "\n", "", -1) | ||
osStr = strings.Replace(osStr, "\r\n", "", -1) | ||
osInfo := strings.Split(osStr, " ") | ||
gio := GoInfoObject{Kernel: osInfo[0], Core: osInfo[1], Platform: osInfo[2], OS: osInfo[0], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} | ||
gio.Hostname, _ = os.Hostname() | ||
return gio, err | ||
} | ||
|
||
func _getInfo() (string, error) { | ||
cmd := exec.Command("uname", "-srm") | ||
cmd.Stdin = strings.NewReader("some input") | ||
var out bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd.Stdout = &out | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
return out.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,19 @@ | ||
package goInfo | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func GetInfo() (GoInfoObject, error) { | ||
out, err := _getInfo() | ||
for strings.Index(out, "broken pipe") != -1 { | ||
out, err = _getInfo() | ||
time.Sleep(500 * time.Millisecond) | ||
kernel, core, platform, _, err := _getInfo("-sri") | ||
gio := GoInfoObject{ | ||
Kernel: kernel, | ||
Core: core, | ||
Platform: runtime.GOARCH, | ||
OS: platform, | ||
GoOS: runtime.GOOS, | ||
CPUs: runtime.NumCPU(), | ||
Hostname: _hostname(), | ||
} | ||
osStr := strings.Replace(out, "\n", "", -1) | ||
osStr = strings.Replace(osStr, "\r\n", "", -1) | ||
osInfo := strings.Split(osStr, " ") | ||
gio := GoInfoObject{Kernel: osInfo[0], Core: osInfo[1], Platform: runtime.GOARCH, OS: osInfo[2], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} | ||
gio.Hostname, _ = os.Hostname() | ||
return gio, err | ||
} | ||
|
||
func _getInfo() (string, error) { | ||
cmd := exec.Command("uname", "-sri") | ||
cmd.Stdin = strings.NewReader("some input") | ||
var out bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd.Stdout = &out | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
return out.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,19 @@ | ||
package goInfo | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func GetInfo() (GoInfoObject, error) { | ||
out, err := _getInfo() | ||
for strings.Index(out, "broken pipe") != -1 { | ||
out, err = _getInfo() | ||
time.Sleep(500 * time.Millisecond) | ||
kernel, core, platform, osname, err := _getInfo("-srio") | ||
gio := GoInfoObject{ | ||
Kernel: kernel, | ||
Core: core, | ||
Platform: platform, | ||
OS: osname, | ||
GoOS: runtime.GOOS, | ||
CPUs: runtime.NumCPU(), | ||
Hostname: _hostname(), | ||
} | ||
osStr := strings.Replace(out, "\n", "", -1) | ||
osStr = strings.Replace(osStr, "\r\n", "", -1) | ||
osInfo := strings.Split(osStr, " ") | ||
gio := GoInfoObject{Kernel: osInfo[0], Core: osInfo[1], Platform: osInfo[2], OS: osInfo[3], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} | ||
gio.Hostname, _ = os.Hostname() | ||
return gio, err | ||
} | ||
|
||
func _getInfo() (string, error) { | ||
cmd := exec.Command("uname", "-srio") | ||
cmd.Stdin = strings.NewReader("some input") | ||
var out bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd.Stdout = &out | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
return out.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,19 @@ | ||
package goInfo | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func GetInfo() (GoInfoObject, error) { | ||
out, err := _getInfo() | ||
for strings.Index(out, "broken pipe") != -1 { | ||
out, err = _getInfo() | ||
time.Sleep(500 * time.Millisecond) | ||
kernel, core, platform, _, err := _getInfo("-srm") | ||
gio := GoInfoObject{ | ||
Kernel: kernel, | ||
Core: core, | ||
Platform: runtime.GOARCH, | ||
OS: platform, | ||
GoOS: runtime.GOOS, | ||
CPUs: runtime.NumCPU(), | ||
Hostname: _hostname(), | ||
} | ||
osStr := strings.Replace(out, "\n", "", -1) | ||
osStr = strings.Replace(osStr, "\r\n", "", -1) | ||
osInfo := strings.Split(osStr, " ") | ||
gio := GoInfoObject{Kernel: osInfo[0], Core: osInfo[1], Platform: runtime.GOARCH, OS: osInfo[2], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} | ||
gio.Hostname, _ = os.Hostname() | ||
return gio, err | ||
} | ||
|
||
func _getInfo() (string, error) { | ||
cmd := exec.Command("uname", "-srm") | ||
cmd.Stdin = strings.NewReader("some input") | ||
var out bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd.Stdout = &out | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
return out.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,19 @@ | ||
package goInfo | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func GetInfo() (GoInfoObject, error) { | ||
out, err := _getInfo() | ||
for strings.Index(out, "broken pipe") != -1 { | ||
out, err = _getInfo() | ||
time.Sleep(500 * time.Millisecond) | ||
kernel, core, platform, _, err := _getInfo("-srm") | ||
gio := GoInfoObject{ | ||
Kernel: kernel, | ||
Core: core, | ||
Platform: runtime.GOARCH, | ||
OS: platform, | ||
GoOS: runtime.GOOS, | ||
CPUs: runtime.NumCPU(), | ||
Hostname: _hostname(), | ||
} | ||
osStr := strings.Replace(out, "\n", "", -1) | ||
osStr = strings.Replace(osStr, "\r\n", "", -1) | ||
osInfo := strings.Split(osStr, " ") | ||
gio := GoInfoObject{Kernel: osInfo[0], Core: osInfo[1], Platform: runtime.GOARCH, OS: osInfo[2], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} | ||
gio.Hostname, _ = os.Hostname() | ||
return gio, err | ||
} | ||
|
||
func _getInfo() (string, error) { | ||
cmd := exec.Command("uname", "-srm") | ||
cmd.Stdin = strings.NewReader("some input") | ||
var out bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd.Stdout = &out | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
return out.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters