Skip to content

Commit

Permalink
CLI Unification: Remove stale confluent and ccloud references (#984)
Browse files Browse the repository at this point in the history
* remove CLIName from config

* remove further references to confluent and ccloud

* remove CLIName from PS1 prompt

* fix integration tests
  • Loading branch information
brianstrauch committed Sep 1, 2021
1 parent 44fb92b commit 62d489c
Show file tree
Hide file tree
Showing 43 changed files with 263 additions and 466 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/audit-log/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type MockCall struct {
}

func (suite *AuditConfigTestSuite) SetupSuite() {
suite.conf = v3.AuthenticatedConfluentConfigMock()
suite.conf = v3.AuthenticatedOnPremConfigMock()
}

func (suite *AuditConfigTestSuite) TearDownSuite() {
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewConfluentCommand(cfg *v3.Config, isTest bool, ver *pversion.Version) *co
disableUpdateCheck := cfg.DisableUpdates || cfg.DisableUpdateCheck
updateClient := update.NewClient(pversion.CLIName, disableUpdateCheck, logger)

analyticsClient := getAnalyticsClient(isTest, pversion.CLIName, cfg, ver.Version, logger)
analyticsClient := getAnalyticsClient(isTest, cfg, ver.Version, logger)
authTokenHandler := pauth.NewAuthTokenHandler(logger)
ccloudClientFactory := pauth.NewCCloudClientFactory(ver.UserAgent, logger)
flagResolver := &pcmd.FlagResolverImpl{Prompt: form.NewPrompt(os.Stdin), Out: os.Stdout}
Expand Down Expand Up @@ -156,14 +156,14 @@ func NewConfluentCommand(cfg *v3.Config, isTest bool, ver *pversion.Version) *co
return &command{Command: cli, Analytics: analyticsClient, logger: logger}
}

func getAnalyticsClient(isTest bool, cliName string, cfg *v3.Config, cliVersion string, logger *log.Logger) analytics.Client {
if cliName == "confluent" || isTest {
func getAnalyticsClient(isTest bool, cfg *v3.Config, cliVersion string, logger *log.Logger) analytics.Client {
if cfg.IsOnPremLogin() || isTest {
return mock.NewDummyAnalyticsMock()
}
segmentClient, _ := segment.NewWithConfig(keys.SegmentKey, segment.Config{
Logger: analytics.NewLogger(logger),
})
return analytics.NewAnalyticsClient(cliName, cfg, cliVersion, segmentClient, clockwork.NewRealClock())
return analytics.NewAnalyticsClient(cfg, cliVersion, segmentClient, clockwork.NewRealClock())
}

func isAPIKeyCredential(cfg *v3.Config) bool {
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/iam/command_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ type ACLTestSuite struct {
}

func (suite *ACLTestSuite) SetupSuite() {
suite.conf = v3.AuthenticatedConfluentConfigMock()
suite.conf.CLIName = "confluent"
suite.conf = v3.AuthenticatedOnPremConfigMock()
}

func (suite *ACLTestSuite) newMockIamCmd(expect chan interface{}, message string) *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/kafka/command_topic_onprem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (suite *KafkaTopicOnPremTestSuite) createCommand() *cobra.Command {
return *suite.replicaList, nil, nil
},
}
conf = v3.AuthenticatedConfluentConfigMock()
conf = v3.AuthenticatedOnPremConfigMock()
provider := suite.getRestProvider()
testPrerunner := cliMock.NewPreRunnerMock(nil, nil, &provider, conf)
return NewTopicCommand(conf, false, testPrerunner, nil, "").Command
Expand Down
14 changes: 3 additions & 11 deletions internal/cmd/login/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,7 @@ func Test_SelfSignedCerts(t *testing.T) {
if tt.setEnv {
os.Setenv(pauth.ConfluentCACertPathEnvVar, "testcert.pem")
}
cfg := v3.New(&config.Params{
CLIName: "confluent",
MetricSink: nil,
Logger: log.New(),
})
cfg := v3.New(&config.Params{Logger: log.New()})
var expectedCaCert string
if tt.setEnv {
expectedCaCert = tt.envCertPath
Expand Down Expand Up @@ -555,7 +551,7 @@ func Test_SelfSignedCertsLegacyContexts(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
legacyContextName := "login-prompt-user@confluent.io-http://localhost:8090"
cfg := v3.AuthenticatedConfigMockWithContextName("confluent", legacyContextName)
cfg := v3.AuthenticatedConfigMockWithContextName(legacyContextName)
cfg.Contexts[legacyContextName].Platform.CaCertPath = originalCaCertPath

loginCmd := getNewLoginCommandForSelfSignedCertTest(req, cfg, tt.expectedCaCertPath)
Expand Down Expand Up @@ -784,11 +780,7 @@ func TestValidateUrl(t *testing.T) {

func newLoginCmd(auth *sdkMock.Auth, user *sdkMock.User, cliName string, req *require.Assertions, netrcHandler netrc.NetrcHandler,
authTokenHandler pauth.AuthTokenHandler, loginCredentialsManager pauth.LoginCredentialsManager) (*Command, *v3.Config) {
cfg := v3.New(&config.Params{
CLIName: cliName,
MetricSink: nil,
Logger: nil,
})
cfg := v3.New(new(config.Params))
var mdsClient *mds.APIClient
if cliName == "confluent" {
mdsConfig := mds.NewConfiguration()
Expand Down
6 changes: 1 addition & 5 deletions internal/cmd/logout/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ func TestRemoveNetrcCredentials(t *testing.T) {

func newLoginCmd(auth *sdkMock.Auth, user *sdkMock.User, cliName string, req *require.Assertions, netrcHandler netrc.NetrcHandler,
authTokenHandler pauth.AuthTokenHandler, loginCredentialsManager pauth.LoginCredentialsManager) (*login.Command, *v3.Config) {
cfg := v3.New(&config.Params{
CLIName: cliName,
MetricSink: nil,
Logger: nil,
})
cfg := v3.New(new(config.Params))
var mdsClient *mds.APIClient
if cliName == "confluent" {
mdsConfig := mds.NewConfiguration()
Expand Down
14 changes: 8 additions & 6 deletions internal/cmd/prompt/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,24 @@ func New(cfg *v3.Config, prerunner pcmd.PreRunner, ps1 *ps1.Prompt, logger *log.

func (c *promptCommand) init(cfg *v3.Config, prerunner pcmd.PreRunner) {
promptCmd := &cobra.Command{
Use: "prompt",
Short: fmt.Sprintf("Print %s context for your terminal prompt.", version.FullCLIName),
Long: parseTemplate(longDescriptionTemplate, version.CLIName),
Args: cobra.NoArgs,
RunE: pcmd.NewCLIRunE(c.prompt),
Annotations: map[string]string{pcmd.RunRequirement: pcmd.RequireNonAPIKeyCloudLogin},
Use: "prompt",
Short: fmt.Sprintf("Print %s context for your terminal prompt.", version.FullCLIName),
Long: parseTemplate(longDescriptionTemplate, version.CLIName),
Args: cobra.NoArgs,
RunE: pcmd.NewCLIRunE(c.prompt),
}

// Ideally we'd default to %c but contexts are implicit today with uber-verbose names like `login-cody@confluent.io-https://devel.cpdev.cloud`
defaultFormat := `({{color "blue" "confluent"}}|{{color "red" "%E"}}:{{color "cyan" "%K"}})`
if cfg.IsOnPremLogin() {
defaultFormat = `({{color "blue" "confluent"}}|{{color "cyan" "%K"}})`
}

promptCmd.Flags().StringP("format", "f", defaultFormat, "The format string to use. See the help for details.")
promptCmd.Flags().BoolP("no-color", "g", false, "Do not include ANSI color codes in the output.")
promptCmd.Flags().StringP("timeout", "t", "200ms", "The maximum execution time in milliseconds.")
promptCmd.Flags().SortFlags = false

c.CLICommand = pcmd.NewAnonymousCLICommand(promptCmd, prerunner)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/utils/analytics_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewTestAnalyticsClient(config *v3.Config, out *[]segment.Message) analytics
},
CloseFunc: func() error { return nil },
}
return analytics.NewAnalyticsClient(config.CLIName, config, "1.1.1.1.1", mockSegmentClient, clockwork.NewFakeClockAt(testTime))
return analytics.NewAnalyticsClient(config, "1.1.1.1.1", mockSegmentClient, clockwork.NewFakeClockAt(testTime))
}

func GetPagePropertyValue(segmentMsg segment.Message, key string) (interface{}, error) {
Expand Down
15 changes: 6 additions & 9 deletions internal/pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ type cmdPage struct {
commandType CommandType
}
type ClientObj struct {
cliName string
client segment.Client
config *v3.Config
clock clockwork.Clock
client segment.Client
config *v3.Config
clock clockwork.Clock

// cache data until we flush events to segment (when each cmd call finishes)
cmdPages []*cmdPage
Expand All @@ -111,9 +110,8 @@ type userInfo struct {
anonymousId string
}

func NewAnalyticsClient(cliName string, cfg *v3.Config, version string, segmentClient segment.Client, clock clockwork.Clock) *ClientObj {
func NewAnalyticsClient(cfg *v3.Config, version string, segmentClient segment.Client, clock clockwork.Clock) *ClientObj {
client := &ClientObj{
cliName: cliName,
config: cfg,
client: segmentClient,
cliVersion: version,
Expand Down Expand Up @@ -264,7 +262,6 @@ func (a *ClientObj) identify() error {
}
traits := segment.Traits{}
traits.Set(VersionPropertiesKey, a.activeCmd.cliVersion)
traits.Set(CliNameTraitsKey, a.cliName)
traits.Set(CredentialPropertiesKey, a.activeCmd.user.credentialType)
if a.activeCmd.user.credentialType == v2.APIKey.String() {
traits.Set(ApiKeyPropertiesKey, a.activeCmd.user.apiKey)
Expand Down Expand Up @@ -322,7 +319,7 @@ func (a *ClientObj) addArgsProperties(cmd *cobra.Command, args []string) {

func (a *ClientObj) addUserProperties() {
a.activeCmd.properties.Set(CredentialPropertiesKey, a.activeCmd.user.credentialType)
if a.cliName == "ccloud" && a.activeCmd.user.credentialType == v2.Username.String() {
if a.config.IsCloudLogin() && a.activeCmd.user.credentialType == v2.Username.String() {
a.activeCmd.properties.Set(OrgIdPropertiesKey, a.activeCmd.user.organizationId)
a.activeCmd.properties.Set(EmailPropertiesKey, a.activeCmd.user.email)
}
Expand All @@ -348,7 +345,7 @@ func (a *ClientObj) getUser() userInfo {
if user.credentialType == v2.APIKey.String() {
user.apiKey = a.getCredApiKey()
}
if a.cliName == "ccloud" {
if a.config.IsCloudLogin() {
userId, organizationId, email := a.getCloudUserInfo()
user.id = userId
user.organizationId = organizationId
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/cmd/prerunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func Test_UpdateToken(t *testing.T) {
if tt.isCloud {
cfg = v3.AuthenticatedCloudConfigMock()
} else {
cfg = v3.AuthenticatedConfluentConfigMock()
cfg = v3.AuthenticatedOnPremConfigMock()
}

cfg.Context().State.AuthToken = tt.authToken
Expand Down Expand Up @@ -437,7 +437,7 @@ func TestPrerun_AutoLogin(t *testing.T) {
if tt.isCloud {
cfg = v3.AuthenticatedCloudConfigMock()
} else {
cfg = v3.AuthenticatedConfluentConfigMock()
cfg = v3.AuthenticatedOnPremConfigMock()
}
err := pauth.PersistLogoutToConfig(cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -559,7 +559,7 @@ func TestPrerun_AutoLoginNotTriggeredIfLoggedIn(t *testing.T) {
if tt.isCloud {
cfg = v3.AuthenticatedCloudConfigMock()
} else {
cfg = v3.AuthenticatedConfluentConfigMock()
cfg = v3.AuthenticatedOnPremConfigMock()
}
cfg.Context().State.AuthToken = validAuthToken

Expand Down Expand Up @@ -840,7 +840,7 @@ func TestHasAPIKeyCLICommand_AddCommand(t *testing.T) {
}

func TestInitializeOnPremKafkaRest(t *testing.T) {
cfg := v3.AuthenticatedConfluentConfigMock()
cfg := v3.AuthenticatedOnPremConfigMock()
cfg.Context().State.AuthToken = validAuthToken
r := getPreRunBase()
r.Config = cfg
Expand Down
4 changes: 0 additions & 4 deletions internal/pkg/cmd/run_requirements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

"github.com/confluentinc/cli/internal/pkg/config"
v2 "github.com/confluentinc/cli/internal/pkg/config/v2"
v3 "github.com/confluentinc/cli/internal/pkg/config/v3"
testserver "github.com/confluentinc/cli/test/test-server"
Expand All @@ -22,7 +21,6 @@ var (
}

apiKeyCloudCfg = &v3.Config{
BaseConfig: &config.BaseConfig{Params: &config.Params{CLIName: "ccloud"}}, // TODO: Remove CLIName
Contexts: map[string]*v3.Context{"cloud": {
PlatformName: testserver.TestCloudURL.String(),
Credential: &v2.Credential{CredentialType: v2.APIKey},
Expand All @@ -32,7 +30,6 @@ var (
}

nonAPIKeyCloudCfg = &v3.Config{
BaseConfig: &config.BaseConfig{Params: &config.Params{CLIName: "ccloud"}}, // TODO: Remove CLIName
Contexts: map[string]*v3.Context{"cloud": {
PlatformName: testserver.TestCloudURL.String(),
Credential: &v2.Credential{CredentialType: v2.Username},
Expand All @@ -42,7 +39,6 @@ var (
}

onPremCfg = &v3.Config{
BaseConfig: &config.BaseConfig{Params: &config.Params{CLIName: "confluent"}}, // TODO: Remove CLIName
Contexts: map[string]*v3.Context{"on-prem": {
Credential: new(v2.Credential),
PlatformName: "https://example.com",
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type Config interface {
}

type Params struct {
CLIName string `json:"-"`
MetricSink metric.Sink `json:"-"`
Logger *log.Logger `json:"-"`
}
9 changes: 6 additions & 3 deletions internal/pkg/config/migrations/v2-v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ func MigrateV2ToV3(cfgV2 *v2.Config) (*v3.Config, error) {
}
cfgV3.Contexts = contextsV3
_, _ = fmt.Fprintf(os.Stderr, "Migrated config from V2 to V3.\n")
if cfgV3.CLIName == "ccloud" {
_, _ = fmt.Fprintf(os.Stderr, "Active Kafka setting and Kafka cluster information are removed from username credential contexts.\n")
for _, ctx := range cfgV3.Contexts {
if ctx.IsCloud(false) {
_, _ = fmt.Fprintf(os.Stderr, "Active Kafka setting and Kafka cluster information are removed from cloud username credential contexts.\n")
break
}
}
return cfgV3, nil
}
Expand All @@ -55,7 +58,7 @@ func migrateContextV2ToV3(contextV2 *v2.Context, cfgV3 *v3.Config) *v3.Context {
}
kafka := contextV2.Kafka
kafkaClusters := contextV2.KafkaClusters
if cfgV3.CLIName == "ccloud" && contextV3.Credential.CredentialType == v2.Username {
if contextV3.IsCloud(false) && contextV3.Credential.CredentialType == v2.Username {
kafka = ""
kafkaClusters = map[string]*v1.KafkaClusterConfig{}
contextV3.Logger.Debugf("Removing active Kafka setting and Kafka cluster information from context %s as part of config migration from V2 to V3.\n", contextV3.Name)
Expand Down
41 changes: 10 additions & 31 deletions internal/pkg/config/v0/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (

"github.com/confluentinc/cli/internal/pkg/config"
"github.com/confluentinc/cli/internal/pkg/errors"
pversion "github.com/confluentinc/cli/internal/pkg/version"
)

const (
defaultConfigFileFmt = "%s/.%s/config.json"
)
const defaultConfigFileFmt = "%s/.confluent/config.json"

var (
Version = semver.MustParse("0.0.0")
Expand All @@ -33,19 +32,13 @@ type Config struct {
CurrentContext string `json:"current_context" hcl:"current_context"`
}

// NewBaseConfig initializes a new Config object
func New(params *config.Params) *Config {
c := &Config{}
baseCfg := config.NewBaseConfig(params, Version)
c.BaseConfig = baseCfg
if c.CLIName == "" {
// HACK: this is a workaround while we're building multiple binaries off one codebase
c.CLIName = "confluent"
}
c.Platforms = map[string]*Platform{}
c.Credentials = map[string]*Credential{}
c.Contexts = map[string]*Context{}
return c
return &Config{
BaseConfig: config.NewBaseConfig(params, Version),
Platforms: make(map[string]*Platform),
Credentials: make(map[string]*Credential),
Contexts: make(map[string]*Context),
}
}

// Load reads the CLI config from disk.
Expand Down Expand Up @@ -100,21 +93,7 @@ func (c *Config) Validate() error {

// Binary returns the display name for the CLI
func (c *Config) Name() string {
name := "Confluent CLI"
if c.CLIName == "ccloud" {
name = "Confluent Cloud CLI"
}
return name
}

// APIName returns the display name of the remote API
// (e.g., Confluent Platform or Confluent Cloud)
func (c *Config) APIName() string {
name := "Confluent Platform"
if c.CLIName == "ccloud" {
name = "Confluent Cloud"
}
return name
return pversion.FullCLIName
}

// Context returns the current Context object.
Expand Down Expand Up @@ -186,7 +165,7 @@ func (c *Config) CheckHasAPIKey(clusterID string) error {
func (c *Config) getFilename() (string, error) {
if c.Filename == "" {
homedir, _ := os.UserHomeDir()
c.Filename = filepath.FromSlash(fmt.Sprintf(defaultConfigFileFmt, homedir, c.CLIName))
c.Filename = filepath.FromSlash(fmt.Sprintf(defaultConfigFileFmt, homedir))
}
return c.Filename, nil
}
Loading

0 comments on commit 62d489c

Please sign in to comment.