settings

package
v0.23.5 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const (
	BackupOn  = "on"
	BackupOff = "off"
)

Values of models.backup.

View Source
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.

View Source
const Kind = "settings"

Kind is the schema kind of settings.json.

Variables

View Source
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.

View Source
var ErrUnknownKey = errors.New("not a shared setting")

ErrUnknownKey: not a phase-1 key.

Keys lists the phase-1 keys.

Functions

func Effective

func Effective(fam Settings, key string, follow bool, own, appDefault string) string

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

func Endpoints(value string) []string

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

func ModelsBackup(s Settings) bool

ModelsBackup reports whether models.backup is on (default off).

func Normalize

func Normalize(key, value string) (string, error)

Normalize checks a value and returns it in the form written to the file: trimmed; endpoint URLs without trailing slashes; a cleaned folder path.

Types

type Follow

type Follow map[string]bool

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).

func (Follow) Of

func (f Follow) Of(key string) bool

Of reports whether key follows the family.

type General

type General struct {
	Language *string      `json:"language,omitempty"`
	Extra    family.Extra `json:"-"`
}

General is the "general" object.

func (General) MarshalJSON

func (g General) MarshalJSON() ([]byte, error)

func (*General) UnmarshalJSON

func (g *General) UnmarshalJSON(b []byte) error

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 (m Models) MarshalJSON() ([]byte, error)

func (*Models) UnmarshalJSON

func (m *Models) UnmarshalJSON(b []byte) error

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 (n Network) MarshalJSON() ([]byte, error)

func (*Network) UnmarshalJSON

func (n *Network) UnmarshalJSON(b []byte) error

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

func Read(d family.Dir) (s Settings, readOnly bool, err error)

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 Set

func Set(ctx context.Context, d family.Dir, bundleID, key, value string) (Settings, error)

Set writes one key (for an app whose flag for it is following).

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) Get

func (s Settings) Get(key string) (value string, ok bool)

Get returns a key's value; ok is false when it is unset.

func (Settings) MarshalJSON

func (s Settings) MarshalJSON() ([]byte, error)

func (*Settings) SetValue

func (s *Settings) SetValue(key, value string) error

SetValue sets a key in memory (Set writes the file). The value is checked (Validate) and normalised (Normalize).

func (*Settings) UnmarshalJSON

func (s *Settings) UnmarshalJSON(b []byte) error

func (*Settings) Unset

func (s *Settings) Unset(key string) error

Unset removes a key in memory.

Jump to

Keyboard shortcuts

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