Skip to content

Commit

Permalink
lab: Remove git version from version command
Browse files Browse the repository at this point in the history
The 'lab version' command prints out the git version.  For example,

[prarit@prarit ~]$ lab version
git version 2.29.2
lab version master

Remove the git version output and move the option into the root file.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Mar 4, 2021
1 parent bcb88ad commit 69136b1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 92 deletions.
14 changes: 14 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,26 @@ var helpCmd = &cobra.Command{
Run: helpFunc,
}

// Version is set with linker flags during build.
var Version string

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("%s %s\n", "lab version", Version)
},
}

func init() {
// NOTE: Calling SetHelpCommand like this causes helpFunc to be called
// with correct arguments. If the default cobra help func is used no
// arguments are passed through and subcommand help breaks.
RootCmd.SetHelpCommand(helpCmd)
RootCmd.SetHelpFunc(helpFunc)
RootCmd.AddCommand(versionCmd)
RootCmd.Flags().Bool("version", false, "Show the lab version")
RootCmd.PersistentFlags().Bool("no-pager", false, "Do not pipe output into a pager")
}
Expand Down
39 changes: 39 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"bytes"
"io"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -407,3 +409,40 @@ func Test_flag_config_FT(t *testing.T) {
// configs overridden on the command line, comments should not be output
require.NotContains(t, string(b), `commented at`)
}

// Make sure the version command don't break things in the future
func Test_versionCmd(t *testing.T) {

t.Run("version", func(t *testing.T) {
lab_cmd := exec.Command(labBinaryPath, "version")
out, err := lab_cmd.CombinedOutput()
if err != nil {
t.Log(string(out))
t.Fatal(err)
}
assert.Contains(t, string(out), "lab version "+Version)
})
t.Run("--version", func(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, "lab version "+Version)
})
}
29 changes: 0 additions & 29 deletions cmd/version.go

This file was deleted.

63 changes: 0 additions & 63 deletions cmd/version_test.go

This file was deleted.

0 comments on commit 69136b1

Please sign in to comment.