Skip to content

Commit

Permalink
[#138] support --version in addition to lab version
Browse files Browse the repository at this point in the history
  • Loading branch information
zaquestion committed Apr 21, 2018
1 parent eb6be9f commit 835e9e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ var RootCmd = &cobra.Command{
Short: "A Git Wrapper for GitLab",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if ok, err := cmd.Flags().GetBool("version"); err == nil && ok {
versionCmd.Run(cmd, args)
return
}
helpCmd.Run(cmd, args)
},
}
Expand Down Expand Up @@ -101,6 +105,7 @@ func init() {
// arguments are passed through and subcommand help breaks.
RootCmd.SetHelpCommand(helpCmd)
RootCmd.SetHelpFunc(helpFunc)
RootCmd.Flags().Bool("version", false, "Show the lab version")
}

// parseArgsRemote returns the remote and a number if parsed. Many commands
Expand Down
28 changes: 28 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"bytes"
"fmt"
"io"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -103,6 +106,31 @@ func TestRootNoArg(t *testing.T) {
fork Fork a remote repository on GitLab and add as remote`)
}

func TestRootVersion(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w

RootCmd.Flag("version").Value.Set("true")
RootCmd.Run(RootCmd, nil)

outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()

// back to normal state
w.Close()
os.Stdout = old // restoring the real stdout
out := <-outC

assert.Contains(t, out, "git version")
assert.Contains(t, out, fmt.Sprintf("lab version %s", Version))
}

func TestGitHelp(t *testing.T) {
cmd := exec.Command("../lab_bin")
expected, _ := cmd.CombinedOutput()
Expand Down

0 comments on commit 835e9e3

Please sign in to comment.