diff options
author | Teddy Wing | 2014-12-06 18:42:03 -0500 |
---|---|---|
committer | Teddy Wing | 2014-12-06 18:44:51 -0500 |
commit | cd4a62fbbd0d8f89c6ab7e9e0d92d86f3442cfbd (patch) | |
tree | 196072f9671528a4b5ff697db6e440b324e648d3 | |
parent | fe9abe49b3b9e9c325d8cb73728fdde119e8c79b (diff) | |
download | git-checkout-history-cd4a62fbbd0d8f89c6ab7e9e0d92d86f3442cfbd.tar.bz2 |
Move branch storage to utils package
Create utils package and move everything related to branch creation into
the utils.go file. Then call `utils.Store` to store a branch.
Doing this so I can easily store a branch in history from a
`git-checkout-history` call. This is because we want to save to history
even if you're checking out using the history shorthand.
-rw-r--r-- | git-checkout-store/main.go | 61 | ||||
-rw-r--r-- | utils/utils.go | 63 |
2 files changed, 65 insertions, 59 deletions
diff --git a/git-checkout-store/main.go b/git-checkout-store/main.go index c2588e4..335bd02 100644 --- a/git-checkout-store/main.go +++ b/git-checkout-store/main.go @@ -3,73 +3,16 @@ package main import ( "bytes" "fmt" - "io/ioutil" - "log" + "github.com/teddywing/git-checkout-history/utils" "os" "os/exec" - "os/user" - - "gopkg.in/yaml.v2" ) -type BranchList struct { - Branches []string -} - -func getHomeDir() string { - usr, err := user.Current() - if err != nil { - log.Fatal(err) - } - return usr.HomeDir -} - -func OpenRCFile() (f *os.File, err error) { - file_path := getHomeDir() + "/.git-checkout-history" - 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-history" - 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([]string{branch}, branchList.Branches...) - - 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:] if len(args) > 0 { - store(args[0]) + utils.Store(args[0]) cmd := exec.Command("git", "checkout", args[0]) var out bytes.Buffer diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..ebb67de --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,63 @@ +package utils + +import ( + "io/ioutil" + "log" + "os" + "os/user" + + "gopkg.in/yaml.v2" +) + +type BranchList struct { + Branches []string +} + +func getHomeDir() string { + usr, err := user.Current() + if err != nil { + log.Fatal(err) + } + return usr.HomeDir +} + +func OpenRCFile() (f *os.File, err error) { + file_path := getHomeDir() + "/.git-checkout-history" + 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-history" + 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([]string{branch}, branchList.Branches...) + + data, err = yaml.Marshal(&branchList) + if err != nil { + log.Fatal(err) + } + + err = ioutil.WriteFile(file_path, data, 0644) + if err != nil { + log.Fatal(err) + } +} |