config

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthOriginOAuthLogin = "oauth_login" // also set on signup
	AuthOriginAPIKey     = "api_key"
	AuthOriginMCPImport  = "mcp_import"
)
View Source
const (
	TokenValid      = "valid"
	TokenNearExpiry = "near_expiry"
	TokenExpired    = "expired"
	TokenNoExpiry   = "no_expiry" // borrowed tokens carry no expiry we can read
)
View Source
const ProjectFileName = ".revenuecat.json"

ProjectFileName is a per-directory project binding, discovered by walking up from the working directory like git finding .git, so a repo can commit it.

Variables

This section is empty.

Functions

func DeleteProfile

func DeleteProfile(name string) error

DeleteProfile removes a profile file. If it was the active one, the .active pointer is cleared.

func Dir

func Dir() (string, error)

Dir is the CLI's config/data directory — the single home for profiles, state, and other per-account files regardless of the working directory.

func DirBinding

func DirBinding() (path, projectID string, ok bool)

DirBinding reports the nearest .revenuecat.json binding for the current working directory: its file path and the project_id it resolves to. ok is false when no readable, parseable binding applies (an unreadable one is ProjectBindingError's concern, not this). Callers use it to warn when a committed binding will shadow a profile default the user just set or cleared.

func ListProfiles

func ListProfiles() ([]string, error)

ListProfiles returns the names of every profile file in the config dir.

func LoadState

func LoadState(profile, name string, out any) error

LoadState reads a named state file into out. A missing file is not an error; out is left untouched.

func ProfileName

func ProfileName(p string) string

ProfileName resolves the active profile name. Precedence (highest first):

  1. explicit argument (e.g. --profile flag)
  2. RC_PROFILE env var
  3. .active pointer file in the config dir (written by `rc profiles use`)
  4. "default"

func Save

func Save(profile string, cfg *Config) error

func SaveState

func SaveState(profile, name string, v any) error

SaveState writes a named state file beside the profile files.

func SetActiveProfile

func SetActiveProfile(name string) error

SetActiveProfile writes the .active pointer file. Errors if the named profile doesn't exist on disk (avoids "active" pointing at nothing).

Types

type Config

type Config struct {
	// API key auth (original).
	APIKey string `json:"api_key,omitempty"`

	// OAuth auth. All fields are omitempty so old profiles remain valid.
	AccessToken    string    `json:"access_token,omitempty"`
	RefreshToken   string    `json:"refresh_token,omitempty"`
	TokenExpiresAt time.Time `json:"token_expires_at,omitempty"`
	TokenType      string    `json:"token_type,omitempty"` // "oauth" | "" (api_key)
	AccountEmail   string    `json:"account_email,omitempty"`
	AccountName    string    `json:"account_name,omitempty"`

	// AuthSource records how the stored credential was obtained, so status can
	// name a borrowed MCP-imported token instead of passing it off as a normal
	// OAuth login or stored key. Flag/env credentials aren't stored, so they're
	// resolved live and never written here. Empty on legacy profiles.
	AuthSource string `json:"auth_source,omitempty"` // AuthOrigin* constants

	ProjectID string `json:"project_id,omitempty"`
	BaseURL   string `json:"base_url,omitempty"`
	// contains filtered or unexported fields
}

func Load

func Load(profile string) (*Config, error)

func LoadStored

func LoadStored(profile string) (*Config, error)

Load reads a profile from disk, layering the per-directory project binding and env vars on top. The project ID resolves with this precedence (highest first): --project-id flag (applied by the caller) > RC_PROJECT_ID env > nearest .revenuecat.json in the directory tree > profile default. Missing files are not an error — they return a zero Config so first-run commands like `rc login` work cleanly. LoadStored reads a profile's config as written on disk, overlaying keyring secrets, without applying any per-invocation override (the per-directory binding, RC_* env vars, or --project-id). Use it to report a profile's own stored defaults, e.g. `profiles list`; use Load for the active resolved config a command should act on.

func (*Config) BearerToken

func (c *Config) BearerToken() string

BearerToken returns the auth credential for the Authorization: Bearer header.

func (*Config) CanAutoRefresh

func (c *Config) CanAutoRefresh() bool

CanAutoRefresh is false for borrowed MCP tokens, which carry no refresh token.

func (*Config) Credential

func (c *Config) Credential() (token string, source CredentialSource)

Credential resolves the active credential. Precedence (highest first): --api-key flag > RC_API_KEY env > OAuth login > stored key — so a CI job or a one-off `RC_API_KEY=... rc ...` always acts on the account it names, even on a machine with a logged-in profile.

func (*Config) CredentialDescription

func (c *Config) CredentialDescription() string

CredentialDescription refines Describe with stored provenance so an MCP-imported credential isn't named as an ordinary OAuth login or stored key.

func (*Config) DescribeSource

func (c *Config) DescribeSource(source CredentialSource) string

DescribeSource names a credential source, refining the stored credential's phrasing with its provenance (only flag/env overrides never carry one).

func (*Config) IsOAuth

func (c *Config) IsOAuth() bool

IsOAuth reports whether this profile holds OAuth credentials.

func (*Config) NeedsRefresh

func (c *Config) NeedsRefresh() bool

NeedsRefresh reports whether the OAuth access token is expired or within 5 minutes of expiry and a refresh token is available.

func (*Config) OverrideProjectID

func (c *Config) OverrideProjectID(id string)

OverrideProjectID applies a --project-id flag for this invocation only. Unlike UseProjectID it keeps the value out of the profile: it records provenance so Save reverts it, so an ordinary command that happens to persist config (an OAuth token refresh, say) never bakes a one-shot flag into the stored default. Login adopts an explicit --project-id as the new default with UseProjectID.

func (*Config) PresentCredentialSources

func (c *Config) PresentCredentialSources() []CredentialSource

PresentCredentialSources lists every available credential source, highest precedence first.

func (*Config) ProjectBindingError

func (c *Config) ProjectBindingError(flagSet bool) error

ProjectBindingError reports a nearest .revenuecat.json that exists but could not be read or parsed. It returns nil when a higher-precedence override wins anyway — RC_PROJECT_ID (env) or an explicit --project-id (flagSet) — since those bypass the tree binding entirely. Otherwise the malformed file would silently retarget another project, so callers surface this before running.

func (*Config) SetAPIKey

func (c *Config) SetAPIKey(key string)

SetAPIKey sets an explicit stored API key, clearing any flag or env override.

func (*Config) SetFlagAPIKey

func (c *Config) SetFlagAPIKey(key string)

func (*Config) SetOAuthTokens added in v0.1.2

func (c *Config) SetOAuthTokens(accessToken, refreshToken string, expiresAt time.Time)

SetOAuthTokens stores OAuth credentials, clearing any flag or env override — like SetAPIKey, an explicit login supersedes ambient credentials for the rest of the invocation, so the token just obtained (not RC_API_KEY) is what gets validated, reported, and saved.

func (*Config) StoredProjectID

func (c *Config) StoredProjectID() string

StoredProjectID returns the profile's on-disk project default, unwinding any per-invocation overlay (the per-directory binding or RC_PROJECT_ID) that Load layered on top of it.

func (*Config) TokenStatus

func (c *Config) TokenStatus() string

TokenStatus is only meaningful when IsOAuth(); the near-expiry window matches NeedsRefresh's 5 minutes.

func (*Config) UseProjectID

func (c *Config) UseProjectID(id string)

UseProjectID records a project the user chose explicitly (projects use, projects create --use, the picker). It clears the env/dir project overrides so Save persists the choice even when it equals the injected override value — otherwise the revert would mistake the deliberate write for an untouched override.

type CredentialSource

type CredentialSource string

CredentialSource names where the active credential came from.

const (
	SourceFlag    CredentialSource = "flag"
	SourceOAuth   CredentialSource = "oauth"
	SourceEnv     CredentialSource = "env"
	SourceProfile CredentialSource = "profile"
	SourceNone    CredentialSource = "none"
)

func (CredentialSource) Describe

func (s CredentialSource) Describe() string

Describe returns a human-readable phrase naming the source.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL