diff --git a/cmd/cmdtest.go b/cmd/cmdtest.go index c4647072..d60cdc85 100644 --- a/cmd/cmdtest.go +++ b/cmd/cmdtest.go @@ -21,7 +21,7 @@ func CallCmd( projectsClient *projects.MockClient, args []string, ) ([]byte, error) { - rootCmd, err := NewRootCommand(flagsClient, membersClient, projectsClient) + rootCmd, err := NewRootCommand(flagsClient, membersClient, projectsClient, "test") require.NoError(t, err) b := bytes.NewBufferString("") rootCmd.SetOut(b) diff --git a/cmd/root.go b/cmd/root.go index f9eb78bc..575ea53f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,12 +17,12 @@ import ( "ldcli/internal/projects" ) -func NewRootCommand(flagsClient flags.Client, membersClient members.Client, projectsClient projects.Client) (*cobra.Command, error) { +func NewRootCommand(flagsClient flags.Client, membersClient members.Client, projectsClient projects.Client, version string) (*cobra.Command, error) { cmd := &cobra.Command{ Use: "ldcli", Short: "LaunchDarkly CLI", Long: "LaunchDarkly CLI to control your feature flags", - Version: "0.0.1", // TODO: set this based on release or use `cmd.SetVersionTemplate(s string)` + Version: version, // Handle errors differently based on type. // We don't want to show the usage if the user has the right structure but invalid data such as @@ -77,8 +77,8 @@ func NewRootCommand(flagsClient flags.Client, membersClient members.Client, proj return cmd, nil } -func Execute() { - rootCmd, err := NewRootCommand(flags.NewClient(), members.NewClient(), projects.NewClient()) +func Execute(version string) { + rootCmd, err := NewRootCommand(flags.NewClient(), members.NewClient(), projects.NewClient(), version) if err != nil { log.Fatal(err) } diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 00000000..b7dac8cc --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "ldcli/internal/flags" +) + +func TestCreate(t *testing.T) { + t.Run("with valid flags prints version", func(t *testing.T) { + client := flags.MockClient{} + args := []string{ + "--version", + } + + output, err := CallCmd(t, &client, nil, nil, args) + + require.NoError(t, err) + assert.Contains(t, string(output), `ldcli version test`) + }) +} diff --git a/main.go b/main.go index fbc373bb..ba8b7515 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,10 @@ package main import "ldcli/cmd" +var ( + version = "dev" +) + func main() { - cmd.Execute() + cmd.Execute(version) }