Simple go program to record timings for git pulls, pushes, and fetches
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
"strconv" | |
"strings" | |
"time" | |
) | |
func main() { | |
var a = make([]string, 0, 5) | |
a = append(a, os.Args[1:]...) | |
c := exec.Command(a[0], a[1:]...) | |
c.Stdout = os.Stdout | |
c.Stderr = os.Stderr | |
c.Stdin = os.Stdin | |
before := time.Now() | |
c.Run() | |
after := time.Now() | |
cwd, _ := os.Getwd() | |
timing := after.Sub(before) | |
if len(a) > 1 { | |
switch a[1] { | |
case "push", "pull", "fetch", "issue", "mr": | |
writeTiming(a[0], a[1], strings.Join(a[2:], " "), strconv.FormatInt(timing.Milliseconds(), 10), cwd) | |
} | |
} | |
fmt.Printf("\nreal: %s\n", timing) | |
os.Exit(c.ProcessState.ExitCode()) | |
} | |
func writeTiming(command, subcommand, args, timing, workdir string) { | |
home, _ := os.UserHomeDir() | |
f, err := os.OpenFile(fmt.Sprintf("%s/.g.csv", home), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Unable to open file: %+v", err) | |
return | |
} | |
defer f.Close() | |
fmt.Fprintf(f, "\"%d\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n", time.Now().Unix(), command, strings.Replace(subcommand, "\"", "", -1), strings.Replace(args, "\"", "", -1), timing, workdir) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment