From a0d213e24d90bfe95342efd57cf560ae2c1996e1 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Dec 2014 13:45:27 -0500 Subject: git-checkout-store: Create an rc file if it doesn't exist Make a `.git-checkout-historyrc` file in the current user's home directory if the file doesn't exist. Resources: - [Path to home directory](http://stackoverflow.com/questions/7922270/obtain-users-home-directory) - [Cross-compilation version](https://github.com/mitchellh/go-homedir) --- git-checkout-store/main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'git-checkout-store') diff --git a/git-checkout-store/main.go b/git-checkout-store/main.go index 56b44ec..fcfd1c2 100644 --- a/git-checkout-store/main.go +++ b/git-checkout-store/main.go @@ -3,13 +3,35 @@ package main import ( "bytes" "fmt" + "log" "os" "os/exec" + "os/user" ) +func getHomeDir() string { + usr, err := user.Current() + if err != nil { + log.Fatal(err) + } + return usr.HomeDir +} + +func OpenRCFile() { + filename := ".git-checkout-historyrc" + if _, err := os.Stat(filename); os.IsNotExist(err) { + _, err := os.Create(getHomeDir() + "/" + filename) + if err != nil { + log.Fatal(err) + } + } +} + func main() { args := os.Args[1:] + OpenRCFile() + if len(args) > 0 { cmd := exec.Command("git", "checkout", args[0]) var out bytes.Buffer -- cgit v1.2.3