Documentation
¶
Overview ¶
Package settings is the family's shared general settings (spec §9): settings.json (interface language, proxy, Hugging Face endpoints, model folder), each app's "follow the family" flags, and the first-run migration that merges an app's existing values without overriding the user's choices.
fam, _, _ := settings.Read(d) lang := settings.Effective(fam, settings.KeyLanguage, follow[settings.KeyLanguage], own.Language, "") settings.Set(ctx, d, bundleID, settings.KeyProxy, "socks5://127.0.0.1:7897") // when following
Index ¶
- Constants
- Variables
- func Effective(fam Settings, key string, follow bool, own, appDefault string) string
- func Endpoints(value string) []string
- func ModelsBackup(s Settings) bool
- func Normalize(key, value string) (string, error)
- type Follow
- type General
- type Local
- type Models
- type Network
- type Outcome
- type Settings
Constants ¶
const ( KeyLanguage = "general.language" // BCP-47; "" = the system's KeyProxy = "network.proxy" // "" automatic, "direct", or a proxy URL KeyHFEndpoints = "network.hfEndpoints" // "auto", "official", "mirror" or URLs KeyModelsRoot = "models.root" // absolute path; "" = the default rules (spec §7.1) // KeyModelsBackup: whether Time Machine backs up the model library // ("on"; unset or "off" = excluded, spec §7.12). It is a property of // the shared folder, so it has no follow flag: every app applies the // family's value (models.ApplyBackup). KeyModelsBackup = "models.backup" )
Keys of the shared settings (spec §9.1). Phase 2 keys ("speech.mainLanguage", "speech.otherLanguages") are kept untouched when present.
const ( BackupOn = "on" BackupOff = "off" )
Values of models.backup.
const ( ProxyAuto = "" ProxyDirect = "direct" EndpointsAuto = "auto" EndpointsOfficial = "official" EndpointsMirror = "mirror" OfficialEndpoint = "https://huggingface.co" MirrorEndpoint = "https://hf-mirror.com" )
Values of network.proxy and network.hfEndpoints.
const Kind = "settings"
Kind is the schema kind of settings.json.
Variables ¶
var ( // TextOtherValue: the one-time prompt on a setting row after a // migration conflict (spec §9.4). {setting} is the row's name. TextOtherValue = family.Text{ EN: "Other ActNow apps use a different {setting}.", ZH: "其他 ActNow app 使用的{setting}不同。", } TextUseTheirs = family.Text{EN: "Use theirs", ZH: "改用相同设置"} )
Reference texts.
var ErrUnknownKey = errors.New("not a shared setting")
ErrUnknownKey: not a phase-1 key.
var Keys = []string{KeyLanguage, KeyProxy, KeyHFEndpoints, KeyModelsRoot}
Keys lists the phase-1 keys.
Functions ¶
func Effective ¶
Effective is a setting's value for an app (spec §9.3): follow ? (family's value if set, else appDefault) : own. Environment variables (HF_ENDPOINT) still override the result where the app reads them, as before.
func Endpoints ¶
Endpoints turns network.hfEndpoints into the endpoints to race: auto → official and mirror; official; mirror; or the listed URLs.
func ModelsBackup ¶ added in v0.8.0
ModelsBackup reports whether models.backup is on (default off).
Types ¶
type Follow ¶
Follow holds an app's follow flags, kept in the app's own settings (e.g. "family.follow.language"). A key that is missing follows: new installs follow everything (spec §9.3).
type General ¶
General is the "general" object.
func (General) MarshalJSON ¶
func (*General) UnmarshalJSON ¶
type Local ¶
type Local struct {
Value string
// IsDefault: the user never changed it in this app.
IsDefault bool
// InUse: the default is in real use and must not silently change —
// for models.root, the default folder holds this app's models.
InUse bool
}
Local is an app's own value of a shared key, for Migrate.
type Models ¶
type Models struct {
Root *string `json:"root,omitempty"`
Backup *string `json:"backup,omitempty"`
Extra family.Extra `json:"-"`
}
Models is the "models" object.
func (Models) MarshalJSON ¶
func (*Models) UnmarshalJSON ¶
type Network ¶
type Network struct {
Proxy *string `json:"proxy,omitempty"`
HFEndpoints *string `json:"hfEndpoints,omitempty"`
Extra family.Extra `json:"-"`
}
Network is the "network" object.
func (Network) MarshalJSON ¶
func (*Network) UnmarshalJSON ¶
type Outcome ¶
type Outcome struct {
Key string
Follow bool // the app's new follow flag
Wrote bool // the app's value became the family's
// Conflict: the family has another value and the app keeps its own;
// show TextOtherValue once on the setting's row (for models.root,
// TextMoveModels from the models package).
Conflict bool
Family string // the family's value, when set
}
Outcome is what Migrate decided for a key.
func Migrate ¶
func Migrate(ctx context.Context, d family.Dir, bundleID string, local map[string]Local) ([]Outcome, error)
Migrate merges an app's values into the family settings at the app's first start with the spec (spec §9.4, §15.2 steps 4–5):
- the family has no value: a value the user changed is written to the family (first come, first served), and the app follows;
- the family has the same value: the app follows;
- the family has another value: an app on its default (and not InUse) follows — a new install adopts the family's choice; otherwise the app keeps its own (Follow false) and shows the conflict once.
Keys missing from local are left alone. Idempotent.
type Settings ¶
type Settings struct {
Schema string `json:"schema"`
Rev int `json:"rev"`
Updated time.Time `json:"updated"`
UpdatedBy string `json:"updatedBy"`
General *General `json:"general,omitempty"`
Network *Network `json:"network,omitempty"`
Models *Models `json:"models,omitempty"`
Extra family.Extra `json:"-"`
}
Settings is settings.json. A member that is absent is unset (not the default): pointers tell the two apart.
func Read ¶
Read reads settings.json. readOnly is true when it has a newer major schema (show family.TextNewerFormat; do not write). A missing file is empty settings; a corrupt one is reported, and treated as empty by Update (which quarantines it).
func Update ¶
func Update(ctx context.Context, d family.Dir, bundleID string, fn func(s *Settings) error) (Settings, error)
Update is the read-modify-write of settings.json (spec §9.2): under locks/settings.lock, fn changes the settings, rev goes up by one, updated/updatedBy are set, the file is replaced atomically and TopicSettings posted. fn may return family.ErrNoChange.
func (Settings) MarshalJSON ¶
func (*Settings) SetValue ¶
SetValue sets a key in memory (Set writes the file). The value is checked (Validate) and normalised (Normalize).