settings

package
v0.12.3 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package settings provides unified storage for lokit user settings, including authentication credentials.

All settings are stored in the XDG data directory:

$XDG_DATA_HOME/lokit/  (default: ~/.local/share/lokit/)

Files stored:

  • auth.json — Authentication credentials (OAuth tokens and API keys)

Auth.json format: The file is a JSON object keyed by provider ID, where each value is a discriminated union on the "type" field:

  • "oauth" — OAuth tokens (copilot, gemini, openai)
  • "api" — API keys (google, groq, opencode, openai, custom-openai)

File permissions are 0600 (owner read/write only).

Lookup order for API keys:

  1. --api-key flag (highest priority)
  2. Provider-specific environment variable (GOOGLE_API_KEY, GROQ_API_KEY, etc.)
  3. This credential store

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DataDir

func DataDir() (string, error)

DataDir returns the lokit data directory path. Default: ~/.local/share/lokit (or $XDG_DATA_HOME/lokit).

func EnvVarForProvider added in v0.4.0

func EnvVarForProvider(providerID string) string

EnvVarForProvider returns the standard environment variable name used to supply an API key for the given provider. Returns empty string for OAuth-only providers (copilot, gemini) and Ollama (no key needed).

Lookup order used by ResolveAPIKey:

  1. --api-key flag
  2. Provider-specific env var (e.g. GOOGLE_API_KEY)
  3. Stored credential from auth.json

func FilePath

func FilePath() string

FilePath returns the auth.json file path for display purposes.

func GetAPIKey

func GetAPIKey(providerID string) string

GetAPIKey retrieves the stored API key for a provider. Returns empty string if not found or not an API key entry.

func GetBaseURL

func GetBaseURL(providerID string) string

GetBaseURL retrieves the stored base URL for a provider. Returns empty string if not found.

func MaskKey

func MaskKey(key string) string

MaskKey returns a masked version of a key/token for display.

func Remove

func Remove(providerID string) error

Remove deletes credentials for a provider.

func RemoveAll

func RemoveAll() error

RemoveAll removes all stored credentials.

func ResolveAPIKey added in v0.4.0

func ResolveAPIKey(providerID, flagKey string) string

ResolveAPIKey returns the effective API key for a provider, following the standard priority order:

  1. flagKey — value from --api-key flag (if non-empty, returned as-is)
  2. Provider-specific env var (e.g. GOOGLE_API_KEY, GROQ_API_KEY)
  3. Stored credential from auth.json

func Save

func Save(store Store) error

Save writes the credential store to disk with 0600 permissions.

func Set

func Set(providerID string, info *Info) error

Set stores an auth entry for a provider (upsert).

func SetAPIKey

func SetAPIKey(providerID, key string) error

SetAPIKey stores an API key for a provider.

func SetAPIKeyWithBaseURL

func SetAPIKeyWithBaseURL(providerID, key, baseURL string) error

SetAPIKeyWithBaseURL stores an API key and base URL for custom-openai.

func SetOAuth

func SetOAuth(providerID, access, refresh string, expires int64, accountID string) error

SetOAuth stores OAuth credentials for a provider. The accountID parameter is optional (pass "" to preserve any existing value).

Types

type Info

type Info struct {
	// Type discriminator: "oauth" or "api"
	Type string `json:"type"`

	// OAuth fields (type == "oauth")
	Access  string `json:"access,omitempty"`
	Refresh string `json:"refresh,omitempty"`
	Expires int64  `json:"expires,omitempty"` // Unix timestamp (0 = no expiry)

	// Gemini-specific OAuth fields
	Email     string `json:"email,omitempty"`     // Google account email
	ProjectID string `json:"projectId,omitempty"` // Code Assist project ID

	// OpenAI-specific OAuth fields
	AccountID string `json:"accountId,omitempty"` // ChatGPT account or organization ID

	// API key fields (type == "api")
	Key string `json:"key,omitempty"`

	// Custom endpoint URL (openai/custom-openai)
	BaseURL string `json:"baseUrl,omitempty"`
}

Info is the discriminated union stored per provider in auth.json. Exactly matches OpenCode's Auth.Info shape.

func Get

func Get(providerID string) *Info

Get returns the auth entry for a provider, or nil if not found.

func GetOAuth

func GetOAuth(providerID string) *Info

GetOAuth returns OAuth credentials for a provider, or nil if not found.

func (*Info) IsAPI

func (i *Info) IsAPI() bool

IsAPI returns true if this is an API key entry.

func (*Info) IsOAuth

func (i *Info) IsOAuth() bool

IsOAuth returns true if this is an OAuth entry.

type Store

type Store map[string]*Info

Store holds all provider credentials, keyed by provider ID.

func Load

func Load() Store

Load reads the credential store from disk. Returns an empty store if the file doesn't exist or is invalid.

Jump to

Keyboard shortcuts

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