Skip to content

Commit

Permalink
chore: Moved create telemetry in its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed Jul 11, 2023
1 parent 24c6a12 commit 15f00bd
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 40 deletions.
5 changes: 3 additions & 2 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os"

"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
"github.com/spf13/cobra"
Expand All @@ -13,9 +14,9 @@ func newCompletionCommand(config *settings.Config) *cobra.Command {
Use: "completion",
Short: "Generate shell completion scripts",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
telemetryClient := createTelemetry(config)
telemetryClient := create_telemetry.CreateTelemetry(config)
defer telemetryClient.Close()
telemetryClient.Track(telemetry.CreateCompletionCommand(getCommandInformation(cmd, false)))
telemetryClient.Track(telemetry.CreateCompletionCommand(create_telemetry.GetCommandInformation(cmd, false)))
},
Run: func(cmd *cobra.Command, _ []string) {
err := cmd.Help()
Expand Down
9 changes: 5 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/CircleCI-Public/circleci-config/labeling"
"github.com/CircleCI-Public/circleci-config/labeling/codebase"

"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/config"
"github.com/CircleCI-Public/circleci-cli/filetree"
"github.com/CircleCI-Public/circleci-cli/proxy"
Expand Down Expand Up @@ -42,7 +43,7 @@ func newConfigCommand(globalConfig *settings.Config) *cobra.Command {
Use: "config",
Short: "Operate on build config files",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
telemetryClient = createTelemetry(globalConfig)
telemetryClient = create_telemetry.CreateTelemetry(globalConfig)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
closeTelemetryClient()
Expand All @@ -55,7 +56,7 @@ func newConfigCommand(globalConfig *settings.Config) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
defer closeTelemetryClient()
err := packConfig(args)
telemetryClient.Track(telemetry.CreateConfigEvent(getCommandInformation(cmd, true)))
telemetryClient.Track(telemetry.CreateConfigEvent(create_telemetry.GetCommandInformation(cmd, true)))
return err
},
Args: cobra.ExactArgs(1),
Expand Down Expand Up @@ -88,7 +89,7 @@ func newConfigCommand(globalConfig *settings.Config) *cobra.Command {
IgnoreDeprecatedImages: ignoreDeprecatedImages,
VerboseOutput: verboseOutput,
})
telemetryClient.Track(telemetry.CreateConfigEvent(getCommandInformation(cmd, true)))
telemetryClient.Track(telemetry.CreateConfigEvent(create_telemetry.GetCommandInformation(cmd, true)))

return err
},
Expand Down Expand Up @@ -131,7 +132,7 @@ func newConfigCommand(globalConfig *settings.Config) *cobra.Command {
PipelineParamsFilePath: pipelineParamsFilePath,
VerboseOutput: verboseOutput,
})
telemetryClient.Track(telemetry.CreateConfigEvent(getCommandInformation(cmd, true)))
telemetryClient.Track(telemetry.CreateConfigEvent(create_telemetry.GetCommandInformation(cmd, true)))

return err
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package create_telemetry

import (
"fmt"
Expand All @@ -18,7 +18,7 @@ import (
)

var (
createUUID = func() string { return uuid.New().String() }
CreateUUID = func() string { return uuid.New().String() }
isStdinATTY = term.IsTerminal(int(os.Stdin.Fd()))
)

Expand All @@ -32,15 +32,21 @@ func (telemetryInteractiveUI) AskUserToApproveTelemetry(message string) bool {
return prompt.AskUserToConfirmWithDefault(message, true)
}

type telemetryAPIClient interface {
getMyUserId() (string, error)
type TelemetryAPIClient interface {
GetMyUserId() (string, error)
}

func CreateAPIClient(config *settings.Config) TelemetryAPIClient {
return telemetryCircleCIAPI{
cli: rest.NewFromConfig(config.Host, config),
}
}

type telemetryCircleCIAPI struct {
cli *rest.Client
}

func (client telemetryCircleCIAPI) getMyUserId() (string, error) {
func (client telemetryCircleCIAPI) GetMyUserId() (string, error) {
me, err := api.GetMe(client.cli)
if err != nil {
return "", err
Expand All @@ -50,12 +56,12 @@ func (client telemetryCircleCIAPI) getMyUserId() (string, error) {

type nullTelemetryAPIClient struct{}

func (client nullTelemetryAPIClient) getMyUserId() (string, error) {
func (client nullTelemetryAPIClient) GetMyUserId() (string, error) {
panic("Should not be called")
}

// Make sure the user gave their approval for the telemetry and
func createTelemetry(config *settings.Config) telemetry.Client {
func CreateTelemetry(config *settings.Config) telemetry.Client {
if config.MockTelemetry != "" {
return telemetry.CreateFileTelemetry(config.MockTelemetry)
}
Expand All @@ -64,7 +70,7 @@ func createTelemetry(config *settings.Config) telemetry.Client {
return telemetry.CreateClient(telemetry.User{}, false)
}

var apiClient telemetryAPIClient = nullTelemetryAPIClient{}
var apiClient TelemetryAPIClient = nullTelemetryAPIClient{}
if config.HTTPClient != nil {
apiClient = telemetryCircleCIAPI{
cli: rest.NewFromConfig(config.Host, config),
Expand All @@ -74,7 +80,7 @@ func createTelemetry(config *settings.Config) telemetry.Client {

telemetrySettings := settings.TelemetrySettings{}
user := telemetry.User{
IsSelfHosted: config.Host == defaultHost,
IsSelfHosted: config.Host == "https://circleci.com",
OS: runtime.GOOS,
Version: version.Version,
TeamName: "devex",
Expand All @@ -86,7 +92,7 @@ func createTelemetry(config *settings.Config) telemetry.Client {
return client
}

func loadTelemetrySettings(settings *settings.TelemetrySettings, user *telemetry.User, apiClient telemetryAPIClient, ui telemetryUI) {
func loadTelemetrySettings(settings *settings.TelemetrySettings, user *telemetry.User, apiClient TelemetryAPIClient, ui telemetryUI) {
err := settings.Load()
if err != nil && !os.IsNotExist(err) {
fmt.Printf("Error loading telemetry configuration: %s\n", err)
Expand All @@ -99,7 +105,7 @@ func loadTelemetrySettings(settings *settings.TelemetrySettings, user *telemetry
if settings.HasAnsweredPrompt {
// If we have no user id, we try requesting the user id again
if settings.UserID == "" && settings.IsActive {
myID, err := apiClient.getMyUserId()
myID, err := apiClient.GetMyUserId()
if err == nil {
settings.UserID = myID
user.UserID = myID
Expand Down Expand Up @@ -136,12 +142,12 @@ func loadTelemetrySettings(settings *settings.TelemetrySettings, user *telemetry
// Make sure we have user info and set them
if settings.IsActive {
if settings.UniqueID == "" {
settings.UniqueID = createUUID()
settings.UniqueID = CreateUUID()
}
user.UniqueID = settings.UniqueID

if settings.UserID == "" {
myID, err := apiClient.getMyUserId()
myID, err := apiClient.GetMyUserId()
if err == nil {
settings.UserID = myID
}
Expand Down Expand Up @@ -172,7 +178,7 @@ func loadTelemetrySettings(settings *settings.TelemetrySettings, user *telemetry
// If getParent is true, puts both the command's args in `LocalArgs` and the parent's args
// Else only put the command's args
// Note: child flags overwrite parent flags with same name
func getCommandInformation(cmd *cobra.Command, getParent bool) telemetry.CommandInfo {
func GetCommandInformation(cmd *cobra.Command, getParent bool) telemetry.CommandInfo {
localArgs := map[string]string{}

parent := cmd.Parent()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package create_telemetry

import (
"path/filepath"
Expand Down Expand Up @@ -63,7 +63,7 @@ type telemetryTestAPIClient struct {
err error
}

func (me telemetryTestAPIClient) getMyUserId() (string, error) {
func (me telemetryTestAPIClient) GetMyUserId() (string, error) {
return me.id, me.err
}

Expand All @@ -73,9 +73,9 @@ func TestLoadTelemetrySettings(t *testing.T) {
uniqueId := "unique-id"

// Mock create UUID
oldUUIDCreate := createUUID
createUUID = func() string { return uniqueId }
defer (func() { createUUID = oldUUIDCreate })()
oldUUIDCreate := CreateUUID
CreateUUID = func() string { return uniqueId }
defer (func() { CreateUUID = oldUUIDCreate })()

// Create test cases
type args struct {
Expand Down
3 changes: 2 additions & 1 deletion cmd/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/CircleCI-Public/circleci-cli/api/graphql"
"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
"github.com/spf13/cobra"
Expand All @@ -30,7 +31,7 @@ func newDiagnosticCommand(config *settings.Config) *cobra.Command {
opts.cl = graphql.NewClient(config.HTTPClient, config.Host, config.Endpoint, config.Token, config.Debug)
},
RunE: func(_ *cobra.Command, _ []string) error {
telemetryClient := createTelemetry(config)
telemetryClient := create_telemetry.CreateTelemetry(config)
defer telemetryClient.Close()

err := diagnostic(opts)
Expand Down
3 changes: 2 additions & 1 deletion cmd/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/git"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
Expand Down Expand Up @@ -55,7 +56,7 @@ func followProjectCommand(config *settings.Config) *cobra.Command {
Use: "follow",
Short: "Attempt to follow the project for the current git repository.",
RunE: func(_ *cobra.Command, _ []string) error {
telemetryClient := createTelemetry(config)
telemetryClient := create_telemetry.CreateTelemetry(config)
defer telemetryClient.Close()

err := followProject(opts)
Expand Down
3 changes: 2 additions & 1 deletion cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"strings"

"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/git"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
Expand Down Expand Up @@ -46,7 +47,7 @@ func newOpenCommand(config *settings.Config) *cobra.Command {
Use: "open",
Short: "Open the current project in the browser.",
RunE: func(_ *cobra.Command, _ []string) error {
telemetryClient := createTelemetry(config)
telemetryClient := create_telemetry.CreateTelemetry(config)
defer telemetryClient.Close()

err := openProjectInBrowser()
Expand Down
3 changes: 2 additions & 1 deletion cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/CircleCI-Public/circleci-cli/api/graphql"
"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/prompt"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
Expand Down Expand Up @@ -190,7 +191,7 @@ func setup(opts setupOptions) error {
setupDiagnosticCheck(opts)
}

telemetryClient := createTelemetry(opts.cfg)
telemetryClient := create_telemetry.CreateTelemetry(opts.cfg)
defer telemetryClient.Close()
if err := telemetryClient.Track(telemetry.CreateSetupEvent(opts.cfg.Host == defaultHost)); err != nil {
fmt.Printf("Unable to send telemetry event: %s\n", err)
Expand Down
12 changes: 5 additions & 7 deletions cmd/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package cmd
import (
"os"

"github.com/CircleCI-Public/circleci-cli/api/rest"
"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func newTelemetryCommand(config *settings.Config) *cobra.Command {
apiClient := telemetryCircleCIAPI{
cli: rest.NewFromConfig(config.Host, config),
}
apiClient := create_telemetry.CreateAPIClient(config)

telemetryEnable := &cobra.Command{
Use: "enable",
Expand Down Expand Up @@ -46,7 +44,7 @@ Note: If you have not configured your telemetry preferences and call the CLI wit
return telemetryCommand
}

func setIsTelemetryActive(apiClient telemetryAPIClient, isActive bool) error {
func setIsTelemetryActive(apiClient create_telemetry.TelemetryAPIClient, isActive bool) error {
settings := settings.TelemetrySettings{}
if err := settings.Load(); err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "Loading telemetry configuration")
Expand All @@ -56,11 +54,11 @@ func setIsTelemetryActive(apiClient telemetryAPIClient, isActive bool) error {
settings.IsActive = isActive

if settings.UniqueID == "" {
settings.UniqueID = createUUID()
settings.UniqueID = create_telemetry.CreateUUID()
}

if settings.UserID == "" {
if myID, err := apiClient.getMyUserId(); err == nil {
if myID, err := apiClient.GetMyUserId(); err == nil {
settings.UserID = myID
}
}
Expand Down
12 changes: 11 additions & 1 deletion cmd/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import (
"path/filepath"
"testing"

"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/spf13/afero"
"gotest.tools/v3/assert"
)

type telemetryTestAPIClient struct {
id string
err error
}

func (me telemetryTestAPIClient) GetMyUserId() (string, error) {
return me.id, me.err
}

func TestSetIsTelemetryActive(t *testing.T) {
type args struct {
apiClient telemetryAPIClient
apiClient create_telemetry.TelemetryAPIClient
isActive bool
settings *settings.TelemetrySettings
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
"github.com/CircleCI-Public/circleci-cli/update"
Expand Down Expand Up @@ -31,9 +32,9 @@ func newUpdateCommand(config *settings.Config) *cobra.Command {
Short: "Update the tool to the latest version",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
opts.cfg.SkipUpdateCheck = true
telemetryClient := createTelemetry(config)
telemetryClient := create_telemetry.CreateTelemetry(config)
defer telemetryClient.Close()
telemetryClient.Track(telemetry.CreateUpdateEvent(getCommandInformation(cmd, cmd.Name() != "update")))
telemetryClient.Track(telemetry.CreateUpdateEvent(create_telemetry.GetCommandInformation(cmd, cmd.Name() != "update")))
},
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
Expand Down
3 changes: 2 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

"github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
"github.com/CircleCI-Public/circleci-cli/version"
Expand All @@ -24,7 +25,7 @@ func newVersionCommand(config *settings.Config) *cobra.Command {
Use: "version",
Short: "Display version information",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
telemetryClient = createTelemetry(config)
telemetryClient = create_telemetry.CreateTelemetry(config)
opts.cfg.SkipUpdateCheck = true
},
PreRun: func(cmd *cobra.Command, args []string) {
Expand Down

0 comments on commit 15f00bd

Please sign in to comment.