Skip to content

Commit 108b73c

Browse files
author
Gustavo Bazan
authored
feat: include output format in telemetry (#3481)
1 parent c0fd95c commit 108b73c

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

internal/telemetry/event.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,40 @@ func withOrgID(cmd CmdFlags, c OrgIDGetter) EventOpt {
290290
}
291291
}
292292

293+
type OutputGetter interface {
294+
Output() string
295+
}
296+
297+
func withOutput(cmd CmdFlags, c OutputGetter) EventOpt {
298+
return func(event Event) {
299+
if cmd.Flags().Changed(flag.Output) {
300+
v, _ := cmd.Flags().GetString(flag.Output)
301+
event.Properties["output"] = output(v)
302+
return
303+
}
304+
event.Properties["output"] = output(c.Output())
305+
}
306+
}
307+
308+
const (
309+
plaintextFormat = "plaintext"
310+
jsonFormat = "json"
311+
jsonPath = "json-path"
312+
goTemplate = "go-template"
313+
goTemplateFile = "go-template-file"
314+
)
315+
316+
var templateFormats = []string{jsonFormat, goTemplate, goTemplateFile, jsonPath}
317+
318+
func output(o string) string {
319+
for _, f := range templateFormats {
320+
if strings.HasPrefix(o, f) {
321+
return f
322+
}
323+
}
324+
return plaintextFormat
325+
}
326+
293327
type Printer interface {
294328
OutOrStdout() io.Writer
295329
}

internal/telemetry/event_test.go

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package telemetry
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"runtime"
2223
"testing"
2324
"time"
@@ -262,9 +263,12 @@ func TestSanitizePrompt(t *testing.T) {
262263
},
263264
}
264265

265-
for _, testCase := range testCases {
266-
got := sanitizePrompt(testCase.input)
267-
assert.Equal(t, testCase.expected, got)
266+
for i, tc := range testCases {
267+
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
268+
t.Parallel()
269+
got := sanitizePrompt(tc.input)
270+
assert.Equal(t, tc.expected, got)
271+
})
268272
}
269273
}
270274

@@ -283,9 +287,12 @@ func TestSanitizeSelectOption(t *testing.T) {
283287
},
284288
}
285289

286-
for _, testCase := range testCases {
287-
got := sanitizeSelectOption(testCase.input)
288-
assert.Equal(t, testCase.expected, got)
290+
for i, tc := range testCases {
291+
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
292+
t.Parallel()
293+
got := sanitizeSelectOption(tc.input)
294+
assert.Equal(t, tc.expected, got)
295+
})
289296
}
290297
}
291298

@@ -384,6 +391,55 @@ func TestWithEmptySearchIndexType(t *testing.T) {
384391
assert.Nil(t, e.Properties["search_index_type"])
385392
}
386393

394+
func Test_withOutput(t *testing.T) {
395+
tests := []struct {
396+
name string
397+
flagValue string
398+
configValue string
399+
want string
400+
}{
401+
{
402+
name: "from flag",
403+
flagValue: "json",
404+
configValue: "",
405+
want: "json",
406+
},
407+
{
408+
name: "from config",
409+
flagValue: "",
410+
configValue: "json",
411+
want: "json",
412+
},
413+
{
414+
name: "no value",
415+
flagValue: "",
416+
configValue: "",
417+
want: "plaintext",
418+
},
419+
}
420+
for _, tc := range tests {
421+
t.Run(tc.name, func(t *testing.T) {
422+
t.Parallel()
423+
cmd := &cobra.Command{
424+
Use: "test-command",
425+
Run: func(_ *cobra.Command, _ []string) {},
426+
}
427+
var p string
428+
cmd.Flags().StringVar(&p, flag.Output, "", "")
429+
if tc.flagValue != "" {
430+
require.NoError(t, cmd.Flags().Set(flag.Output, tc.flagValue))
431+
}
432+
c := &configMock{}
433+
if tc.configValue != "" {
434+
c.out = tc.configValue
435+
}
436+
require.NoError(t, cmd.ExecuteContext(NewContext()))
437+
e := newEvent(withOutput(cmd, c))
438+
assert.Equal(t, tc.want, e.Properties["output"])
439+
})
440+
}
441+
}
442+
387443
type configMock struct {
388444
name string
389445
publicKey string
@@ -393,6 +449,7 @@ type configMock struct {
393449
url string
394450
project string
395451
org string
452+
out string
396453
}
397454

398455
var _ Authenticator = configMock{}
@@ -428,3 +485,7 @@ func (c configMock) PrivateAPIKey() string {
428485
func (c configMock) AccessToken() string {
429486
return c.accessToken
430487
}
488+
489+
func (c configMock) Output() string {
490+
return c.out
491+
}

internal/telemetry/tracker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func (t *tracker) defaultCommandOptions() []EventOpt {
9696
withService(config.Default()),
9797
withProjectID(t.cmd, config.Default()),
9898
withOrgID(t.cmd, config.Default()),
99+
withOutput(t.cmd, config.Default()),
99100
withTerminal(t.cmd),
100101
withInstaller(t.installer),
101102
withUserAgent(),

0 commit comments

Comments
 (0)