Skip to content

Commit

Permalink
chore(cli): UX Improvements (#3251)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoscar authored Oct 12, 2023
1 parent c36ddb6 commit feee677
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
1 change: 0 additions & 1 deletion agent/workers/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func NewPollerWorker(client *client.Client, opts ...PollerOption) *PollerWorker
}

func (w *PollerWorker) Poll(ctx context.Context, request *proto.PollingRequest) error {
fmt.Println("Poll handled by agent")
datastoreConfig, err := convertProtoToDataStore(request.Datastore)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion agent/workers/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func NewTriggerWorker(client *client.Client, opts ...TriggerOption) *TriggerWork
}

func (w *TriggerWorker) Trigger(ctx context.Context, triggerRequest *proto.TriggerRequest) error {
fmt.Println("Trigger handled by agent")
triggerConfig := convertProtoToTrigger(triggerRequest.Trigger)
triggerer, err := w.registry.Get(triggerConfig.Type)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/start_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var startCmd = &cobra.Command{
Use: "start",
Short: "Start Tracetest",
Long: "Start using Tracetest",
PreRun: setupCommand(SkipVersionMismatchCheck()),
PreRun: setupCommand(SkipConfigValidation()),
Run: WithResultHandler((func(_ *cobra.Command, _ []string) (string, error) {
ctx := context.Background()

Expand Down
6 changes: 6 additions & 0 deletions cli/config/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func (c Configurator) Start(ctx context.Context, prev Config, flags ConfigFlags)
return nil
}

confirmed := c.ui.Enter("Lets get to it! Press enter to launch a browser and authenticate:")
if !confirmed {
c.ui.Finish()
return nil
}

oauthServer := oauth.NewOAuthServer(fmt.Sprintf("%s%s", cfg.URL(), cfg.Path()), cfg.UIEndpoint)
err = oauthServer.WithOnSuccess(c.onOAuthSuccess(ctx, cfg)).
WithOnFailure(c.onOAuthFailure).
Expand Down
6 changes: 4 additions & 2 deletions cli/config/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func (c Configurator) organizationSelector(ctx context.Context, cfg Config) (str
}

if len(elements) == 1 {
c.ui.Println(fmt.Sprintf("Defaulting to only available Organization: %s", elements[0].Name))
c.ui.Println(fmt.Sprintf(`
Defaulting to only available Organization: %s`, elements[0].Name))
return elements[0].ID, nil
}

Expand All @@ -43,7 +44,8 @@ func (c Configurator) organizationSelector(ctx context.Context, cfg Config) (str
}
}

option := c.ui.Select("What Organization do you want to use?", options, 0)
option := c.ui.Select(`
What Organization do you want to use?`, options, 0)
option.Fn(c.ui)

return orgID, nil
Expand Down
39 changes: 20 additions & 19 deletions cli/pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func NewStarter(configurator config.Configurator, resources *resourcemanager.Reg

func (s *Starter) Run(ctx context.Context, cfg config.Config, flags config.ConfigFlags) error {
s.ui.Banner(config.Version)
s.ui.Println(`Tracetest start launches a lightweight agent. It enables you to run tests and collect traces with Tracetest.
Once started, Tracetest Agent exposes OTLP ports 4317 and 4318 to ingest traces via gRCP and HTTP.`)

return s.configurator.WithOnFinish(s.onStartAgent).Start(ctx, cfg, flags)
}
Expand All @@ -46,10 +48,6 @@ func (s *Starter) onStartAgent(ctx context.Context, cfg config.Config) {
s.ui.Error(err.Error())
}

s.ui.Info(fmt.Sprintf(`
Connecting Agent with name %s to Organization %s and Environment %s
`, "local", cfg.OrganizationID, env.Name))

err = s.StartAgent(ctx, cfg.AgentEndpoint, env.AgentApiKey, cfg.UIEndpoint)
if err != nil {
s.ui.Error(err.Error())
Expand Down Expand Up @@ -108,7 +106,8 @@ func (s *Starter) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpo
cfg.APIKey = agentApiKey
}

s.ui.Info(fmt.Sprintf("Starting Agent with name %s...", cfg.Name))
s.ui.Println(fmt.Sprintf(`
Starting Agent with name %s...`, cfg.Name))
session, err := initialization.Start(ctx, cfg)
if err != nil {
return err
Expand All @@ -120,21 +119,23 @@ func (s *Starter) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpo
}

isOpen := true
message := fmt.Sprintf("Agent is started! Leave the terminal open so tests can be run and traces gathered from this environment (%s). You can:", claims["environment_id"])
for isOpen {
options := []ui.Option{{
Text: "Open Tracetest in a browser to this environment",
Fn: func(_ ui.UI) {
s.ui.OpenBrowser(fmt.Sprintf("%sorganizations/%s/environments/%s/dashboard", uiEndpoint, claims["organization_id"], claims["environment_id"]))
},
}, {
Text: "Stop this agent",
Fn: func(_ ui.UI) {
isOpen = false
session.Close()
},
}}
message := `Agent is started! Leave the terminal open so tests can be run and traces gathered from this environment.
You can`
options := []ui.Option{{
Text: "Open Tracetest in a browser to this environment",
Fn: func(_ ui.UI) {
s.ui.OpenBrowser(fmt.Sprintf("%sorganizations/%s/environments/%s/dashboard", uiEndpoint, claims["organization_id"], claims["environment_id"]))
},
}, {
Text: "Stop this agent",
Fn: func(_ ui.UI) {
isOpen = false
session.Close()
s.ui.Finish()
},
}}

for isOpen {
selected := s.ui.Select(message, options, 0)
selected.Fn(s.ui)
}
Expand Down
20 changes: 20 additions & 0 deletions cli/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type UI interface {
Red(string) string

Confirm(prompt string, defaultValue bool) bool
Enter(msg string) bool
Select(prompt string, options []Option, defaultIndex int) (selected Option)
TextInput(msg, defaultValue string) string
}
Expand Down Expand Up @@ -120,6 +121,25 @@ func (ui ptermUI) Confirm(msg string, defaultValue bool) bool {
return confirm
}

func (ui ptermUI) Enter(msg string) bool {
confirm, err := (&pterm.InteractiveConfirmPrinter{
DefaultText: msg,
DefaultValue: true,
TextStyle: &pterm.ThemeDefault.DefaultText,
ConfirmText: "Enter",
RejectText: "Cancel",
RejectStyle: &pterm.ThemeDefault.ErrorMessageStyle,
ConfirmStyle: &pterm.ThemeDefault.SuccessMessageStyle,
SuffixStyle: &pterm.ThemeDefault.SecondaryStyle,
}).
Show()
if err != nil {
ui.Panic(err)
}

return confirm
}

func (ui ptermUI) TextInput(msg, defaultValue string) string {
text := msg
if defaultValue != "" {
Expand Down

0 comments on commit feee677

Please sign in to comment.