Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentio #325

Merged
merged 45 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b4e1af9
get branch up to date with latest version on segment-integration bran…
Nov 1, 2019
4459988
refactored main.go and NewConfluentCommand to wrap a cobra command, m…
Nov 6, 2019
ab51135
fix import ordering
Nov 6, 2019
f32e076
clean up code
Nov 6, 2019
4be45a5
clean up
Nov 6, 2019
28dadd0
changed segment key
Nov 6, 2019
f2711c8
add reset anon id when token time out
Nov 8, 2019
b066d78
clean up code
Nov 13, 2019
20f81ad
move setuserinfo to flushing rather than beginning
Nov 14, 2019
48dea6c
change analytics client to an interface, and generated a mock, and pr…
Nov 20, 2019
df332e6
added test case to see anonymous id reset
Nov 21, 2019
8c2753b
Merge branch 'master' into segmentio
Nov 21, 2019
c07f50e
clean up code, make adjustments to allow session time out to be track…
Nov 22, 2019
12867c0
fixed some minor bugs
Nov 22, 2019
435535b
moved mocks to cli/mock directory; logout and print message everytime…
Nov 23, 2019
73957cc
Merge branch 'master' into segmentio
Nov 24, 2019
918c8eb
added prerun cover for context use call
Nov 24, 2019
afb46fa
Merge branch 'segmentio' of github.com:confluentinc/cli into segmentio
Nov 24, 2019
beec4cf
fixed bug with context prerunner, and add prerunner as argument for c…
Nov 24, 2019
ab6710f
change credential type flag name to be consistent; added a test for c…
Nov 24, 2019
39c5194
fixed error bug making sure client is close; switch to using Credenti…
Nov 25, 2019
3725934
clean up code
Nov 25, 2019
2bc03e4
clean up
Nov 25, 2019
8d20c42
change method name from Flush to Send because flushing happens at Cli…
Nov 25, 2019
6a3ab88
fixed bug that came from cobra help calls do not triggering preruns
Nov 26, 2019
11b6e9e
changed apikey properties key to api-key to be consistent with creden…
Nov 26, 2019
dba3771
Merge branch 'master' into segmentio
Nov 26, 2019
981e2cc
retrigger build
Nov 26, 2019
30e9401
Merge branch 'segmentio' of github.com:confluentinc/cli into segmentio
Nov 26, 2019
29d3335
fixed bindata
Nov 26, 2019
ecb8d82
redirected segment error message to debug log
Dec 3, 2019
2f557da
pull master branch and resolve conflicts
Dec 6, 2019
885f289
make use of isTest flag so that segment isn't bombarded with integrat…
Dec 6, 2019
d412062
minor cleanups, edit CatchHelpCalls params
Dec 10, 2019
ccfcec2
removed printing
Dec 10, 2019
aa0f582
fixed conflict
Dec 10, 2019
e4c2307
go mod and sum
Dec 10, 2019
4294933
fixed comment to retrigger test
Dec 10, 2019
80f26c6
turn off for confluent CLI, and fix error not flushed issue
Dec 11, 2019
3e48e9d
create exit function in main, inject dependency in command execute
Dec 11, 2019
06bf82b
fixed setArgs argument, and fixed analytics tests
Dec 11, 2019
1593e2e
fixed merge conflicts
Dec 11, 2019
9e115f1
fixed integration test problem
Dec 11, 2019
59308df
cleanedup, added prerruner test for expired token
Dec 12, 2019
8952019
fixed import ordering
Dec 12, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions cmd/confluent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@ import (
"os"
"strconv"

"github.com/jonboulle/clockwork"
segment "github.com/segmentio/analytics-go"
"github.com/spf13/viper"

"github.com/confluentinc/cli/internal/cmd"
"github.com/confluentinc/cli/internal/pkg/analytics"
pcmd "github.com/confluentinc/cli/internal/pkg/cmd"
"github.com/confluentinc/cli/internal/pkg/config"
"github.com/confluentinc/cli/internal/pkg/log"
"github.com/confluentinc/cli/internal/pkg/metric"
"github.com/confluentinc/cli/internal/pkg/test-integ"
cliVersion "github.com/confluentinc/cli/internal/pkg/version"
"github.com/confluentinc/cli/mock"
)

var (
// Injected from linker flags like `go build -ldflags "-X main.version=$VERSION" -X ...`
version = "v0.0.0"
commit = ""
date = ""
host = ""
cliName = "confluent"
version = "v0.0.0"
commit = ""
date = ""
host = ""
cliName = "confluent"
segmentKey = "KDsYPLPBNVB1IPJIN5oqrXnxQT9iKezo"
isTest = "false"
)

Expand Down Expand Up @@ -51,25 +56,48 @@ func main() {

version := cliVersion.NewVersion(cfg.CLIName, cfg.Name(), cfg.Support(), version, commit, date, host)

cli, err := cmd.NewConfluentCommand(cliName, cfg, version, logger)
var analyticsClient analytics.Client
if !isTest && cfg.CLIName == "ccloud" {
segmentClient, _ := segment.NewWithConfig(segmentKey, segment.Config{
Logger: analytics.NewLogger(logger),
})

analyticsClient = analytics.NewAnalyticsClient(cfg.CLIName, cfg, version.Version, segmentClient, clockwork.NewRealClock())
} else {
analyticsClient = mock.NewDummyAnalyticsMock()
}

cli, err := cmd.NewConfluentCommand(cliName, cfg, version, logger, analyticsClient)
if err != nil {
if cli == nil {
fmt.Fprintln(os.Stderr, err)
} else {
pcmd.ErrPrintln(cli, err)
pcmd.ErrPrintln(cli.Command, err)
}
if isTest {
test_integ.ExitCode = 1
} else {
os.Exit(1)
exit(1, analyticsClient, logger)
}
}
err = cli.Execute()
err = cli.Execute(os.Args[1:])
csreesan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
csreesan marked this conversation as resolved.
Show resolved Hide resolved
if isTest {
test_integ.ExitCode = 1
} else {
os.Exit(1)
exit(1, analyticsClient, logger)
}
}
exit(0, analyticsClient, logger)
}

func exit(exitCode int, analytics analytics.Client, logger *log.Logger) {
err := analytics.Close()
if err != nil {
logger.Debug(err)
}
if exitCode == 1 {
os.Exit(exitCode)
}
// no os.Exit(0) because it will shutdown integration test
}
9 changes: 5 additions & 4 deletions cmd/confluent/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/confluentinc/cli/internal/pkg/config"
"github.com/confluentinc/cli/internal/pkg/log"
cliVersion "github.com/confluentinc/cli/internal/pkg/version"
"github.com/confluentinc/cli/mock"
)

func TestAddCommands_ShownInHelpUsage_CCloud(t *testing.T) {
Expand All @@ -24,10 +25,10 @@ func TestAddCommands_ShownInHelpUsage_CCloud(t *testing.T) {

version := cliVersion.NewVersion("ccloud", "Confluent Cloud CLI", "https://confluent.cloud; support@confluent.io", "1.2.3", "abc1234", "01/23/45", "CI")

root, err := cmd.NewConfluentCommand("ccloud", cfg, version, logger)
root, err := cmd.NewConfluentCommand("ccloud", cfg, version, logger, mock.NewDummyAnalyticsMock())
req.NoError(err)

output, err := pcmd.ExecuteCommand(root, "help")
output, err := pcmd.ExecuteCommand(root.Command, "help")
req.NoError(err)
req.Contains(output, "kafka")
//Hidden: req.Contains(output, "ksql")
Expand All @@ -53,10 +54,10 @@ func TestAddCommands_ShownInHelpUsage_Confluent(t *testing.T) {

version := cliVersion.NewVersion("confluent", "Confluent CLI", "https://confluent.io; support@confluent.io", "1.2.3", "abc1234", "01/23/45", "CI")

root, err := cmd.NewConfluentCommand("confluent", cfg, version, logger)
root, err := cmd.NewConfluentCommand("confluent", cfg, version, logger, mock.NewDummyAnalyticsMock())
req.NoError(err)

output, err := pcmd.ExecuteCommand(root, "help")
output, err := pcmd.ExecuteCommand(root.Command, "help")
req.NoError(err)
req.NotContains(output, "kafka")
req.NotContains(output, "ksql")
Expand Down
7 changes: 4 additions & 3 deletions cmd/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/confluentinc/cli/internal/pkg/doc"
"github.com/confluentinc/cli/internal/pkg/log"
"github.com/confluentinc/cli/internal/pkg/version"
"github.com/confluentinc/cli/mock"
)

var (
Expand All @@ -20,11 +21,11 @@ var (
func main() {
emptyStr := func(filename string) string { return "" }
sphinxRef := func(name, ref string) string { return fmt.Sprintf(":ref:`%s`", ref) }
confluent, err := cmd.NewConfluentCommand(cliName, &config.Config{CLIName: cliName}, &version.Version{}, log.New())
confluent, err := cmd.NewConfluentCommand(cliName, &config.Config{CLIName: cliName}, &version.Version{}, log.New(), mock.NewDummyAnalyticsMock())
if err != nil {
panic(err)
}
err = doc.GenReSTTreeCustom(confluent, path.Join(".", "docs", cliName), emptyStr, sphinxRef)
err = doc.GenReSTTreeCustom(confluent.Command, path.Join(".", "docs", cliName), emptyStr, sphinxRef)
if err != nil {
panic(err)
}
Expand All @@ -39,7 +40,7 @@ The available |ccloud| CLI commands are documented here.

`
}
err = doc.GenReSTIndex(confluent, path.Join(".", "docs", cliName, "index.rst"), indexHeader, sphinxRef)
err = doc.GenReSTIndex(confluent.Command, path.Join(".", "docs", cliName, "index.rst"), indexHeader, sphinxRef)
if err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
linter "github.com/confluentinc/cli/internal/pkg/lint-cli"
"github.com/confluentinc/cli/internal/pkg/log"
"github.com/confluentinc/cli/internal/pkg/version"
"github.com/confluentinc/cli/mock"
)

var (
Expand Down Expand Up @@ -163,12 +164,12 @@ func main() {

var issues *multierror.Error
for _, cliName := range cliNames {
cli, err := cmd.NewConfluentCommand(cliName, &config.Config{CLIName: cliName}, &version.Version{Binary: cliName}, log.New())
cli, err := cmd.NewConfluentCommand(cliName, &config.Config{CLIName: cliName}, &version.Version{Binary: cliName}, log.New(), mock.NewDummyAnalyticsMock())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = l.Lint(cli)
err = l.Lint(cli.Command)
if err != nil {
issues = multierror.Append(issues, err)
}
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/Shopify/sarama v1.20.1
github.com/Shopify/toxiproxy v2.1.3+incompatible // indirect
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 // indirect
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da
Expand All @@ -33,7 +35,7 @@ require (
github.com/gliderlabs/ssh v0.1.4 // indirect
github.com/gobuffalo/flect v0.1.3
github.com/gogo/protobuf v1.2.1
github.com/golang/mock v1.3.1
github.com/golang/mock v1.2.0
github.com/golang/protobuf v1.3.2
github.com/golang/snappy v0.0.1 // indirect
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d // indirect
Expand All @@ -51,6 +53,7 @@ require (
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/havoc-io/gopass v0.0.0-20170602182606-9a121bec1ae7
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365
github.com/joeshaw/iso8601 v0.0.0-20140327141645-861d1ce636d0
github.com/jonboulle/clockwork v0.1.0
github.com/kevinburke/go-bindata v3.13.0+incompatible
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
Expand All @@ -66,6 +69,8 @@ require (
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/pkg/errors v0.8.1
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a // indirect
github.com/segmentio/analytics-go v3.1.0+incompatible
github.com/segmentio/backo-go v0.0.0-20160424052352-204274ad699c // indirect
github.com/shurcooL/go v0.0.0-20190704215121-7189cc372560 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spf13/afero v1.2.2 // indirect
Expand All @@ -75,7 +80,7 @@ require (
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.0
github.com/tidwall/pretty v0.0.0-20190325153808-1166b9ac2b65
github.com/travisjeffery/mocker v1.1.0
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -283,6 +285,8 @@ github.com/jdkato/prose v1.1.0 h1:LpvmDGwbKGTgdCH3a8VJL56sr7p/wOFPw/R4lM4PfFg=
github.com/jdkato/prose v1.1.0/go.mod h1:jkF0lkxaX5PFSlk9l4Gh9Y+T57TqUZziWT7uZbW5ADg=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Aowv3l/EQUlocDHW2Cp4G9WJVH7uyH8QFJE=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/joeshaw/iso8601 v0.0.0-20140327141645-861d1ce636d0 h1:wrf53Uj+TG07ntTSFTuWYyJdPPfaSbRHn7CZvlzAxq8=
github.com/joeshaw/iso8601 v0.0.0-20140327141645-861d1ce636d0/go.mod h1:sPXhe9b9ZqQe0SoSy9DSlN2zaifj5LoQs/zs8mCETOQ=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
Expand Down Expand Up @@ -402,6 +406,10 @@ github.com/sebdah/goldie v0.0.0-20180424091453-8784dd1ab561 h1:IY+sDBJR/wRtsxq+6
github.com/sebdah/goldie v0.0.0-20180424091453-8784dd1ab561/go.mod h1:lvjGftC8oe7XPtyrOidaMi0rp5B9+XY/ZRUynGnuaxQ=
github.com/securego/gosec v0.0.0-20191002120514-e680875ea14d h1:BzRvVq1EHuIjxpijCEKpAxzKUUMurOQ4sknehIATRh8=
github.com/securego/gosec v0.0.0-20191002120514-e680875ea14d/go.mod h1:w5+eXa0mYznDkHaMCXA4XYffjlH+cy1oyKbfzJXa2Do=
github.com/segmentio/analytics-go v3.1.0+incompatible h1:IyiOfUgQFVHvsykKKbdI7ZsH374uv3/DfZUo9+G0Z80=
github.com/segmentio/analytics-go v3.1.0+incompatible/go.mod h1:C7CYBtQWk4vRk2RyLu0qOcbHJ18E3F1HV2C/8JvKN48=
github.com/segmentio/backo-go v0.0.0-20160424052352-204274ad699c h1:rsRTAcCR5CeNLkvgBVSjQoDGRRt6kggsE6XYBqCv2KQ=
github.com/segmentio/backo-go v0.0.0-20160424052352-204274ad699c/go.mod h1:kJ9mm9YmoWSkk+oQ+5Cj8DEoRCX2JT6As4kEtIIOp1M=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
Expand Down Expand Up @@ -479,6 +487,8 @@ github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro
github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnWhgSqHD8=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
github.com/zclconf/go-cty v0.0.0-20180815031001-58bb2bc0302a h1:x70ZZ4caA8eY4abjpcCnf6uvIPY3cpgRFrXE47JF4Sc=
github.com/zclconf/go-cty v0.0.0-20180815031001-58bb2bc0302a/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
35 changes: 23 additions & 12 deletions internal/cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/confluentinc/ccloud-sdk-go"
orgv1 "github.com/confluentinc/ccloudapis/org/v1"

"github.com/confluentinc/cli/internal/pkg/analytics"
auth_server "github.com/confluentinc/cli/internal/pkg/auth-server"
pcmd "github.com/confluentinc/cli/internal/pkg/cmd"
"github.com/confluentinc/cli/internal/pkg/config"
Expand All @@ -27,10 +28,11 @@ import (
)

type commands struct {
Commands []*cobra.Command
config *config.Config
mdsClient *mds.APIClient
Logger *log.Logger
Commands []*cobra.Command
config *config.Config
mdsClient *mds.APIClient
Logger *log.Logger
analyticsClient analytics.Client
// @VisibleForTesting, defaults to the OS filesystem
certReader io.Reader
// for testing
Expand All @@ -40,21 +42,22 @@ type commands struct {
}

// New returns a list of auth-related Cobra commands.
func New(prerunner pcmd.PreRunner, config *config.Config, logger *log.Logger, mdsClient *mds.APIClient, userAgent string) []*cobra.Command {
func New(prerunner pcmd.PreRunner, config *config.Config, logger *log.Logger, mdsClient *mds.APIClient, userAgent string, analyticsClient analytics.Client) []*cobra.Command {
var defaultAnonHTTPClientFactory = func(baseURL string, logger *log.Logger) *ccloud.Client {
return ccloud.NewClient(&ccloud.Params{BaseURL: baseURL, HttpClient: ccloud.BaseClient, Logger: logger, UserAgent: userAgent})
}
var defaultJwtHTTPClientFactory = func(ctx context.Context, jwt string, baseURL string, logger *log.Logger) *ccloud.Client {
return ccloud.NewClientWithJWT(ctx, jwt, &ccloud.Params{BaseURL: baseURL, Logger: logger, UserAgent: userAgent})
}
return newCommands(prerunner, config, logger, mdsClient, pcmd.NewPrompt(os.Stdin),
defaultAnonHTTPClientFactory, defaultJwtHTTPClientFactory,
defaultAnonHTTPClientFactory, defaultJwtHTTPClientFactory, analyticsClient,
).Commands
}

func newCommands(prerunner pcmd.PreRunner, config *config.Config, log *log.Logger, mdsClient *mds.APIClient, prompt pcmd.Prompt,
anonHTTPClientFactory func(baseURL string, logger *log.Logger) *ccloud.Client,
jwtHTTPClientFactory func(ctx context.Context, authToken string, baseURL string, logger *log.Logger) *ccloud.Client,
analyticsClient analytics.Client,
) *commands {
cmd := &commands{
config: config,
Expand All @@ -63,6 +66,7 @@ func newCommands(prerunner pcmd.PreRunner, config *config.Config, log *log.Logge
prompt: prompt,
anonHTTPClientFactory: anonHTTPClientFactory,
jwtHTTPClientFactory: jwtHTTPClientFactory,
analyticsClient: analyticsClient,
}
cmd.init(prerunner)
return cmd
Expand All @@ -87,7 +91,7 @@ func (a *commands) init(prerunner pcmd.PreRunner) {
check(loginCmd.MarkFlagRequired("url")) // because https://confluent.cloud isn't an MDS endpoint
}
loginCmd.Flags().SortFlags = false
loginCmd.PersistentPreRunE = prerunner.Anonymous()
loginCmd.PersistentPreRunE = a.analyticsPreRunCover(analytics.Login, prerunner)
logoutCmd := &cobra.Command{
Use: "logout",
Short: fmt.Sprintf("Logout of %s.", a.config.APIName()),
Expand All @@ -96,7 +100,7 @@ func (a *commands) init(prerunner pcmd.PreRunner) {
RunE: a.logout,
Args: cobra.NoArgs,
}
logoutCmd.PersistentPreRunE = prerunner.Anonymous()
logoutCmd.PersistentPreRunE = a.analyticsPreRunCover(analytics.Logout, prerunner)
a.Commands = []*cobra.Command{loginCmd, logoutCmd}
}

Expand Down Expand Up @@ -267,11 +271,9 @@ func (a *commands) loginMDS(cmd *cobra.Command, args []string) error {
}

func (a *commands) logout(cmd *cobra.Command, args []string) error {
a.config.AuthToken = ""
a.config.Auth = nil
err := a.config.Save()
err := a.config.DeleteUserAuth()
if err != nil {
return errors.Wrap(err, "Unable to delete user auth")
return err
}
pcmd.Println(cmd, "You are now logged out")
return nil
Expand Down Expand Up @@ -358,6 +360,13 @@ func (a *commands) setContextAndAddContextIfAbsent(username string, caCertPath s
return nil
}

func (a *commands) analyticsPreRunCover(commandType analytics.CommandType, prerunner pcmd.PreRunner) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
a.analyticsClient.SetCommandType(commandType)
return prerunner.Anonymous()(cmd, args)
csreesan marked this conversation as resolved.
Show resolved Hide resolved
}
}

func SelfSignedCertClient(certReader io.Reader, logger *log.Logger) (*http.Client, error){
certPool, err := x509.SystemCertPool()
if err != nil {
Expand Down Expand Up @@ -398,3 +407,5 @@ func check(err error) {
panic(err)
}
}


4 changes: 2 additions & 2 deletions internal/cmd/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func Test_SelfSignedCerts(t *testing.T) {
cfg.Logger = log.New()
cfg.CLIName = "confluent"
prompt := prompt("cody@confluent.io", "iambatman")
cmds := newCommands(&cliMock.Commander{}, cfg, log.New(), mdsClient, prompt, nil, nil)
cmds := newCommands(&cliMock.Commander{}, cfg, log.New(), mdsClient, prompt, nil, nil, cliMock.NewDummyAnalyticsMock())

for _, c := range cmds.Commands {
c.PersistentFlags().CountP("verbose", "v", "Increase output verbosity")
Expand Down Expand Up @@ -304,7 +304,7 @@ func newAuthCommand(prompt pcmd.Prompt, auth *sdkMock.Auth, user *sdkMock.User,
},
}
}
commands := newCommands(&cliMock.Commander{}, cfg, log.New(), mdsClient, prompt, mockAnonHTTPClientFactory, mockJwtHTTPClientFactory)
commands := newCommands(&cliMock.Commander{}, cfg, log.New(), mdsClient, prompt, mockAnonHTTPClientFactory, mockJwtHTTPClientFactory, cliMock.NewDummyAnalyticsMock())
for _, c := range commands.Commands {
c.PersistentFlags().CountP("verbose", "v", "Increase output verbosity")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/cluster/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func NewScopedIdService(client *http.Client, userAgent string, logger *log.Logge
}

type Element struct {
Type string
ID string
Type string
ID string
}

var (
Expand Down
Loading