go-learn/check-versions.go

40 lines
703 B
Go
Raw Normal View History

2021-03-08 17:50:01 +00:00
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)
func validateos() {
if runtime.GOOS == "windows" {
fmt.Println("Can't Execute this on a windows machine")
os.Exit(1)
}
}
2021-03-08 21:27:15 +00:00
func execute(cmdstr string) string {
2021-03-08 17:50:01 +00:00
validateos()
var cmdargs = strings.Split(cmdstr, " ") // string arrayified
var cmd = cmdargs[0] // command
cmdargs = append(cmdargs[:0], cmdargs[1:]...) // argument array sans cmd
out, err := exec.Command(cmd, cmdargs...).CombinedOutput()
2021-03-08 21:27:15 +00:00
var output string
2021-03-08 17:50:01 +00:00
if err != nil {
2021-03-08 21:27:15 +00:00
output = err.Error()
} else {
output = string(out[:])
2021-03-08 17:50:01 +00:00
}
2021-03-08 21:27:15 +00:00
return output
2021-03-08 17:50:01 +00:00
}
func main() {
2021-03-08 21:27:15 +00:00
gitver := execute("git --version")
fmt.Println(gitver)
2021-03-08 17:50:01 +00:00
}