Skip to content

Commit

Permalink
fix(cli): automatically use cloud endpoint if token is passed (#3671)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren committed Feb 21, 2024
1 parent dfa3a52 commit 53c7a25
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 0 additions & 1 deletion Makefile
Expand Up @@ -11,7 +11,6 @@ ifneq "$(CURRENT_GORELEASER_VERSION)" "$(GORELEASER_VERSION)"
@printf "\033[0;31m Bad goreleaser version $(CURRENT_GORELEASER_VERSION), please install $(GORELEASER_VERSION)\033[0m\n\n"
@printf "\033[0;31m Tracetest requires goreleaser pro installed (licence not necessary for local builds)\033[0m\n\n"
@printf "\033[0;33m See https://goreleaser.com/install/ \033[0m\n\n"
@exit 1
endif


Expand Down
4 changes: 4 additions & 0 deletions agent/config/flags.go
Expand Up @@ -18,3 +18,7 @@ type Flags struct {
LogLevel string
CollectorEndpoint string
}

func (f Flags) AutomatedEnvironmentCanBeInferred() bool {
return f.CI || f.AgentApiKey != "" || f.Token != ""
}
19 changes: 11 additions & 8 deletions cli/config/configurator.go
Expand Up @@ -59,10 +59,9 @@ func (c Configurator) Start(ctx context.Context, prev *Config, flags agentConfig
c.flags = &flags
var serverURL string

// if token is passed, we cannot assume an interactive environment,
// so fallback to last used
if c.flags.Token != "" {
serverURL = lastUsedURL(prev)
if c.flags.AutomatedEnvironmentCanBeInferred() {
// avoid prompts on automated or non-interactive environments
serverURL = c.lastUsedURL(prev)
} else {
var err error
serverURL, err = c.getServerURL(prev)
Expand Down Expand Up @@ -103,7 +102,11 @@ func (c Configurator) Start(ctx context.Context, prev *Config, flags agentConfig
return nil
}

func lastUsedURL(prev *Config) string {
func (c Configurator) lastUsedURL(prev *Config) string {
if c.flags.ServerURL != "" {
return c.flags.ServerURL
}

possibleValues := []string{}
if prev != nil {
possibleValues = append(possibleValues, prev.UIEndpoint, prev.URL())
Expand All @@ -118,7 +121,7 @@ func (c Configurator) getServerURL(prev *Config) (string, error) {

// if flag was passed, don't show prompt
if c.flags.ServerURL == "" {
serverURL = c.ui.TextInput("What tracetest server do you want to use?", lastUsedURL(prev))
serverURL = c.ui.TextInput("What tracetest server do you want to use?", c.lastUsedURL(prev))
}

if err := validateServerURL(serverURL); err != nil {
Expand Down Expand Up @@ -187,8 +190,8 @@ func (c Configurator) populateConfigWithDevConfig(ctx context.Context, cfg *Conf
}

func (c Configurator) populateConfigWithVersionInfo(ctx context.Context, cfg Config) (_ Config, _ error, isOSS bool) {
cliVersion := Version
if cliVersion == "dev" {
useDevVersion := os.Getenv("TRACETEST_AGENT_DEV_CONFIG") == "true"
if useDevVersion && Version == "dev" {
c.populateConfigWithDevConfig(ctx, &cfg)

c.ui.Success("Configured Tracetest CLI in development mode")
Expand Down

0 comments on commit 53c7a25

Please sign in to comment.