diff options
| author | Teddy Wing | 2014-12-06 17:43:46 -0500 | 
|---|---|---|
| committer | Teddy Wing | 2014-12-06 17:45:51 -0500 | 
| commit | 2b091e1072c62f0a6ac35349ac2a899e038b099c (patch) | |
| tree | d65bb129f7b0e40a39d0d1572c2934dffc8f074d | |
| parent | a0d213e24d90bfe95342efd57cf560ae2c1996e1 (diff) | |
| download | git-checkout-history-2b091e1072c62f0a6ac35349ac2a899e038b099c.tar.bz2 | |
git-checkout-store: Store branch in YAML file
Take the branch parameter and store it in a `branches` array in our YAML
rcfile.
| -rw-r--r-- | git-checkout-store/main.go | 56 | 
1 files changed, 47 insertions, 9 deletions
| diff --git a/git-checkout-store/main.go b/git-checkout-store/main.go index fcfd1c2..45ea0da 100644 --- a/git-checkout-store/main.go +++ b/git-checkout-store/main.go @@ -3,12 +3,19 @@ package main  import (  	"bytes"  	"fmt" +	"io/ioutil"  	"log"  	"os"  	"os/exec"  	"os/user" + +	"gopkg.in/yaml.v2"  ) +type BranchList struct { +	Branches []string +} +  func getHomeDir() string {  	usr, err := user.Current()  	if err != nil { @@ -17,22 +24,53 @@ func getHomeDir() string {  	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 OpenRCFile() (f *os.File, err error) { +	file_path := getHomeDir() + "/.git-checkout-historyrc" +	if _, err := os.Stat(file_path); os.IsNotExist(err) { +		return os.Create(file_path) +	} else { +		return os.Open(file_path) +	} +} + +func store(branch string) { +	branchList := BranchList{} +	rcfile, err := OpenRCFile() +	if err != nil { +		log.Fatal(err) +	} +	rcfile.Close() +	 +	file_path := getHomeDir() + "/.git-checkout-historyrc" +	data, err := ioutil.ReadFile(file_path) +	if err != nil { +		log.Fatal(err) +	} +	 +	err = yaml.Unmarshal(data, &branchList) +	if err != nil { +		log.Fatal(err) +	} +	 +	branchList.Branches = append(branchList.Branches, branch) +	 +	data, err = yaml.Marshal(&branchList) +	if err != nil { +		log.Fatal(err) +	} +	 +	err = ioutil.WriteFile(file_path, data, 0644) +	if err != nil { +		log.Fatal(err)  	}  }  func main() {  	args := os.Args[1:] - -	OpenRCFile()  	if len(args) > 0 { +		store(args[0]) +		  		cmd := exec.Command("git", "checkout", args[0])  		var out bytes.Buffer  		cmd.Stdout = &out | 
