Skip to content

Instantly share code, notes, and snippets.

@jessedearing
Last active October 3, 2019 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessedearing/6e30c16362083c46856db7e94d729d33 to your computer and use it in GitHub Desktop.
Save jessedearing/6e30c16362083c46856db7e94d729d33 to your computer and use it in GitHub Desktop.
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)
}
module g
go 1.13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment