diff options
| author | Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com> | 2019-01-01 15:04:04 +0300 |
|---|---|---|
| committer | Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com> | 2019-01-01 15:04:04 +0300 |
| commit | 457d39a46dc891d18b0b199c2295f38e5538ca8a (patch) | |
| tree | ccbcd49fa127bc1ffa4b69b2f353bd4d01bf2631 | |
| parent | add page up page down controls on the repo view (diff) | |
| download | gitbatch-457d39a46dc891d18b0b199c2295f38e5538ca8a.tar.gz | |
remove utility package
| -rw-r--r-- | main.go | 22 | ||||
| -rw-r--r-- | pkg/git/branch.go | 5 | ||||
| -rw-r--r-- | pkg/git/cmd.go | 80 | ||||
| -rw-r--r-- | pkg/git/repository.go | 5 | ||||
| -rw-r--r-- | pkg/git/util-random.go (renamed from pkg/helpers/utils.go) | 20 | ||||
| -rw-r--r-- | pkg/git/util-random_test.go (renamed from pkg/helpers/utils_test.go) | 2 | ||||
| -rw-r--r-- | pkg/gui/mainview.go | 3 | ||||
| -rw-r--r-- | pkg/gui/util-common.go | 11 | ||||
| -rw-r--r-- | pkg/helpers/command.go | 51 |
9 files changed, 101 insertions, 98 deletions
@@ -1,6 +1,8 @@ package main import ( + "os" + "github.com/isacikgoz/gitbatch/pkg/app" log "github.com/sirupsen/logrus" kingpin "gopkg.in/alecthomas/kingpin.v2" @@ -19,6 +21,15 @@ func main() { // parse the command line flag and options kingpin.Parse() + if err := run(); err != nil { + log.WithFields(log.Fields{ + "error": err.Error(), + }).Error("Application quitted with an unhandled error.") + os.Exit(1) + } +} + +func run() error { // set the app app, err := app.Setup(&app.SetupConfig{ Directories: *dirs, @@ -28,15 +39,12 @@ func main() { Mode: *mode, }) if err != nil { - log.Fatal(err) - } - - // execute the app and wait its routine - err = app.Gui.Run() - if err != nil { - log.Fatal(err) + return err } // good citizens always clean up their mess defer app.Close() + + // execute the app and wait its routine + return app.Gui.Run() } diff --git a/pkg/git/branch.go b/pkg/git/branch.go index d3b2425..94ad578 100644 --- a/pkg/git/branch.go +++ b/pkg/git/branch.go @@ -4,9 +4,8 @@ import ( "strconv" "strings" - "github.com/isacikgoz/gitbatch/pkg/helpers" log "github.com/sirupsen/logrus" - "gopkg.in/src-d/go-git.v4" + git "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" ) @@ -142,7 +141,7 @@ func (e *RepoEntity) Checkout(branch *Branch) error { // an issue about it: https://github.com/src-d/go-git/issues/844 func (e *RepoEntity) isClean() bool { s := e.StatusWithGit() - s = helpers.TrimTrailingNewline(s) + s = TrimTrailingNewline(s) if s != "?" { vs := strings.Split(s, "\n") line := vs[len(vs)-1] diff --git a/pkg/git/cmd.go b/pkg/git/cmd.go index a84f489..05d030c 100644 --- a/pkg/git/cmd.go +++ b/pkg/git/cmd.go @@ -1,12 +1,68 @@ package git import ( - "github.com/isacikgoz/gitbatch/pkg/helpers" + "log" + "os/exec" + "strings" + "syscall" ) +// RunCommandWithOutput runs the OS command and return its output. If the output +// returns error it also encapsulates it as a golang.error which is a return code +// of the command except zero +func RunCommandWithOutput(dir string, command string, args []string) (string, error) { + cmd := exec.Command(command, args...) + if dir != "" { + cmd.Dir = dir + } + output, err := cmd.Output() + return string(output), err +} + +// GetCommandStatus returns if we supposed to get return value as an int of a command +// this method can be used. It is practical when you use a command and process a +// failover acoording to a soecific return code +func GetCommandStatus(dir string, command string, args []string) (int, error) { + cmd := exec.Command(command, args...) + if dir != "" { + cmd.Dir = dir + } + var err error + // this time the execution is a little different + if err := cmd.Start(); err != nil { + return -1, err + } + if err := cmd.Wait(); err != nil { + if exiterr, ok := err.(*exec.ExitError); ok { + // The program has exited with an exit code != 0 + + // This works on both Unix and Windows. Although package + // syscall is generally platform dependent, WaitStatus is + // defined for both Unix and Windows and in both cases has + // an ExitStatus() method with the same signature. + if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { + statusCode := status.ExitStatus() + return statusCode, err + } + } else { + log.Fatalf("cmd.Wait: %v", err) + } + } + return -1, err +} + +// TrimTrailingNewline removes the trailing new line form a string. this method +// is used mostly on outputs of a command +func TrimTrailingNewline(str string) string { + if strings.HasSuffix(str, "\n") { + return str[:len(str)-1] + } + return str +} + // GenericGitCommand runs any git command without expecting output func GenericGitCommand(repoPath string, args []string) error { - _, err := helpers.RunCommandWithOutput(repoPath, "git", args) + _, err := RunCommandWithOutput(repoPath, "git", args) if err != nil { return err } @@ -15,26 +71,26 @@ func GenericGitCommand(repoPath string, args []string) error { // GenericGitCommandWithOutput runs any git command with returning output func GenericGitCommandWithOutput(repoPath string, args []string) (string, error) { - out, err := helpers.RunCommandWithOutput(repoPath, "git", args) + out, err := RunCommandWithOutput(repoPath, "git", args) if err != nil { return "?", err } - return helpers.TrimTrailingNewline(out), nil + return TrimTrailingNewline(out), nil } // GenericGitCommandWithErrorOutput runs any git command with returning output func GenericGitCommandWithErrorOutput(repoPath string, args []string) (string, error) { - out, err := helpers.RunCommandWithOutput(repoPath, "git", args) + out, err := RunCommandWithOutput(repoPath, "git", args) if err != nil { - return helpers.TrimTrailingNewline(out), err + return TrimTrailingNewline(out), err } - return helpers.TrimTrailingNewline(out), nil + return TrimTrailingNewline(out), nil } // GitShow is conventional git show command without any argument func GitShow(repoPath, hash string) string { args := []string{"show", hash} - diff, err := helpers.RunCommandWithOutput(repoPath, "git", args) + diff, err := RunCommandWithOutput(repoPath, "git", args) if err != nil { return "?" } @@ -44,7 +100,7 @@ func GitShow(repoPath, hash string) string { // GitShowEmail gets author's e-mail with git show command func GitShowEmail(repoPath, hash string) string { args := []string{"show", "--quiet", "--pretty=format:%ae", hash} - diff, err := helpers.RunCommandWithOutput(repoPath, "git", args) + diff, err := RunCommandWithOutput(repoPath, "git", args) if err != nil { return "?" } @@ -54,7 +110,7 @@ func GitShowEmail(repoPath, hash string) string { // GitShowBody gets body of the commit with git show func GitShowBody(repoPath, hash string) string { args := []string{"show", "--quiet", "--pretty=format:%B", hash} - diff, err := helpers.RunCommandWithOutput(repoPath, "git", args) + diff, err := RunCommandWithOutput(repoPath, "git", args) if err != nil { return err.Error() } @@ -64,7 +120,7 @@ func GitShowBody(repoPath, hash string) string { // GitShowDate gets commit's date with git show as string func GitShowDate(repoPath, hash string) string { args := []string{"show", "--quiet", "--pretty=format:%ai", hash} - diff, err := helpers.RunCommandWithOutput(repoPath, "git", args) + diff, err := RunCommandWithOutput(repoPath, "git", args) if err != nil { return "?" } @@ -74,7 +130,7 @@ func GitShowDate(repoPath, hash string) string { // StatusWithGit returns the plaintext short status of the repo func (e *RepoEntity) StatusWithGit() string { args := []string{"status"} - status, err := helpers.RunCommandWithOutput(e.AbsPath, "git", args) + status, err := RunCommandWithOutput(e.AbsPath, "git", args) if err != nil { return "?" } diff --git a/pkg/git/repository.go b/pkg/git/repository.go index 0e57482..0c02f26 100644 --- a/pkg/git/repository.go +++ b/pkg/git/repository.go @@ -6,9 +6,8 @@ import ( "sync" "time" - "github.com/isacikgoz/gitbatch/pkg/helpers" log "github.com/sirupsen/logrus" - "gopkg.in/src-d/go-git.v4" + git "gopkg.in/src-d/go-git.v4" ) // RepoEntity is the main entity of the application. The repository name is @@ -82,7 +81,7 @@ func FastInitializeRepo(dir string) (e *RepoEntity, err error) { return nil, err } // initialize RepoEntity with minimum viable fields - e = &RepoEntity{RepoID: helpers.RandomString(8), + e = &RepoEntity{RepoID: RandomString(8), Name: fstat.Name(), AbsPath: dir, ModTime: fstat.ModTime(), diff --git a/pkg/helpers/utils.go b/pkg/git/util-random.go index 35de2c3..c388613 100644 --- a/pkg/helpers/utils.go +++ b/pkg/git/util-random.go @@ -1,31 +1,13 @@ -package helpers +package git import ( "math/rand" - "strings" "time" ) var characterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") var r = rand.New(rand.NewSource(time.Now().UnixNano())) -// TrimTrailingNewline removes the trailing new line form a string. this method -// is used mostly on outputs of a command -func TrimTrailingNewline(str string) string { - if strings.HasSuffix(str, "\n") { - return str[:len(str)-1] - } - return str -} - -// Min finds the minimum value of two int -func Min(x, y int) int { - if x < y { - return x - } - return y -} - // RandomString generates a random string of n length func RandomString(n int) string { b := make([]rune, n) diff --git a/pkg/helpers/utils_test.go b/pkg/git/util-random_test.go index a6a56a4..2eeb00c 100644 --- a/pkg/helpers/utils_test.go +++ b/pkg/git/util-random_test.go @@ -1,4 +1,4 @@ -package helpers +package git import "testing" diff --git a/pkg/gui/mainview.go b/pkg/gui/mainview.go index 869b334..6cd43bb 100644 --- a/pkg/gui/mainview.go +++ b/pkg/gui/mainview.go @@ -139,6 +139,9 @@ func (gui *Gui) pageDown(g *gocui.Gui, v *gocui.View) error { cx, _ := v.Cursor() _, vy := v.Size() lr := len(gui.State.Repositories) + if lr < vy { + return nil + } if oy+vy >= lr-vy { if err := v.SetOrigin(ox, lr-vy); err != nil { return err diff --git a/pkg/gui/util-common.go b/pkg/gui/util-common.go index 92c49b9..b25848c 100644 --- a/pkg/gui/util-common.go +++ b/pkg/gui/util-common.go @@ -1,7 +1,6 @@ package gui import ( - "github.com/isacikgoz/gitbatch/pkg/helpers" "github.com/jroimartin/gocui" log "github.com/sirupsen/logrus" ) @@ -94,7 +93,7 @@ func (gui *Gui) correctCursor(v *gocui.View) error { if oy+cy <= ly { return nil } - newCy := helpers.Min(ly, maxY) + newCy := min(ly, maxY) if err := v.SetCursor(cx, newCy); err != nil { return err } @@ -102,6 +101,14 @@ func (gui *Gui) correctCursor(v *gocui.View) error { return err } +// min finds the minimum value of two int +func min(x, y int) int { + if x < y { + return x + } + return y +} + // this function handles the iteration of a side view and set its origin point // so that the selected line can be in the middle of the view func (gui *Gui) smartAnchorRelativeToLine(v *gocui.View, currentindex, totallines int) error { diff --git a/pkg/helpers/command.go b/pkg/helpers/command.go deleted file mode 100644 index b861812..0000000 --- a/pkg/helpers/command.go +++ /dev/null @@ -1,51 +0,0 @@ -package helpers - -import ( - "log" - "os/exec" - "syscall" -) - -// RunCommandWithOutput runs the OS command and return its output. If the output -// returns error it also encapsulates it as a golang.error which is a return code -// of the command except zero -func RunCommandWithOutput(dir string, command string, args []string) (string, error) { - cmd := exec.Command(command, args...) - if dir != "" { - cmd.Dir = dir - } - output, err := cmd.Output() - return string(output), err -} - -// GetCommandStatus returns if we supposed to get return value as an int of a command -// this method can be used. It is practical when you use a command and process a -// failover acoording to a soecific return code -func GetCommandStatus(dir string, command string, args []string) (int, error) { - cmd := exec.Command(command, args...) - if dir != "" { - cmd.Dir = dir - } - var err error - // this time the execution is a little different - if err := cmd.Start(); err != nil { - return -1, err - } - if err := cmd.Wait(); err != nil { - if exiterr, ok := err.(*exec.ExitError); ok { - // The program has exited with an exit code != 0 - - // This works on both Unix and Windows. Although package - // syscall is generally platform dependent, WaitStatus is - // defined for both Unix and Windows and in both cases has - // an ExitStatus() method with the same signature. - if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { - statusCode := status.ExitStatus() - return statusCode, err - } - } else { - log.Fatalf("cmd.Wait: %v", err) - } - } - return -1, err -} |
