Documentation
¶
Overview ¶
Package config loads the per-pack JSON configs and the registry that lists them, and reads and writes the version a pack repo says it vendors.
The config directory is a mounted volume in production -- each pack's sync-upstream.yml bind-mounts the toolkit checkout's config/ at /app/config -- so this schema is an external interface, not an internal one. It is ported from the Pydantic models in core/config.py and must accept every file those models accept, unchanged.
Index ¶
- Constants
- Variables
- func CheckPrivateConfigDir() error
- func DefaultConfigDir() string
- func DefaultConfigSource() (string, string)
- func DefaultVersionKeys() []string
- func ExecutableDir() string
- func FindToolkitRoot(start string) string
- func Interpolate(template string, bindings map[string]string) string
- func IsSlug(s string) bool
- func PackFile(slug, configDir string) (string, error)
- func PackSchema() []byte
- func PrivateConfigDir() string
- func ReadVendoredVersion(pack *PackConfig) string
- func ResolveConfigSource(explicit string, getenv func(string) string, cwd, exeDir string) (string, string)
- func ResolveFile(configDir, name string) (string, error)
- func ResolvedCurrentVersion(pack *PackConfig) string
- func SetPrivateConfigDir(dir string) (restore func())
- func ValidatePackDocument(doc []byte) error
- func VendoredVersionFile(pack *PackConfig) string
- func WritePackFileAtomic(path string, data []byte) error
- func WriteVendoredVersion(pack *PackConfig, version string) (string, error)
- type PackConfig
- type PacksRegistry
- type SchemaError
- type SinkConfig
- type SourceConfig
Constants ¶
const ( SourceFlag = "flag" SourceEnv = "env" SourceCwd = "cwd" SourceExecutable = "executable" SourceEmbedded = "embedded" SourceMissing = "missing" )
Config directory sources, as the Settings page and the startup log name them.
const DefaultVersionFile = "resources/assets/svg/config.json"
DefaultVersionFile and DefaultVersionKeys mirror the Python defaults. No shipped config overrides either, so changing them changes every pack at once.
const SlugPattern = `^[a-z0-9]+(-[a-z0-9]+)*$`
SlugPattern is the one shape a pack name may take: lowercase letters and digits in words joined by single hyphens. A pack name is a file name, a URL path segment, a branch-name part and a key in run history, so it carries no dot, underscore, capital or space ("icon-sets-extras-ant-design", never "icon-sets-extras.ant-design"). config/schema/pack.schema.json carries the same pattern for editors; TestSchemaSlugPatternMatchesIsSlug keeps them one.
const SlugRule = "a pack name is a lowercase slug: letters a-z and digits, in words joined by single hyphens"
SlugRule is the rule in words, for messages.
Variables ¶
var SinkTypes = []string{"filesystem", "git-branch"}
SinkTypes is the closed set Python declared as a Literal.
var SourceTypes = []string{"npm", "github-archive", "github-tag", "github-release", "url", "script"}
SourceTypes is the closed set Python declared as a Literal. A config naming anything else is rejected at load time, before any network call.
Functions ¶
func CheckPrivateConfigDir ¶ added in v0.1.2
func CheckPrivateConfigDir() error
CheckPrivateConfigDir reports a private directory that is set but cannot be read as a directory. Nil when none is set.
func DefaultConfigDir ¶
func DefaultConfigDir() string
DefaultConfigDir resolves the directory the pack configs live in, from the process environment, working directory and executable. See ResolveConfigSource.
func DefaultConfigSource ¶ added in v0.1.3
DefaultConfigSource is DefaultConfigDir with the layer that answered.
func DefaultVersionKeys ¶
func DefaultVersionKeys() []string
DefaultVersionKeys is returned by value; callers must not retain the slice.
func ExecutableDir ¶ added in v0.1.3
func ExecutableDir() string
ExecutableDir is the directory of the running binary with symlinks resolved, so a symlink on PATH still finds the checkout it points into. "" when it cannot be told.
func FindToolkitRoot ¶ added in v0.1.3
FindToolkitRoot returns this repository's root: starting at start and climbing at most configWalkLimit parents, stopping after a directory holding .git, the first directory with config/packs.json next to src/go.mod. It returns "" when there is none.
func Interpolate ¶
Interpolate replaces {key} placeholders with the given bindings.
This is a replacement loop, not format-string semantics: an unknown token is left in the output verbatim rather than raising. Several templates in the shipped configs rely on that, carrying tokens a later stage substitutes.
func PackFile ¶
PackFile returns the path of a pack's config: <slug>.json, .yaml or .yml, from the private config directory when it has one (see private.go), else from configDir.
func PrivateConfigDir ¶ added in v0.1.2
func PrivateConfigDir() string
PrivateConfigDir is the private config directory in use, or "".
func ReadVendoredVersion ¶
func ReadVendoredVersion(pack *PackConfig) string
ReadVendoredVersion returns the version the pack repo says it vendors.
Returns "" when the file is missing, unreadable, unparseable, or declares none of the keys -- callers then fall back to the toolkit config's current_version. Swallowing the error is deliberate and matches Python: a pack that has not been synced yet is an ordinary state, not a failure.
func ResolveConfigSource ¶ added in v0.1.3
func ResolveConfigSource(explicit string, getenv func(string) string, cwd, exeDir string) (string, string)
ResolveConfigSource is the one place the config directory is decided, for every command and the web UI, and it names the layer that answered. It covers the layers a double-clicked binary needs too. Finder and Explorer start a binary with the working directory at $HOME, so the walk from cwd never reaches the checkout. In order:
- explicit (--config-dir): SourceFlag;
- ICHAVA_CONFIG_DIR: SourceEnv;
- config/ beside the executable: SourceExecutable;
- ./config, or the repository layout walking up from cwd: SourceCwd;
- the repository layout walking up from the executable's own directory, by the same rule (config/packs.json next to src/go.mod, at most configWalkLimit parents, stopping at .git), so build/macos/icon-sets-sync and build/macos/icon-sets-sync_macos_* find the checkout: SourceExecutable;
- the configs embedded in the binary, written to a read-only cache directory: SourceEmbedded;
- ./config, so a missing registry reports the path it looked for: SourceMissing (only if the embedded copy cannot be written).
func ResolveFile ¶ added in v0.1.2
ResolveFile returns where a file a recipe reads beside its pack config is: the private directory's copy when there is one, else the public path -- returned even when it does not exist, so the caller's error names it. The error is set only when name is refused.
name is a file in the directory and nothing else. One that could leave it -- a `..`, an absolute path, a link out -- is refused with pathguard.ErrEscape in every directory, so a slug from a request can probe neither the overlay nor the public directory.
func ResolvedCurrentVersion ¶
func ResolvedCurrentVersion(pack *PackConfig) string
ResolvedCurrentVersion is the version upstream is compared against.
The pack repo wins. The toolkit config's current_version is a fallback for a pack with no vendored record yet: it lives in *this* repository, so a bump written during a sync run is discarded with the runner. That asymmetry is the whole reason the sync converges (V49).
func SetPrivateConfigDir ¶ added in v0.1.2
func SetPrivateConfigDir(dir string) (restore func())
SetPrivateConfigDir sets the private config directory and returns a func restoring the previous one, for tests. An empty dir means none.
func ValidatePackDocument ¶
ValidatePackDocument checks a pack config, as JSON bytes, against the schema.
The error lists every violation, one per line, each with the JSON pointer of the offending value -- "/sinks/0/root" rather than "invalid config".
func VendoredVersionFile ¶
func VendoredVersionFile(pack *PackConfig) string
VendoredVersionFile is the pack repo's own version record.
func WritePackFileAtomic ¶ added in v0.1.1
WritePackFileAtomic validates data as a pack config for path and, only if it is valid, replaces the file atomically (see docfile.WriteAtomic). An invalid document never reaches the disk, so a reader can never load half a save or a save that would not load.
func WriteVendoredVersion ¶
func WriteVendoredVersion(pack *PackConfig, version string) (string, error)
WriteVendoredVersion records version at every declared key in the pack repo.
Returns the file written, or "" when the pack ships no such file. It only rewrites a leaf that already exists and already differs -- it never creates a key. That is what keeps it from inventing `package.upstream_version` in a pack that deliberately has none (icon-sets-bundled), and from touching `package.version`, which is the pack's own release number rather than the upstream one.
The edit is surgical rather than parse-modify-reserialise. Go maps have no key order, so round-tripping the document would reorder every key in it and bury a one-line version bump in a whole-file diff on the sync PR.
Types ¶
type PackConfig ¶
type PackConfig struct {
Name string `json:"name"`
Pack string `json:"pack"`
PackRoot string `json:"pack_root"`
CurrentVersion string `json:"current_version,omitempty"`
VersionFile string `json:"version_file,omitempty"`
VersionKeys []string `json:"version_keys,omitempty"`
VersionCheckURL string `json:"version_check_url"`
Source SourceConfig `json:"source"`
Sinks []SinkConfig `json:"sinks,omitempty"`
Transforms []map[string]any `json:"transforms,omitempty"`
// Recipe picks the pipeline builder; empty and "auto" dispatch by pack
// name, then by source type, exactly as before the field existed.
Recipe string `json:"recipe,omitempty"`
// Licence is the SPDX id the pack's upstream is expected to carry. When
// set, the licence-text sink refuses a sync whose upstream licence text
// no longer matches it, so a relicensing blocks the refresh instead of
// being copied into the pack unreviewed.
Licence string `json:"licence,omitempty"`
// LicenceSHA256 pins the upstream licence text to the byte: one of the
// upstream's licence files must have this digest, or the sync fails as a
// changed Licence does. For a licence with no standard text to recognise.
LicenceSHA256 string `json:"licence_sha256,omitempty"`
// LicenceSet names the directory under resources/assets/svg/licenses the
// upstream's licence texts are written to. Empty means one named after
// the upstream (the npm package, or the GitHub repository).
LicenceSet string `json:"licence_set,omitempty"`
// LicenceFrom is the upstream directory the licence files are read from,
// relative to its root. Empty means the root.
LicenceFrom string `json:"licence_from,omitempty"`
// LicenceMissing records, with the evidence, that the upstream ships no
// licence file at its root, so a sync may run and the gap is reported
// instead of failing it. The text is then supplied by hand in the pack,
// beside a source.md saying where it came from.
LicenceMissing string `json:"licence_missing,omitempty"`
// Settings is the pack layer of the settings precedence. It is kept as a
// raw tree here and resolved by internal/core/settings, which owns the
// key registry; the schema has already checked its shape.
Settings map[string]any `json:"settings,omitempty"`
// File is where the config was loaded from. Not part of the document.
File string `json:"-"`
// PackRootOrigin says where PackRoot came from (the pack config, the
// workspace setting, or a sibling checkout). Not part of the document.
PackRootOrigin string `json:"-"`
}
PackConfig is one config/<slug>.json.
Unlike the source and sink blocks this rejects unknown keys, so a typo fails loudly instead of being silently ignored. Python expressed that as `extra="forbid"`, which is also why the comment-key strip below has to happen before validation rather than after.
func LoadAll ¶
func LoadAll(configDir string) ([]*PackConfig, error)
LoadAll loads every pack in the registry, in registry order.
func LoadPack ¶
func LoadPack(slug, configDir string) (*PackConfig, error)
LoadPack reads and validates one pack config, in JSON or YAML.
func LoadPackFile ¶
func LoadPackFile(path string) (*PackConfig, error)
LoadPackFile reads, schema-validates and decodes one pack config.
Validation runs against the whole document before anything is decoded, so a file with three mistakes reports all three rather than the first one the decoder trips on. The strict decode after it is a second gate, and catches a schema that has drifted from the Go types.
func ParsePackBytes ¶ added in v0.1.1
func ParsePackBytes(path string, data []byte) (*PackConfig, error)
ParsePackBytes is LoadPackFile on bytes already in hand: it validates and decodes them exactly as if they had been read from path. The path decides the format (JSON, or YAML by extension), prefixes every error, and is recorded as the pack's File.
It exists for an editor, which has to validate the exact bytes it is about to write through the same loader that will later read them. Re-marshalling a decoded PackConfig instead would silently drop every `_note` annotation, because loading strips them.
type PacksRegistry ¶
type PacksRegistry struct {
Packs []string `json:"packs"`
}
PacksRegistry is config/packs.json.
func LoadRegistry ¶
func LoadRegistry(configDir string) (*PacksRegistry, error)
LoadRegistry reads and validates config/packs.json (or packs.yaml), merged with the private config directory's registry when one is set.
func ParseRegistryBytes ¶ added in v0.1.1
func ParseRegistryBytes(registryFile string, data []byte) (*PacksRegistry, error)
ParseRegistryBytes validates and decodes a registry document as if read from path. LoadRegistry reads the file and calls it; an editor calls it on the bytes it is about to save.
type SchemaError ¶
type SchemaError struct {
Problems []string
}
SchemaError carries every schema violation in a document.
func (*SchemaError) Error ¶
func (e *SchemaError) Error() string
type SinkConfig ¶
type SinkConfig struct {
Type string `json:"type"`
// filesystem
Root string `json:"root,omitempty"`
Incremental bool `json:"incremental,omitempty"`
MinRetention float64 `json:"min_retention,omitempty"`
// git-branch. Each of these is a pack-level value for the matching
// git.* setting: it wins over the default and gives way to the global
// config, the environment and flags.
RepoRoot string `json:"repo_root,omitempty"`
Branch string `json:"branch,omitempty"`
CommitSubject string `json:"commit_subject,omitempty"`
CommitBody string `json:"commit_body,omitempty"`
Base string `json:"base,omitempty"`
OpenPR *bool `json:"open_pr,omitempty"`
Extra map[string]any `json:"-"`
}
SinkConfig says where refreshed assets land.
func (SinkConfig) MarshalJSON ¶
func (s SinkConfig) MarshalJSON() ([]byte, error)
MarshalJSON writes the extras back alongside the known fields.
func (*SinkConfig) UnmarshalJSON ¶
func (s *SinkConfig) UnmarshalJSON(data []byte) error
type SourceConfig ¶
type SourceConfig struct {
Type string `json:"type"`
Package string `json:"package,omitempty"`
Owner string `json:"owner,omitempty"`
Repo string `json:"repo,omitempty"`
ArchiveURL string `json:"archive_url,omitempty"`
SourcePath string `json:"source_path,omitempty"`
Path string `json:"path,omitempty"`
Args []string `json:"args,omitempty"`
// MaxDownloadBytes raises the archive download cap for this one source,
// for an upstream whose tagged archive is larger than the default 1 GiB.
// Zero means the default. The schema bounds it at 4 GiB.
MaxDownloadBytes int64 `json:"max_download_bytes,omitempty"`
// Extra holds keys with no field above. See the type comment.
Extra map[string]any `json:"-"`
}
SourceConfig says where to fetch upstream artefacts from.
Unknown keys are kept rather than rejected, and that is load-bearing: the checker reads `version_field` out of them for `type: "url"`, and both shipped url-typed packs (icon-sets-bundled, icon-sets-twemoji) rely on it. Python expressed this as `extra="allow"`.
func (SourceConfig) MarshalJSON ¶
func (s SourceConfig) MarshalJSON() ([]byte, error)
MarshalJSON writes the extras back alongside the known fields, so `config print` shows the source block as the file declared it.
func (*SourceConfig) UnmarshalJSON ¶
func (s *SourceConfig) UnmarshalJSON(data []byte) error
func (SourceConfig) VersionField ¶
func (s SourceConfig) VersionField() string
VersionField returns the dot-path into the version-check response for a url-typed source, defaulting to "version" exactly as the checker does.