Skip to content

Commit

Permalink
inject Segment information at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed Aug 1, 2023
1 parent 7e7f1ee commit dbd1239
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
9 changes: 5 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
- TESTING=true go test -race -coverprofile=coverage.txt ./...
32 changes: 19 additions & 13 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}
}
Expand Down

0 comments on commit dbd1239

Please sign in to comment.