config

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config manages all configuration files for AgentSecrets.

This mirrors the Python SecretsCLI's config.py and parts of credentials.py.

File layout:

~/.agentsecrets/
├── config.json     # User email, workspace cache, selected workspace
└── token.json      # JWT access/refresh tokens

./.agentsecrets/
└── project.json    # Project binding for current directory

Note: Private key is stored in OS keychain (see pkg/keyring), not in files.

Index

Constants

This section is empty.

Variables

View Source
var HomeDirHook = os.UserHomeDir

HomeDirHook is used to determine the user's home directory. It can be overridden in tests to redirect config files.

View Source
var ValidEnvironments = []string{"development", "staging", "production"}

ValidEnvironments is the list of valid environment names.

Functions

func ClearProjectConfig

func ClearProjectConfig() error

ClearProjectConfig resets the local .agentsecrets/project.json file.

func ClearSession

func ClearSession() error

ClearSession removes all stored credentials (logout). Does NOT clear project.json.

func GetAccessToken

func GetAccessToken() string

GetAccessToken returns the current access token, or empty string.

func GetEmail

func GetEmail() string

GetEmail returns the stored user email, or empty string if not logged in.

func GetProjectRoot added in v1.1.2

func GetProjectRoot() (string, error)

GetProjectRoot walks up the directory tree from the current working directory. It returns the absolute or relative path to the nearest directory containing '.agentsecrets/project.json'. If none is found before reaching the filesystem root, it returns an empty string.

func GetProjectWorkspaceKey

func GetProjectWorkspaceKey() ([]byte, error)

GetProjectWorkspaceKey returns the workspace key for the current project directory.

func GetSelectedEnvironment added in v1.1.2

func GetSelectedEnvironment() string

GetSelectedEnvironment returns the globally selected environment.

func GetSelectedProjectID added in v1.1.2

func GetSelectedProjectID() string

GetSelectedProjectID returns the globally selected project ID.

func GetSelectedWorkspaceID

func GetSelectedWorkspaceID() string

GetSelectedWorkspaceID returns the selected workspace for new project creation.

func GetStorageMode

func GetStorageMode() int

GetStorageMode returns the configured storage mode (1: keychain, 2: env_file). Resolution order: 1. AGENTSECRETS_STORAGE_MODE environment variable 2. .agentsecrets/project.json storage_mode 3. ~/.agentsecrets/config.json default_storage_mode 4. Default to 1 (keychain)

func GetWorkspaceKey

func GetWorkspaceKey(workspaceID string) ([]byte, error)

GetWorkspaceKey returns the decrypted workspace key for a given workspace ID.

func GlobalConfigExists

func GlobalConfigExists() bool

GlobalConfigExists returns true if ~/.agentsecrets/config.json already exists.

func InitGlobalConfig

func InitGlobalConfig() error

InitGlobalConfig creates the ~/.agentsecrets/ directory and default config files.

func InitProjectConfig

func InitProjectConfig(storageMode int) error

InitProjectConfig creates .agentsecrets/project.json in the current directory.

func IsAuthenticated

func IsAuthenticated() bool

IsAuthenticated checks if the user has a valid session (token + email present).

func IsValidEnvironment added in v1.1.2

func IsValidEnvironment(env string) bool

IsValidEnvironment returns true if the given string is a valid environment name.

func ResolveEnvironment added in v1.1.2

func ResolveEnvironment() string

ResolveEnvironment returns the active environment for the current context. Resolution order: 1. AGENTSECRETS_ENV environment variable 2. .agentsecrets/project.json environment field (walk up from cwd) 3. ~/.agentsecrets/config.json selected_environment 4. "development" (hardcoded default)

func ResolveEnvironmentWithSource added in v1.1.2

func ResolveEnvironmentWithSource() (string, string)

ResolveEnvironmentWithSource returns the active environment and its source. Sources: "AGENTSECRETS_ENV", "project.json", "global config", "default"

func SaveGlobalConfig

func SaveGlobalConfig(config *GlobalConfig) error

SaveGlobalConfig writes ~/.agentsecrets/config.json

func SaveProjectConfig

func SaveProjectConfig(config *ProjectConfig) error

SaveProjectConfig writes .agentsecrets/project.json to the nearest project root.

func SaveTokens

func SaveTokens(tokens *TokenConfig) error

SaveTokens writes ~/.agentsecrets/token.json

func SetEmail

func SetEmail(email string) error

SetEmail stores the user's email in global config.

func SetProjectStorageMode added in v1.1.2

func SetProjectStorageMode(mode int) error

SetProjectStorageMode updates the storage mode in the local project config.

func SetSelectedEnvironment added in v1.1.2

func SetSelectedEnvironment(env string) error

SetSelectedEnvironment sets the globally selected environment.

func SetSelectedProjectID added in v1.1.2

func SetSelectedProjectID(id string) error

SetSelectedProjectID sets the globally selected project ID.

func SetSelectedWorkspaceID

func SetSelectedWorkspaceID(id string) error

SetSelectedWorkspaceID sets the selected workspace for new project creation.

func SetStorageMode

func SetStorageMode(mode int) error

SetStorageMode updates the default storage mode in the global config.

func StoreTokens

func StoreTokens(accessToken, refreshToken, expiresAt string) error

StoreTokens saves authentication tokens.

func StoreWorkspaceCache

func StoreWorkspaceCache(workspaces map[string]WorkspaceCacheEntry) error

StoreWorkspaceCache saves decrypted workspace keys to global config.

Types

type CheckResult added in v1.1.2

type CheckResult struct {
	NewVersionAvailable bool
	LatestVersion       string
	CurrentVersion      string
}

CheckResult represents the outcome of an update check

func CheckForUpdates added in v1.1.2

func CheckForUpdates(currentVersion string) (*CheckResult, error)

CheckForUpdates checks GitHub for a newer version of the CLI. It only performs the check if more than 24 hours have passed since the last one. Returns a CheckResult if a newer version is found, or nil if no check was performed or no update was found.

type GitHubRelease added in v1.1.2

type GitHubRelease struct {
	TagName string `json:"tag_name"`
}

GitHubRelease represents the structure of a GitHub release from the API

type GlobalConfig

type GlobalConfig struct {
	Email               string                         `json:"email,omitempty"`
	SelectedWorkspaceID string                         `json:"selected_workspace_id,omitempty"`
	SelectedProjectID   string                         `json:"selected_project_id,omitempty"`
	SelectedEnvironment string                         `json:"selected_environment,omitempty"` // "development", "staging", "production"
	Workspaces          map[string]WorkspaceCacheEntry `json:"workspaces,omitempty"`
	PasswordHash        string                         `json:"password_hash,omitempty"` // Added for local password verification
	DefaultStorageMode  int                            `json:"default_storage_mode"`    // 1 = keychain (default), 2 = env_file
	LastUpdateCheck     int64                          `json:"last_update_check,omitempty"`
	LatestVersion       string                         `json:"latest_version,omitempty"`
}

GlobalConfig represents ~/.agentsecrets/config.json

func LoadGlobalConfig

func LoadGlobalConfig() (*GlobalConfig, error)

LoadGlobalConfig reads ~/.agentsecrets/config.json

type Paths

type Paths struct {
	GlobalDir  string // ~/.agentsecrets/
	ConfigFile string // ~/.agentsecrets/config.json
	TokenFile  string // ~/.agentsecrets/token.json
}

Paths returns the standard config file paths

func GetPaths

func GetPaths() (*Paths, error)

GetPaths returns the standard config paths based on the user's home directory.

type ProjectConfig

type ProjectConfig struct {
	ProjectID     string `json:"project_id"`
	ProjectName   string `json:"project_name"`
	Description   string `json:"description"`
	Environment   string `json:"environment"` // "development", "staging", "production"
	WorkspaceID   string `json:"workspace_id"`
	WorkspaceName string `json:"workspace_name"`
	LastPull      string `json:"last_pull"`
	LastPush      string `json:"last_push"`
	StorageMode   int    `json:"storage_mode"`
}

ProjectConfig represents ./.agentsecrets/project.json

func LoadProjectConfig

func LoadProjectConfig() (*ProjectConfig, error)

LoadProjectConfig reads .agentsecrets/project.json from the nearest project root.

type TokenConfig

type TokenConfig struct {
	AccessToken  string `json:"access_token,omitempty"`
	RefreshToken string `json:"refresh_token,omitempty"`
	ExpiresAt    string `json:"expires_at,omitempty"`
}

TokenConfig represents ~/.agentsecrets/token.json

func LoadTokens

func LoadTokens() (*TokenConfig, error)

LoadTokens reads ~/.agentsecrets/token.json

type WorkspaceCacheEntry

type WorkspaceCacheEntry struct {
	Name string `json:"name"`
	Key  string `json:"key"`  // Base64-encoded decrypted workspace key
	Role string `json:"role"` // "owner", "admin", "member"
	Type string `json:"type"` // "personal", "shared"
}

WorkspaceCacheEntry is a cached workspace with its decrypted key

Jump to

Keyboard shortcuts

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