Skip to content

Commit

Permalink
feat: setting version dynamically (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Guduru authored Mar 28, 2024
1 parent 7002866 commit 7e4e794
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/cmdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
24 changes: 24 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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`)
})
}
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package main

import "ldcli/cmd"

var (
version = "dev"
)

func main() {
cmd.Execute()
cmd.Execute(version)
}

0 comments on commit 7e4e794

Please sign in to comment.