summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git.go
diff options
context:
space:
mode:
authorAndrei Miulescu <lusu777@gmail.com>2018-08-12 19:31:27 +1000
committerAndrei Miulescu <lusu777@gmail.com>2018-08-12 19:31:27 +1000
commitdcd461d29f21a9626d5298a03283b6d8b46312c3 (patch)
tree42f43f27eb7403c60cc05805fc627debff76417b /pkg/commands/git.go
parent98c22a36fdaf8806f8fafe8f1e23e53f8e97658d (diff)
Restrucure project in a way where it is more modular
Diffstat (limited to 'pkg/commands/git.go')
-rw-r--r--pkg/commands/git.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
new file mode 100644
index 000000000..17855b98e
--- /dev/null
+++ b/pkg/commands/git.go
@@ -0,0 +1,71 @@
+package commands
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/Sirupsen/logrus"
+ git "gopkg.in/src-d/go-git.v4"
+)
+
+// GitCommand is our main git interface
+type GitCommand struct {
+ Log *logrus.Logger
+ OSCommand *OSCommand
+ Worktree *git.Worktree
+ Repo *git.Repository
+}
+
+// NewGitCommand it runs git commands
+func NewGitCommand(log *logrus.Logger, osCommand *OSCommand) (*GitCommand, error) {
+ gitCommand := &GitCommand{
+ Log: log,
+ OSCommand: osCommand,
+ }
+ return gitCommand, nil
+}
+
+// SetupGit sets git repo up
+func (c *GitCommand) SetupGit() {
+ c.verifyInGitRepo()
+ c.navigateToRepoRootDirectory()
+ c.setupWorktree()
+}
+
+func (c *GitCommand) GitIgnore(filename string) {
+ if _, err := c.OSCommand.RunDirectCommand("echo '" + filename + "' >> .gitignore"); err != nil {
+ panic(err)
+ }
+}
+
+func (c *GitCommand) verifyInGitRepo() {
+ if output, err := c.OSCommand.RunCommand("git status"); err != nil {
+ fmt.Println(output)
+ os.Exit(1)
+ }
+}
+
+func (c *GitCommand) navigateToRepoRootDirectory() {
+ _, err := os.Stat(".git")
+ for os.IsNotExist(err) {
+ c.Log.Debug("going up a directory to find the root")
+ os.Chdir("..")
+ _, err = os.Stat(".git")
+ }
+}
+
+func (c *GitCommand) setupWorktree() {
+ var err error
+ r, err := git.PlainOpen(".")
+ if err != nil {
+ panic(err)
+ }
+ c.Repo = r
+
+ w, err := r.Worktree()
+ c.Worktree = w
+ if err != nil {
+ panic(err)
+ }
+ c.Worktree = w
+}