Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions .surface
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ hey --ids-only
hey --jq
hey --json
hey --markdown
hey --profile
hey --quiet
hey --stats
hey --styled
Expand Down Expand Up @@ -291,6 +292,15 @@ hey mcp --domains
hey mcp --read-only
hey move
hey move --to
hey profile
hey profile create
hey profile create --account
hey profile create --base-url
hey profile create --no-browser
hey profile delete
hey profile list
hey profile set-default
hey profile show
hey reply
hey reply --attach
hey reply --bcc
Expand Down
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ The setup wizard, the Omarchy bar plugin and the skill refresh hook are in

Configuration (the base URL, the default linked account, the Imbox's cover, and the `onboarded` flag) is stored in `~/.config/hey-cli/config.json`; `onboarded` is read from the global file only, never from a repository's `.hey/config.json`. Credentials are stored in the system keyring (service name: `hey`) with automatic fallback to `~/.config/hey-cli/credentials.json` when the keyring is unavailable. Set `HEY_NO_KEYRING=1` to force file storage.

Named profiles (`profiles` and `default_profile` in the global config, chosen by `-P`/`--profile`, `HEY_PROFILE` or `default_profile`) are ported from basecamp-cli and live in `internal/config/profiles.go` and `internal/cmd/profile*.go`. A credential is keyed by its origin with no profile active, and `profile:<name>` (`mcp:profile:<name>` for `hey mcp`) with one. With no profiles configured nothing reads differently; keep it that way. A repository's `.hey/config.json` sets the profile keys only once `hey config trust-local` trusts them.
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved

### CLI

The README is the front door, not the manual: install, first run, a dozen examples, and
Expand Down
60 changes: 58 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ hey --account 12345 boxes # override the default for one invocation
HEY_ACCOUNT_ID=12345 hey search "quarterly planning"
```

The default is `all`. Selection precedence is `--account`, `HEY_ACCOUNT_ID`, trusted local
`.hey/config.json`, the global default for the active server, then All Accounts. Global
The default is `all`. Selection precedence is `--account`, `HEY_ACCOUNT_ID`, the active
profile's account (see [Profiles](#profiles)), trusted local `.hey/config.json`, the global
default for the active server, then All Accounts. Global
account defaults are stored separately for each server origin, so development and production
selections cannot affect one another. Explicit and persisted IDs are validated against the
signed-in identity before mail requests, so an unavailable account fails closed.
Expand All @@ -100,6 +101,61 @@ Compose and contact creation use an individually selected account; replies and f
the thread's account. Calendars, todos, habits, time tracking, and journal entries remain
identity-wide.

### Profiles

A profile is a name for a server, a mail account on it, and a sign-in of its own. Profiles let
one computer keep a second HEY account, or staging or a beta, beside the one you use every day.
With no profiles configured, nothing changes: hey uses the top-level configuration and the
sign-in `hey auth login` made.

```bash
hey profile create personal # sign in through the browser, then save it
hey profile create staging --base-url https://staging.example.com
hey profile create work --account 12345 # with a linked mail account
hey profile list # every profile, signed in or not
hey profile show staging # one profile's details
hey profile set-default personal # used when nothing else chooses
hey profile delete staging # revoke its sign-in and remove it
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
hey -P staging box view imbox # one command as a profile
HEY_PROFILE=staging hey search "quarterly planning"
```

A command picks its profile from `--profile` (`-P`), then `HEY_PROFILE`, then `default_profile`,
then the only profile when there is one. With several profiles and none of those, an interactive
terminal asks which; anywhere else the command exits 1 with "multiple profiles configured but
none selected" and the profiles' names. The first profile you create becomes the default.

Each profile's credential is stored under its own key (`profile:<name>`), and `hey mcp`'s under
`mcp:profile:<name>`, so no profile, and not the top-level configuration, reads another's.
`hey auth login -P <name>` signs a profile in again. A profile's response cache is its own too.

A profile's `base_url` and `account_id` replace the top-level ones. `HEY_ACCOUNT_ID` and
`--account` still win over the account for one invocation, but a profile's credential goes only
to its own server: `HEY_BASE_URL` or `--base-url` naming another one is refused. A profile
that names no account uses All Accounts rather than the top-level account default, which belongs
to the top-level sign-in; `hey account use` under a profile saves the account on the profile.

Profiles live in the global config:

```json
{
"default_profile": "personal",
"profiles": {
"personal": { "base_url": "https://app.hey.com" },
"staging": { "base_url": "https://staging.example.com", "account_id": "12345" }
}
}
```

A repository's `.hey/config.json` may set `profiles` and `default_profile` too, but they choose
where a credential is sent, so hey ignores them, with a warning, until you run
`hey config trust-local` from that directory, and again after any change to them. A trusted
repository may add profiles, but an entry that would move a global profile to another server is
ignored, with a warning. `hey profile delete` revokes a sign-in only at the server that issued it.

JSON answers carry `meta.profile` while a profile is active. `hey auth status`, `hey config show`
and `hey doctor` name it as well.

## Output formats

Structured data commands support `--json` for full output and `--jq '<expression>'` to
Expand Down
51 changes: 49 additions & 2 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ const callbackListenAddr = "127.0.0.1:0"
type Manager struct {
baseURL string
// storeKey is where the credential lives in the store: the origin for the CLI's
// own, "mcp::" and the origin for hey mcp's (mcp.go).
// own, "mcp::" and the origin for hey mcp's (mcp.go), and under a profile
// "profile:<name>" and "mcp:profile:<name>" (UseProfile).
storeKey string
// mcp marks hey mcp's manager, whose credential is kept apart from the CLI's.
mcp bool
// profile is the profile this manager's credential belongs to, "" for none.
profile string
// clientID is the OAuth client this manager signs in and refreshes as.
clientID string
// scope is what a sign-in asks for. Empty asks for none, which HEY grants the
Expand Down Expand Up @@ -742,11 +747,53 @@ func (m *Manager) GetStore() *Store {
}

// CredentialKey returns the credential's storage key: the base URL for the CLI's own
// credential, prefixed for hey mcp's (mcp.go).
// credential, prefixed for hey mcp's (mcp.go), or the profile's (UseProfile).
func (m *Manager) CredentialKey() string {
return m.storeKey
}

// ProfileCredentialKey is the key a named profile's credential is stored under,
// basecamp-cli's "profile:<name>" (internal/auth/auth.go at 48b01aaf).
func ProfileCredentialKey(profile string) string { return "profile:" + profile }

// MCPProfileCredentialKey is the key of hey mcp's credential under a profile, kept
// apart from the profile's own as "mcp::<origin>" is from the origin's.
func MCPProfileCredentialKey(profile string) string { return "mcp:" + ProfileCredentialKey(profile) }

// UseProfile keys this manager's credential by profile rather than by origin, so two
// profiles on one server hold two sign-ins and neither reads the other's.
func (m *Manager) UseProfile(profile string) {
m.profile = profile
if m.mcp {
m.storeKey = MCPProfileCredentialKey(profile)
} else {
m.storeKey = ProfileCredentialKey(profile)
}
}

// LoginCommand is the command that signs this manager's credential in again:
// addressed to its profile when there is one, since a bare login would store the new
// credential under the top-level key instead. It is meant to be pasted, so the name is
// shell-quoted.
func (m *Manager) LoginCommand() string {
command := "hey auth login"
if m.mcp {
command += " --mcp"
}
if m.profile != "" {
command += " -P " + shellQuote(m.profile)
}
return command
}

// shellQuote quotes a word for a POSIX shell when it needs it.
func shellQuote(word string) string {
if word != "" && strings.Trim(word, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.") == "" {
return word
}
return "'" + strings.ReplaceAll(word, "'", `'\''`) + "'"
}

func (m *Manager) waitForCallback(ctx context.Context, expectedState, authURL string, listener net.Listener, opts LoginOptions) (string, error) {
codeCh := make(chan string, 1)
errCh := make(chan error, 1)
Expand Down
1 change: 1 addition & 0 deletions internal/auth/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewMCPManager(baseURL string, httpClient *http.Client, configDir, clientID
origin = canonical
}
m.storeKey = mcpStoreKeyPrefix + origin
m.mcp = true
Comment thread
jeremy marked this conversation as resolved.
Resolved
m.clientID = clientID
m.scope = MCPScope
m.honorEnvToken = false
Expand Down
12 changes: 12 additions & 0 deletions internal/cmd/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func unavailableAccountNotice(accounts []accountListItem, selected string) strin
}

func accountOverrideNotice(source config.Source) string {
if cfg != nil && cfg.ActiveProfile != "" {
// Saved on the profile, which outranks a repository's account_id.
saved := fmt.Sprintf("Saved profile %q's default", cfg.ActiveProfile)
switch source {
case config.SourceEnv:
return saved + "; HEY_ACCOUNT_ID remains higher precedence."
case config.SourceFlag:
return saved + "; --account controls only this invocation."
default:
return ""
}
}
switch source {
case config.SourceLocal:
return "Saved the global default; local .hey/config.json remains higher precedence."
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/basecamp/hey-cli/internal/apierr"
"github.com/basecamp/hey-cli/internal/auth"
"github.com/basecamp/hey-cli/internal/output"
"github.com/basecamp/hey-cli/internal/terminal"
)

type authCommand struct {
Expand Down Expand Up @@ -173,6 +175,9 @@ func newAuthStatusCommand() *cobra.Command {
"authenticated": false,
"mcp": mcpStatus,
}
if cfg.ActiveProfile != "" {
status["profile"] = cfg.ActiveProfile
}

// The install identifier is install-scoped: it survives logout and
// is sent on every OAuth login and refresh. Surface it in every
Expand All @@ -190,6 +195,7 @@ func newAuthStatusCommand() *cobra.Command {

if writer.IsStyled() {
w := cmd.OutOrStdout()
printProfileLine(w)
fmt.Fprintf(w, "Base URL: %s\n", cfg.BaseURL)
fmt.Fprintf(w, "Mail: %s (%s)\n", cfg.AccountID, cfg.SourceOf("account_id"))
if installID != "" {
Expand All @@ -216,6 +222,7 @@ func newAuthStatusCommand() *cobra.Command {
if errors.Is(err, auth.ErrCredentialsNotFound) || (creds.AccessToken == "" && creds.RefreshToken == "") {
if writer.IsStyled() {
w := cmd.OutOrStdout()
printProfileLine(w)
fmt.Fprintf(w, "Base URL: %s\n", cfg.BaseURL)
fmt.Fprintf(w, "Mail: %s (%s)\n", cfg.AccountID, cfg.SourceOf("account_id"))
if installID != "" {
Expand Down Expand Up @@ -253,6 +260,7 @@ func newAuthStatusCommand() *cobra.Command {
}
if writer.IsStyled() {
w := cmd.OutOrStdout()
printProfileLine(w)
fmt.Fprintf(w, "Base URL: %s\n", cfg.BaseURL)
fmt.Fprintf(w, "Mail: %s (%s)\n", cfg.AccountID, cfg.SourceOf("account_id"))
fmt.Fprintln(w, "Status: Logged in")
Expand Down Expand Up @@ -296,6 +304,14 @@ func newAuthStatusCommand() *cobra.Command {
}
}

// printProfileLine names the active profile in a styled status, and prints nothing
// with none.
func printProfileLine(w io.Writer) {
if cfg.ActiveProfile != "" {
fmt.Fprintf(w, "Profile: %s\n", terminal.SanitizeLine(cfg.ActiveProfile))
}
}

// refresh subcommand

func newAuthRefreshCommand() *cobra.Command {
Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ func newConfigShowCommand() *cobra.Command {
"source": "global",
},
}
if cfg.ActiveProfile != "" {
entries = append(entries, map[string]string{
"key": "profile",
"value": cfg.ActiveProfile,
"source": string(cfg.SourceOf("profile")),
})
}
if local := cfg.LocalConfig(); local != nil {
trust := "untrusted"
if local.Trusted {
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ func runDoctorChecks(ctx context.Context, root *cobra.Command) []map[string]stri
})
}

// Profile, only where one is active: with none the checks read as they always did.
if cfg != nil && cfg.ActiveProfile != "" {
checks = append(checks, map[string]string{
"name": "Profile",
"status": "ok",
"message": fmt.Sprintf("%s (%s)", cfg.ActiveProfile, cfg.BaseURL),
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
})
}

// Credentials
if authMgr != nil {
store := authMgr.GetStore()
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var curatedCategories = []struct {
},
{
heading: "ACCOUNT & SYSTEM",
names: []string{"auth", "account", "config", "setup", "mcp", "shell-completion", "doctor", "upgrade", "version"},
names: []string{"auth", "account", "profile", "config", "setup", "mcp", "shell-completion", "doctor", "upgrade", "version"},
},
}

Expand Down Expand Up @@ -307,13 +307,14 @@ func renderCommandHelp(cmd *cobra.Command) {
}

// globalFlagReadingOrder is how the global flags read in root help: the
// account first, then the output shaping, then verbosity. It orders the
// account and the profile first, then the output shaping, then verbosity. It orders the
// flags rather than choosing them, so a flag registered without a place
// here still gets listed, at the end.
var globalFlagReadingOrder = []string{"account", "json", "jq", "markdown", "quiet", "ids-only", "count", "styled", "html", "stats", "base-url", "verbose"}
var globalFlagReadingOrder = []string{"account", "profile", "json", "jq", "markdown", "quiet", "ids-only", "count", "styled", "html", "stats", "base-url", "verbose"}

var rootFlagDescriptions = map[string]string{
"account": "Select a linked mail account",
"profile": "Use a named profile",
"json": "Output a JSON response envelope",
"jq": "Filter JSON with a jq expression",
"markdown": "Output Markdown",
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ CALENDAR & TASKS
ACCOUNT & SYSTEM
auth Sign in, sign out, and check login status
account List and select linked mail accounts
profile Manage named profiles
config View and change settings
setup Set up HEY for first use
mcp Serve HEY to MCP clients over stdio
Expand All @@ -174,6 +175,7 @@ HELP TOPICS

FLAGS
--account Select a linked mail account
-P, --profile Use a named profile
--json Output a JSON response envelope
--jq Filter JSON with a jq expression
--markdown Output Markdown
Expand Down
8 changes: 5 additions & 3 deletions internal/cmd/help_topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ CONNECTION & AUTHENTICATION
HEY_MCP_CLIENT_ID Override the HEY MCP (local) client id hey auth login --mcp signs in as.
HEY_BASE_URL Override the HEY server URL.
HEY_ACCOUNT_ID Select a linked mail account ID or all.
HEY_PROFILE Use a named profile, as -P does (see hey profile).
HEY_NO_KEYRING Store credentials in the config directory instead of a keyring.

INTERACTION & DIAGNOSTICS
Expand Down Expand Up @@ -89,9 +90,10 @@ ONE INVOCATION
SELECTION ORDER
1. --account
2. HEY_ACCOUNT_ID
3. A trusted repository .hey/config.json
4. The global default for the active server
5. All Accounts
3. The active profile's account_id
4. A trusted repository .hey/config.json
5. The global default for the active server, when no profile is active
6. All Accounts

Compose and contact creation use an individually selected account. Replies and forwards use the thread's account. Calendar, task, time tracking, and journal commands remain identity-wide.`,
},
Expand Down
19 changes: 19 additions & 0 deletions internal/cmd/local_config_trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,30 @@ func promptForLocalConfigTrust(cmd *cobra.Command, local *config.LocalConfig) (l
fmt.Fprintf(w, " Configuration: %s\n", terminal.SanitizeLine(local.Path))
fmt.Fprintf(w, " Local server: %s\n", localTrustDisplayValue(local.BaseURL))
fmt.Fprintf(w, " Local mail account: %s\n", localTrustDisplayValue(local.AccountID))
for i, name := range local.ProfileNames {
label := " Local profiles: "
if i > 0 {
label = " "
}
server := "(no base_url)"
if entry, ok := local.Profiles[name].(map[string]any); ok {
if baseURL, ok := entry["base_url"].(string); ok && baseURL != "" {
server = baseURL
}
}
fmt.Fprintf(w, "%s%s %s\n", label, terminal.SanitizeLine(name), terminal.SanitizeLine(server))
}
if local.DefaultProfile != "" {
fmt.Fprintf(w, " Local default: %s\n", terminal.SanitizeLine(local.DefaultProfile))
}
fmt.Fprintf(w, " Effective server: %s\n", terminal.SanitizeLine(cfg.BaseURL))
fmt.Fprintf(w, " Effective account: %s\n", terminal.SanitizeLine(cfg.AccountID))
fmt.Fprintln(w)
fmt.Fprintln(w, "Always trust approves the exact local values above for this effective server origin.")
fmt.Fprintln(w, "Commands run here may send credentials to that server and read or send mail using that account.")
if len(local.ProfileNames) > 0 {
fmt.Fprintln(w, "It also approves the local profiles, which choose the server a command here signs in to.")
}
fmt.Fprintln(w)
fmt.Fprintln(w, " [1] Use once")
fmt.Fprintln(w, " [2] Always trust these settings")
Expand Down
Loading
Loading