Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions docs/COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,30 @@ nylas dashboard apps apikeys create --expires 30 # Expire in 30 days

After creating a key, you choose: activate in CLI (recommended), copy to clipboard, or save to file.

### Structured output

`status`, `apps list|create|use`, `apps apikeys list|create` and
`orgs list|switch` honour `--json`, `--format yaml` and `--quiet`, so scripts
and AI agents can read the result instead of the text:

```bash
nylas dashboard status --json # {"logged_in": true, "organization_id": ..., "active_app": {...}}
nylas dashboard apps list --json # [] when there are none
nylas dashboard apps create --name "My App" --region us --secret-delivery file --json
nylas dashboard apps apikeys create --delivery activate --json
nylas dashboard apps apikeys create --delivery file -q # prints only the key ID
```

- A secret is never printed in any format. `apps create` and `apikeys create`
report how it was delivered (`method`, `delivered`, and `file` for a file),
never its value. A clipboard that is unavailable falls back to a file and
says so in `warning`.
- Signed out, `status --json` prints `{"logged_in": false, ...}` and exits 0,
like the text form.
- A structured run never prompts: pass `--delivery` / `--secret-delivery`,
the application ID to `apps use`, and `--org` to `orgs switch`.
- `apps list` partial-region warnings go to stderr, so stdout stays valid JSON.

### Domains

```bash
Expand Down
38 changes: 34 additions & 4 deletions internal/cli/dashboard/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ Use --region to filter to a specific region.`,
if !errors.As(err, &partialErr) || len(apps) == 0 {
return wrapDashboardError(err)
}
common.PrintWarning("%v", partialErr)
if structuredOutput(cmd) {
common.PrintWarningStderr("%v", partialErr)
} else {
common.PrintWarning("%v", partialErr)
}
}

if len(apps) == 0 {
if len(apps) == 0 && !structuredOutput(cmd) {
fmt.Println("No applications found.")
fmt.Println("\nCreate one with: nylas dashboard apps create --name MyApp --region us")
return nil
Expand Down Expand Up @@ -127,7 +131,8 @@ func newAppsCreateCmd() *cobra.Command {
if err := validateSecretDelivery(secretDelivery); err != nil {
return err
}
if !isInteractive() && secretDelivery == "" {
structured := structuredOutput(cmd)
if secretDelivery == "" && (structured || !isInteractive()) {
return dashboardError(
"client secret delivery requires an explicit choice in non-interactive runs",
"Pass --secret-delivery clipboard or --secret-delivery file",
Expand All @@ -152,6 +157,10 @@ func newAppsCreateCmd() *cobra.Command {
return wrapDashboardError(err)
}

if structured {
return writeAppCreated(cmd, app, secretDelivery)
}

_, _ = common.Green.Println("✓ Application created!")
fmt.Printf(" Application ID: %s\n", app.ApplicationID)
fmt.Printf(" Region: %s\n", app.Region)
Expand Down Expand Up @@ -208,7 +217,7 @@ When called without arguments, lists your applications and lets you pick one int

// If no app ID provided, show interactive selector
if appID == "" {
if !isInteractive() {
if structuredOutput(cmd) || !isInteractive() {
return dashboardError(
"application selection requires an interactive terminal",
"Pass the application ID and --region in non-interactive runs",
Expand All @@ -234,6 +243,9 @@ When called without arguments, lists your applications and lets you pick one int
return wrapDashboardError(err)
}

if structuredOutput(cmd) {
return common.GetOutputWriter(cmd).Write(activeAppReport{ApplicationID: appID, Region: region})
}
_, _ = common.Green.Printf("✓ Active app: %s (%s)\n", appID, region)
return nil
},
Expand All @@ -244,6 +256,24 @@ When called without arguments, lists your applications and lets you pick one int
return cmd
}

// writeAppCreated delivers a new application's client secret and reports the
// application without the secret's value.
func writeAppCreated(cmd *cobra.Command, app *domain.GatewayCreatedApplication, secretDelivery string) error {
report := appCreatedReport{
ApplicationID: app.ApplicationID,
Region: app.Region,
Environment: app.Environment,
}
if app.ClientSecret != "" {
delivered, err := deliverClientSecret(app.ClientSecret, secretDelivery)
if err != nil {
return err
}
report.ClientSecret = &delivered
}
return common.GetOutputWriter(cmd).Write(report)
}

// selectApp fetches apps and presents an interactive selector.
// Returns the selected app ID and region.
func selectApp(regionFilter string) (appID, region string, err error) {
Expand Down
4 changes: 3 additions & 1 deletion internal/cli/dashboard/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ func chooseAuthMethod(action string) (string, error) {
return common.Select(fmt.Sprintf("How would you like to %s?", action), opts)
}

func isInteractive() bool {
// isInteractive reports whether stdin is a terminal. A variable so tests can
// stand in a terminal and prove --json alone is what stops a prompt.
var isInteractive = func() bool {
return term.IsTerminal(int(os.Stdin.Fd()))
}

Expand Down
87 changes: 30 additions & 57 deletions internal/cli/dashboard/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Set an active app with: nylas dashboard apps use <app-id> --region <region>`,
return wrapDashboardError(err)
}

if len(keys) == 0 {
if len(keys) == 0 && !structuredOutput(cmd) {
createCmd := "nylas dashboard apps " +
"apikeys create --app " + appID +
" --region " + region
Expand Down Expand Up @@ -156,7 +156,8 @@ Set an active app with: nylas dashboard apps use <app-id> --region <region>`,
if err := validateAPIKeyDelivery(delivery); err != nil {
return err
}
if !isInteractive() && delivery == "" {
structured := structuredOutput(cmd)
if delivery == "" && (structured || !isInteractive()) {
return dashboardError(
"API key delivery requires an explicit choice in non-interactive runs",
"Pass --delivery activate, --delivery clipboard, or --delivery file",
Expand All @@ -180,6 +181,10 @@ Set an active app with: nylas dashboard apps use <app-id> --region <region>`,
return wrapDashboardError(err)
}

if structured {
return writeAPIKeyCreated(cmd, key, appID, region, delivery)
}

_, _ = common.Green.Println("✓ API key created")
fmt.Printf(" ID: %s\n", key.ID)
fmt.Printf(" Name: %s\n", key.Name)
Expand All @@ -197,6 +202,21 @@ Set an active app with: nylas dashboard apps use <app-id> --region <region>`,
return cmd
}

// writeAPIKeyCreated delivers a new key and reports it without its value.
func writeAPIKeyCreated(cmd *cobra.Command, key *domain.GatewayCreatedAPIKey, appID, region, delivery string) error {
report, err := deliverAPIKey(key.APIKey, appID, region, delivery)
if err != nil {
return err
}
return common.GetOutputWriter(cmd).Write(apiKeyCreatedReport{
ID: key.ID,
Name: key.Name,
ApplicationID: appID,
Region: region,
Delivery: report,
})
}

// handleAPIKeyDelivery prompts the user to choose how to handle the newly created key.
// The API key is never printed to stdout to prevent leaking it in terminal history or logs.
func handleAPIKeyDelivery(apiKey, appID, region, delivery string) error {
Expand All @@ -219,33 +239,11 @@ func handleAPIKeyDelivery(apiKey, appID, region, delivery string) error {
choice = selected
}

switch choice {
case "activate":
if err := activateAPIKey(apiKey, appID, region, ""); err != nil {
_, _ = common.Yellow.Printf(" Could not activate: %v\n", err)
return nil
}
_, _ = common.Green.Println("✓ API key activated — CLI is ready to use")
_, _ = common.Dim.Println(" Try: nylas auth status")

case "clipboard":
if err := common.CopyToClipboard(apiKey); err != nil {
_, _ = common.Yellow.Printf(" Clipboard unavailable: %v\n", err)
_, _ = common.Dim.Println(" Falling back to file save")
return saveSecretToFile(apiKey, "nylas-api-key.txt", "API key")
}
_, _ = common.Green.Println("✓ API key copied to clipboard")

case "file":
return saveSecretToFile(apiKey, "nylas-api-key.txt", "API key")

default:
return dashboardError(
"invalid API key delivery method",
"Use --delivery activate, --delivery clipboard, or --delivery file",
)
report, err := deliverAPIKey(apiKey, appID, region, choice)
if err != nil {
return err
}

printDelivery("API key", report)
return nil
}

Expand All @@ -270,25 +268,11 @@ func handleSecretDelivery(secret, label, delivery string) error {
choice = selected
}

switch choice {
case "clipboard":
if err := common.CopyToClipboard(secret); err != nil {
_, _ = common.Yellow.Printf(" Clipboard unavailable: %v\n", err)
_, _ = common.Dim.Println(" Falling back to file save")
return saveSecretToFile(secret, "nylas-client-secret.txt", label)
}
_, _ = common.Green.Printf("✓ %s copied to clipboard\n", label)

case "file":
return saveSecretToFile(secret, "nylas-client-secret.txt", label)

default:
return dashboardError(
"invalid secret delivery method",
"Use --secret-delivery clipboard or --secret-delivery file",
)
report, err := deliverClientSecret(secret, choice)
if err != nil {
return err
}

printDelivery(label, report)
return nil
}

Expand Down Expand Up @@ -316,17 +300,6 @@ func validateSecretDelivery(delivery string) error {
}
}

// saveSecretToFile writes a secret to a temp file with restrictive permissions.
func saveSecretToFile(secret, filename, label string) error {
keyFile, err := writeSecretTempFile(secret, filename)
if err != nil {
return wrapDashboardError(fmt.Errorf("failed to write file: %w", err))
}
_, _ = common.Green.Printf("✓ %s saved to: %s\n", label, keyFile)
_, _ = common.Dim.Println(" Read it, then delete the file")
return nil
}

func writeSecretTempFile(secret, filename string) (string, error) {
pattern := tempSecretPattern(filename)
file, err := os.CreateTemp("", pattern)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/dashboard/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newOrgsListCmd() *cobra.Command {
return wrapDashboardError(err)
}

if len(session.Relations) == 0 {
if len(session.Relations) == 0 && !structuredOutput(cmd) {
fmt.Println("No organizations found.")
return nil
}
Expand Down
Loading
Loading