config

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAPIKeyNotConfigured is the error returned when the loaded profile is missing the api_key property
	ErrAPIKeyNotConfigured = errors.New("you have not configured your API key yet")
	// ErrApplicationIDNotConfigured is the error returned when the loaded profile is missing the application_id property
	ErrApplicationIDNotConfigured = errors.New("you have not configured your Application ID yet")
	// ErrAPIKeyMissingFromKeychain is the error returned when the current application has no API key in the OS keychain
	ErrAPIKeyMissingFromKeychain = errors.New("no API key stored in your keychain for the current application")

	// ErrCrawlerAPIKeyNotConfigured is the error returned when the loaded profile is missing the crawler_api_key property
	ErrCrawlerAPIKeyNotConfigured = errors.New("you have not configured your Crawler API key yet")
	// ErrCrawlerUserIDNotConfigured is the error returned when the loaded profile is missing the crawler_user_id property
	ErrCrawlerUserIDNotConfigured = errors.New("you have not configured your Crawler user ID yet")
)
View Source
var DefaultSearchHosts string

DefaultSearchHosts can be set at build time via ldflags, e.g. -X github.com/algolia/cli/pkg/config.DefaultSearchHosts=host1,host2

Functions

func AdminAPIKey

func AdminAPIKey(input string) error

AdminAPIKey validates that a string looks like an Admin API key.

Types

type ApplicationState added in v1.12.0

type ApplicationState struct {
	APIKeyUUID    string   `toml:"api_key_uuid,omitempty"`
	Alias         string   `toml:"alias"`
	SearchHosts   []string `toml:"search_hosts,omitempty"`
	CrawlerUserID string   `toml:"crawler_user_id,omitempty"`
}

ApplicationState holds the non-secret, per-application data persisted in state.toml. Secrets (API keys) live in the OS keychain, not here.

type Config

type Config struct {
	ApplicationName string

	CurrentProfile Profile

	File      string
	StateFile string
	// contains filtered or unexported fields
}

Config handles all overall configuration for the CLI.

It must not be copied after InitConfig: it holds sync primitives (govet copylocks) and CurrentProfile holds a back-pointer to it. Pass it by pointer.

func (*Config) APIKeyUUID added in v1.12.0

func (c *Config) APIKeyUUID(appID string) (string, bool)

APIKeyUUID returns the CLI-managed API key UUID recorded in state.toml for the given application, and whether one is stored. A configured application with no UUID (a legacy setup) returns ("", false).

func (*Config) ActiveApplicationID added in v1.12.0

func (c *Config) ActiveApplicationID() string

ActiveApplicationID exposes the application resolved by the new model (env → flag → state.toml alias → current application). Empty when only the legacy config.toml could answer.

func (*Config) ApplicationIDByAlias added in v1.12.0

func (c *Config) ApplicationIDByAlias(alias string) (string, bool)

ApplicationIDByAlias returns the application ID carrying the given alias in state.toml, and whether one was found.

func (*Config) ApplicationIDExists

func (c *Config) ApplicationIDExists(appID string) (bool, string)

ApplicationIDExists check if an application ID exists in any profiles

func (*Config) ApplicationIDForProfile added in v1.8.0

func (c *Config) ApplicationIDForProfile(profileName string) (bool, string)

ApplicationIDForProfile returns the application ID for a given profile name.

func (*Config) ApplicationInState added in v1.12.0

func (c *Config) ApplicationInState(appID string) bool

ApplicationInState reports whether state.toml already holds an entry for the given application, i.e. the application has been configured under the new storage model.

func (*Config) ConfiguredProfiles

func (c *Config) ConfiguredProfiles() []*Profile

ConfiguredProfiles return the profiles in the configuration file

func (*Config) Default

func (c *Config) Default() *Profile

Default returns the default profile

func (*Config) GetConfigFolder

func (c *Config) GetConfigFolder(xdgPath string) string

GetConfigFolder retrieves the folder where the configuration file is stored It searches for the xdg environment path first and will secondarily place it in the home directory

func (*Config) InitConfig

func (c *Config) InitConfig()

InitConfig reads in profiles file and ENV variables if set.

func (*Config) Migrate added in v1.12.0

func (c *Config) Migrate() error

Migrate moves the legacy config.toml profiles into the new model (state.toml + OS keychain); config.toml is never modified. Keychain first, state.toml last (atomic): a failure leaves state.toml absent, so the migration retries on the next run.

func (*Config) Profile

func (c *Config) Profile() *Profile

Profile returns the current profile

func (*Config) ProfileExists

func (c *Config) ProfileExists(appName string) bool

ProfileExists check if a profile with the given name exists

func (*Config) ProfileNames

func (c *Config) ProfileNames() []string

ProfileNames returns the list of name of the configured profiles

func (*Config) RemoveProfile

func (c *Config) RemoveProfile(name string) error

RemoveProfile remove a profile from the configuration

func (*Config) SaveApplication added in v1.12.0

func (c *Config) SaveApplication(appID, alias, apiKeyUUID, apiKey string, setCurrent bool) error

SaveApplication persists an application's credentials in the new model. The keychain is written first so a failure never leaves state.toml pointing at a key that was not stored. Empty alias/apiKeyUUID preserve the values already in state.toml, and an existing crawler key is preserved.

Note: a command that already resolved its active application keeps that resolution for the rest of the command (per-command cache).

func (*Config) SetCrawlerAPIKey added in v1.12.0

func (c *Config) SetCrawlerAPIKey(appID, crawlerAPIKey string) error

SetCrawlerAPIKey stores the crawler API key in the keychain entry of the given application, preserving the search API key (load-modify-save).

func (*Config) SetCrawlerAuth added in v1.8.3

func (c *Config) SetCrawlerAuth(profile, crawlerUserID, crawlerAPIKey string) error

SetCrawlerAuth sets the config properties for crawler public api

func (*Config) SetDefaultProfile

func (c *Config) SetDefaultProfile(name string) error

SetDefaultProfile set the default profile

func (*Config) ShouldMigrate added in v1.12.0

func (c *Config) ShouldMigrate() bool

ShouldMigrate reports whether the one-time config.toml → state.toml + keychain migration still has to run: config.toml exists and state.toml (only written on success, so doubling as the "migrated" marker) does not.

The state.toml check comes first so an already-migrated machine — the steady state, hit on every command — settles in a single stat instead of also stat-ing config.toml.

func (*Config) StateFileExists added in v1.12.0

func (c *Config) StateFileExists() bool

StateFileExists reports whether state.toml exists on disk, i.e. the new storage model (state.toml + OS keychain) is already in use on this machine.

type IConfig

type IConfig interface {
	InitConfig()

	ConfiguredProfiles() []*Profile
	ProfileNames() []string

	ProfileExists(name string) bool
	RemoveProfile(name string) error
	SetDefaultProfile(name string) error

	ApplicationIDExists(appID string) (bool, string)
	ApplicationIDForProfile(profileName string) (bool, string)

	SetCrawlerAuth(profileName, crawlerUserID, crawlerAPIKey string) error

	// New model (state.toml + OS keychain).
	ActiveApplicationID() string
	APIKeyUUID(appID string) (string, bool)
	ApplicationInState(appID string) bool
	ApplicationIDByAlias(alias string) (string, bool)
	SaveApplication(appID, alias, apiKeyUUID, apiKey string, setCurrent bool) error
	SetCrawlerAPIKey(appID, crawlerAPIKey string) error
	StateFileExists() bool

	Profile() *Profile
	Default() *Profile
}

type Profile

type Profile struct {
	Name string

	ApplicationID string   `mapstructure:"application_id"`
	APIKey        string   `mapstructure:"api_key"`
	AdminAPIKey   string   `mapstructure:"admin_api_key"`
	SearchHosts   []string `mapstructure:"search_hosts"`

	Default bool `mapstructure:"default"`
	// contains filtered or unexported fields
}

func (*Profile) Add

func (p *Profile) Add() error

Add adds a profile to the configuration, preserving any existing profiles.

func (*Profile) GetAPIKey added in v1.6.0

func (p *Profile) GetAPIKey() (string, error)

func (*Profile) GetAdminAPIKey

func (p *Profile) GetAdminAPIKey() (string, error)

func (*Profile) GetApplicationID

func (p *Profile) GetApplicationID() (string, error)

func (*Profile) GetCrawlerAPIKey added in v1.4.0

func (p *Profile) GetCrawlerAPIKey() (string, error)

GetCrawlerAPIKey returns the Crawler API key.

func (*Profile) GetCrawlerUserID added in v1.4.0

func (p *Profile) GetCrawlerUserID() (string, error)

GetCrawlerUserID returns the Crawler user ID.

func (*Profile) GetFieldName

func (p *Profile) GetFieldName(field string) string

func (*Profile) GetSearchHosts added in v1.3.5

func (p *Profile) GetSearchHosts() []string

func (*Profile) LoadDefault

func (p *Profile) LoadDefault()

type State added in v1.12.0

type State struct {
	CurrentApplicationID string                      `toml:"current_application_id"`
	Applications         map[string]ApplicationState `toml:"applications"`
}

State is the in-memory representation of state.toml, the new source of truth for non-secret CLI configuration.

func LoadState added in v1.12.0

func LoadState(path string) (*State, error)

LoadState reads state.toml from path. A missing file is not an error: it returns an empty, ready-to-use State.

func (*State) ApplicationByAlias added in v1.12.0

func (s *State) ApplicationByAlias(alias string) (string, bool)

ApplicationByAlias returns the application ID whose entry carries the given alias, and whether such an entry was found.

func (*State) Save added in v1.12.0

func (s *State) Save(path string) error

Save writes the State to path atomically: it encodes to a temporary file in the same directory, then renames it over the target so a crash mid-write can never leave a half-written state.toml.

func (*State) SetCurrentApplication added in v1.12.0

func (s *State) SetCurrentApplication(appID string)

SetCurrentApplication records appID as the currently selected application.

func (*State) UpsertApplication added in v1.12.0

func (s *State) UpsertApplication(appID string, app ApplicationState)

UpsertApplication inserts or replaces the state for a given application ID.

Jump to

Keyboard shortcuts

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