go-learn/check-versions.go

65 lines
1.2 KiB
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 22:43:59 +00:00
gcnf := execute("git config -l")
config := strings.Split(gcnf, "\n")
for i, v := range config {
entry := strings.Split(v, "=")
if len(entry) < 2 {
continue
}
key := entry[0]
val := entry[1]
fmt.Printf("%d: %s = '%s' \n", i, key, val)
}
glog := execute("git log --oneline")
logarray := strings.Split(glog, "\n")
for _, v := range logarray {
entryarray := strings.SplitN(v, " ", 2)
if len(entryarray) < 2 {
continue
}
commitid := entryarray[0]
reason := entryarray[1]
fmt.Println(commitid, reason)
}
2021-03-08 17:50:01 +00:00
}