diff --git a/Taskfile.yml b/Taskfile.yml index ad2d19df9..f261e03a9 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -47,14 +47,15 @@ tasks: build: desc: Build main cmds: - - go build -v -o build/darwin/amd64/circleci . - + # LDFlags sets the segment endpoint to an empty string so that all it is replaced by the default value + - go build -v -o build/darwin/amd64/circleci -ldflags="-X 'github.com/CircleCI-Public/circleci-cli/telemetry.SegmentEndpoint=' -X 'github.com/CircleCI-Public/circleci-cli/telemetry.SegmentKey=AbgkrgN4cbRhAVEwlzMkHbwvrXnxHh35'" . + build-linux: desc: Build main cmds: - go build -v -o build/linux/amd64/circleci . - + cover: desc: tests and generates a cover profile cmds: - - TESTING=true go test -race -coverprofile=coverage.txt ./... \ No newline at end of file + - TESTING=true go test -race -coverprofile=coverage.txt ./... diff --git a/telemetry/telemetry.go b/telemetry/telemetry.go index c4c0c89f9..59a0298dd 100644 --- a/telemetry/telemetry.go +++ b/telemetry/telemetry.go @@ -9,13 +9,16 @@ import ( "github.com/segmentio/analytics-go" ) -type Approval string - var ( // Overwrite this function for tests CreateActiveTelemetry = newSegmentClient + + SegmentEndpoint = "localhost" + SegmentKey = "" ) +type Approval string + const ( Enabled Approval = "enabled" Disabled Approval = "disabled" @@ -86,25 +89,28 @@ type segmentClient struct { user User } -const ( - segmentKey = "" -) - func newSegmentClient(user User) Client { - cli := analytics.New(segmentKey) + cli, err := analytics.NewWithConfig(SegmentKey, analytics.Config{ + Endpoint: SegmentEndpoint, + }) - userID := user.UniqueID - if userID == "" { - userID = "none" + if err != nil { + return CreateNullClient() } - err := cli.Enqueue( + if len(user.UniqueID) == 0 { + user.UniqueID = "null" + } + + err = cli.Enqueue( analytics.Identify{ - UserId: userID, + UserId: user.UniqueID, Traits: analytics.NewTraits().Set("os", user.OS), }, ) - fmt.Printf("Error while identifying with telemetry: %s\n", err) + if err != nil { + fmt.Printf("Error while identifying with telemetry: %s\n", err) + } return &segmentClient{cli, user} }