Skip to content

Commit

Permalink
Use shell as command executor
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Meinhardt committed Mar 30, 2019
1 parent acf7d0a commit 8fdb12a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
20 changes: 10 additions & 10 deletions git/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ type Service interface {
}

type service struct {
gitPath string
shell string
Service
}

func NewGitService(gitPath string) Service {
return service{gitPath: gitPath}
func NewGitService(pathToShell string) Service {
return service{shell: pathToShell}
}

func (s service) GitRepoPath() (string, error) {
cmd := exec.Command(s.gitPath, "rev-parse", "--show-toplevel")
cmd := exec.Command(s.shell, "-c", "git rev-parse --show-toplevel")
var stdout, stderr bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
Expand All @@ -39,7 +39,7 @@ func (s service) GitRepoPath() (string, error) {
}

func (s service) IsRepoClean() (bool, error) {
cmd := exec.Command(s.gitPath, "status", "-s")
cmd := exec.Command(s.shell, "-c", "git status -s")
var stdout, stderr bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
Expand All @@ -48,23 +48,23 @@ func (s service) IsRepoClean() (bool, error) {
}

func (s service) CreateTag(version string) error {
cmd := exec.Command(s.gitPath, "tag", "-a", "v"+version, "-m", fmt.Sprintf("create new tag v%s", version))
cmd := exec.Command(s.shell, "-c", fmt.Sprintf("git tag -a v%s -m 'create new tag v%s'", version, version))
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
return errors.Wrap(err, fmt.Sprintf("pkg(git) CreateTag(): %s", stderr.String()))
}

func (s service) Push() error {
cmd := exec.Command(s.gitPath, "push", "--follow-tags")
cmd := exec.Command(s.shell, "-c", "git push --follow-tags")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
return errors.Wrap(err, fmt.Sprintf("pkg(git) Push(): %s", stderr.String()))
}

func (s service) PushTag(version string) error {
cmd := exec.Command(s.gitPath, "push", "origin", "v"+version)
cmd := exec.Command(s.shell, "-c", fmt.Sprintf("git push origin v%s", version))
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
Expand All @@ -77,15 +77,15 @@ func (s service) AddVersionChanges(filename string) error {
return err
}
filePath := repoPath + "/" + filename
cmd := exec.Command(s.gitPath, "add", filePath)
cmd := exec.Command(s.shell, "-c", fmt.Sprintf("git add %s", filePath))
var stderr bytes.Buffer
cmd.Stderr = &stderr
err = cmd.Run()
return errors.Wrap(err, fmt.Sprintf("pkg(git) AddVersionChanges(): %s", stderr.String()))
}

func (s service) CommitVersionChanges(version string) error {
cmd := exec.Command(s.gitPath, "commit", "-m", fmt.Sprintf("add changes for version %s", version))
cmd := exec.Command(s.shell, "-c", fmt.Sprintf("git commit -m 'add changes for version %s'", version))
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var getCmd = &cobra.Command{

l := internal.NewLogger(rootCmdFlags.verbose)

gs := git.NewGitService(viper.GetString("gitPath"))
gs := git.NewGitService(viper.GetString("shellPath"))
repoPath, err := gs.GitRepoPath()
l.LogFatalOnError(err)

Expand Down
10 changes: 5 additions & 5 deletions pkg/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var rootCmdFlags struct {
gitPath string
shellPath string
verbose bool
push bool
createTag bool
Expand All @@ -23,14 +23,14 @@ var rootCmdFlags struct {
}

func init() {
rootCmd.PersistentFlags().StringVar(&rootCmdFlags.gitPath, "gitPath", "/usr/local/bin/git", "path to native git installation")
rootCmd.PersistentFlags().StringVar(&rootCmdFlags.shellPath, "shellPath", "/bin/bash", "path to shell executor")
rootCmd.PersistentFlags().BoolVarP(&rootCmdFlags.verbose, "verbose", "v", false, "more logs")
rootCmd.PersistentFlags().BoolVarP(&rootCmdFlags.push, "push", "P", false, "push git tags")
rootCmd.PersistentFlags().BoolVarP(&rootCmdFlags.createTag, "tag", "T", false, "create a git tag")
rootCmd.PersistentFlags().StringVarP(&rootCmdFlags.versionFile, "versionFile", "f", "VERSION", "name of version file")
rootCmd.PersistentFlags().StringVarP(&rootCmdFlags.versionFileType, "versionFileType", "t", "raw", "type of version file (json, raw)")

viper.BindPFlag("gitPath", rootCmd.PersistentFlags().Lookup("gitPath"))
viper.BindPFlag("shellPath", rootCmd.PersistentFlags().Lookup("shellPath"))
viper.BindPFlag("pushChanges", rootCmd.PersistentFlags().Lookup("push"))
viper.BindPFlag("tagVersions", rootCmd.PersistentFlags().Lookup("tag"))
viper.BindPFlag("versionFile", rootCmd.PersistentFlags().Lookup("versionFile"))
Expand All @@ -41,7 +41,7 @@ var rootCmd = &cobra.Command{
Use: "semver",
Short: "standalone tool to version your gitlab repo with semver",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
g := git.NewGitService(viper.GetString("gitPath"))
g := git.NewGitService(viper.GetString("shellPath"))
repoPath, _ := g.GitRepoPath()

viper.SetConfigName("semver.config")
Expand All @@ -58,7 +58,7 @@ var rootCmd = &cobra.Command{
var fs file.VersionFileService
var repoPath string
if rootCmdFlags.push || rootCmdFlags.createTag {
g = git.NewGitService(viper.GetString("gitPath"))
g = git.NewGitService(viper.GetString("shellPath"))
rp, err := g.GitRepoPath()
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var versionCmd = &cobra.Command{

l := internal.NewLogger(rootCmdFlags.verbose)

gs := git.NewGitService(viper.GetString("gitPath"))
gs := git.NewGitService(viper.GetString("shellPath"))
repoPath, err := gs.GitRepoPath()
l.LogFatalOnError(err)

Expand Down
2 changes: 1 addition & 1 deletion semver.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"gitPath": "/usr/local/bin/git",
"shellPath": "/bin/bash",
"versionFile": "VERSION",
"versionFileType": "raw",
"tagVersions": true,
Expand Down

0 comments on commit 8fdb12a

Please sign in to comment.