Skip to content

Commit

Permalink
feat: added cmd completed to commands (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Guduru authored Apr 25, 2024
1 parent e782a43 commit bf0f6aa
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 70 deletions.
27 changes: 27 additions & 0 deletions cmd/analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package analytics

import (
"ldcli/cmd/cliflags"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

func CmdRunEventProperties(cmd *cobra.Command, name string) map[string]interface{} {
baseURI := viper.GetString(cliflags.BaseURIFlag)
var flags []string
cmd.Flags().Visit(func(f *pflag.Flag) {
flags = append(flags, f.Name)
})

properties := map[string]interface{}{
"name": name,
"action": cmd.CalledAs(),
"flags": flags,
}
if baseURI != cliflags.BaseURIDefault {
properties["baseURI"] = baseURI
}
return properties
}
15 changes: 15 additions & 0 deletions cmd/cmdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"os"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/require"

"ldcli/cmd/cliflags"
"ldcli/internal/analytics"
)

Expand Down Expand Up @@ -35,9 +37,22 @@ func CallCmd(

err = rootCmd.Execute()
if err != nil {
tracker.SendCommandCompletedEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
analytics.ERROR,
)
return nil, err
}

tracker.SendCommandCompletedEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
analytics.SUCCESS,
)

out, err := io.ReadAll(b)
require.NoError(t, err)

Expand Down
6 changes: 3 additions & 3 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/viper"
"gopkg.in/yaml.v3"

cmdAnalytics "ldcli/cmd/analytics"
"ldcli/cmd/cliflags"
"ldcli/internal/analytics"
"ldcli/internal/config"
Expand All @@ -29,12 +30,11 @@ func NewConfigCmd(analyticsTracker analytics.Tracker) *cobra.Command {
Short: "View and modify specific configuration values",
Use: "config",
PreRun: func(cmd *cobra.Command, args []string) {
analyticsTracker.SendEvent(
analyticsTracker.SendCommandRunEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "config"),
cmdAnalytics.CmdRunEventProperties(cmd, "config"),
)
},
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/environments/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmdAnalytics "ldcli/cmd/analytics"
"ldcli/cmd/cliflags"
"ldcli/internal/analytics"
"ldcli/internal/environments"
Expand All @@ -18,12 +19,11 @@ func NewEnvironmentsCmd(
Short: "Make requests (list, create, etc.) on environments",
Long: "Make requests (list, create, etc.) on environments",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
analyticsTracker.SendEvent(
analyticsTracker.SendCommandRunEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "environments"),
cmdAnalytics.CmdRunEventProperties(cmd, "environments"),
)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/environments/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestGet(t *testing.T) {
"base-uri",
"environment",
"project",
})
}, analytics.SUCCESS)
client := environments.MockClient{}
client.
On("Get", mockArgs...).
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestCreate(t *testing.T) {
"base-uri",
"data",
"project",
})
}, analytics.SUCCESS)

client := flags.MockClient{}
client.
Expand Down
6 changes: 3 additions & 3 deletions cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmdAnalytics "ldcli/cmd/analytics"
"ldcli/cmd/cliflags"
"ldcli/internal/analytics"
"ldcli/internal/flags"
Expand All @@ -15,12 +16,11 @@ func NewFlagsCmd(analyticsTracker analytics.Tracker, client flags.Client) (*cobr
Short: "Make requests (list, create, etc.) on flags",
Long: "Make requests (list, create, etc.) on flags",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
analyticsTracker.SendEvent(
analyticsTracker.SendCommandRunEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "flags"),
cmdAnalytics.CmdRunEventProperties(cmd, "flags"),
)
},
}
Expand Down
34 changes: 33 additions & 1 deletion cmd/flags/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestGet(t *testing.T) {
"flag",
"output",
"project",
})
}, analytics.SUCCESS)

client := flags.MockClient{}
client.
Expand All @@ -156,4 +156,36 @@ func TestGet(t *testing.T) {
_, err := cmd.CallCmd(t, clients, tracker, args)
require.NoError(t, err)
})

t.Run("will track analytics for api error", func(t *testing.T) {
tracker := analytics.MockedTracker(
"flags",
"get",
[]string{
"access-token",
"base-uri",
"environment",
"flag",
"project",
}, analytics.ERROR)
client := flags.MockClient{}
client.
On("Get", mockArgs...).
Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`))
clients := cmd.APIClients{
FlagsClient: &client,
}

args := []string{
"flags", "get",
"--access-token", "testAccessToken",
"--base-uri", "http://test.com",
"--flag", "test-key",
"--project", "test-proj-key",
"--environment", "test-env-key",
}

_, err := cmd.CallCmd(t, clients, tracker, args)
require.EqualError(t, err, "An error")
})
}
4 changes: 2 additions & 2 deletions cmd/flags/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestUpdate(t *testing.T) {
"flag",
"output",
"project",
})
}, analytics.SUCCESS)

client := flags.MockClient{}
client.
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestToggle(t *testing.T) {
"flag",
"output",
"project",
})
}, analytics.SUCCESS)

client := flags.MockClient{}
client.
Expand Down
4 changes: 2 additions & 2 deletions cmd/members/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func TestCreate(t *testing.T) {
"access-token",
"base-uri",
"data",
"output",
})
"output",
}, analytics.SUCCESS)

client := members.MockClient{}
client.
Expand Down
6 changes: 3 additions & 3 deletions cmd/members/invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func TestInvite(t *testing.T) {
"access-token",
"base-uri",
"emails",
"output",
})
"output",
}, analytics.SUCCESS)

client := members.MockClient{}
client.
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestInviteWithOptionalRole(t *testing.T) {
"emails",
"output",
"role",
})
}, analytics.SUCCESS)

client := members.MockClient{}
client.
Expand Down
6 changes: 3 additions & 3 deletions cmd/members/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmdAnalytics "ldcli/cmd/analytics"
"ldcli/cmd/cliflags"
"ldcli/internal/analytics"
"ldcli/internal/members"
Expand All @@ -15,12 +16,11 @@ func NewMembersCmd(analyticsTracker analytics.Tracker, client members.Client) (*
Short: "Make requests (list, create, etc.) on members",
Long: "Make requests (list, create, etc.) on members",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
analyticsTracker.SendEvent(
analyticsTracker.SendCommandRunEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "members"),
cmdAnalytics.CmdRunEventProperties(cmd, "members"),
)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/projects/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestCreate(t *testing.T) {
"access-token",
"base-uri",
"data",
})
}, analytics.SUCCESS)

client := projects.MockClient{}
client.
Expand Down
2 changes: 1 addition & 1 deletion cmd/projects/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestList(t *testing.T) {
[]string{
"access-token",
"base-uri",
})
}, analytics.SUCCESS)

client := projects.MockClient{}
client.
Expand Down
6 changes: 3 additions & 3 deletions cmd/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmdAnalytics "ldcli/cmd/analytics"
"ldcli/cmd/cliflags"
"ldcli/internal/analytics"
"ldcli/internal/projects"
Expand All @@ -15,12 +16,11 @@ func NewProjectsCmd(analyticsTracker analytics.Tracker, client projects.Client)
Short: "Make requests (list, create, etc.) on projects",
Long: "Make requests (list, create, etc.) on projects",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
analyticsTracker.SendEvent(
analyticsTracker.SendCommandRunEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "projects"),
cmdAnalytics.CmdRunEventProperties(cmd, "projects"),
)
},
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,18 @@ func Execute(analyticsTracker analytics.Tracker, version string) {
}

err = rootCmd.Execute()
outcome := analytics.SUCCESS
if err != nil {
outcome = analytics.ERROR
fmt.Fprintln(os.Stderr, err.Error())
}

analyticsTracker.SendCommandCompletedEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIDefault),
viper.GetBool(cliflags.AnalyticsOptOut),
outcome,
)
}

// setFlagsFromConfig reads in the config file if it exists and uses any flag values for commands.
Expand Down
Loading

0 comments on commit bf0f6aa

Please sign in to comment.