go-gitcmd
Go (golang) package providing for tiny git command wrapper.
Installation
To install go-gitcmd
, simply run:
$ go get -u github.com/tsuyoshiwada/go-gitcmd
Usage
It is the simplest example.
package main
import (
"log"
"github.com/tsuyoshiwada/go-gitcmd"
)
func main() {
git := gitcmd.New(nil) // or `git := gitcmd.New(&Config{Bin: "/your/custom/git/bin"})`
out, err := git.Exec("rev-parse", "--git-dir")
if err != nil {
log.Fatal(err)
}
fmt.Println(out) // ".git"
}
See godoc for API detail 👍
How to Mock
Since Client
is an interface, it can easily be Mocked.
type MockClient struct {
gitcmd.Client // Interface embedding
ReturnCanExec func() error
ReturnExec func(string, ...string) (string, error)
ReturnInsideWorkTree func() error
}
func (m *MockClient) CanExec() error {
return m.ReturnCanExec()
}
func (m *MockClient) Exec(subcmd string, args ...string) (string, error) {
return m.ReturnExec(subcmd, args...)
}
func (m *MockClient) InsideWorkTree() error {
return m.ReturnInsideWorkTree()
}
func main() {
git := &MockClient{}
// Set `InsideWorkTree()` mock function
git.ReturnInsideWorkTree = func() error {
return errors.New("error...")
}
err := git.InsideWorkTree()
fmt.Println(err.Error()) // "error..."
}
Contribute
- Fork (https://github.com/tsuyoshiwada/go-gitcmd)
- Create a feature branch
- Commit your changes
- Rebase your local changes against the master branch
- Run test suite with the
go test
command and confirm that it passes
- Create new Pull Request 💪
Bugs, feature requests and comments are more than welcome in the issues.
License
MIT © tsuyoshiwada