localstate

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 9 Imported by: 0 modules, 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 localstate config.go 实现 config.toml 的加载/校验/写入(PRD-0017)。

设计原则(决策见 CONTEXT.md「不可逆决策」表):

  • config key 只收「已存在 flag 或硬编码默认值」(当前六项,见 configKeys);
  • 文件不存在 = 正常态(全部内置默认);文件存在但坏 = 硬错误,静默回退会掩盖用户设置;
  • set 写盘前完成全部校验,读取侧复用同一套校验(手改文件同样被拦);
  • tmp+rename 原子写,文件 0600、目录 0700(与 session 一致)。

Package localstate proxy.go 实现代理 URL 的解析与校验(PRD-0018)。

flag(--proxy)与 config(proxy key)共用本函数:单一校验真相。 空串 = 未设置(返回 nil,nil,回落环境变量层——那是 Go 默认 transport 的 ProxyFromEnvironment 行为,本包不做任何注入)。

Index

Constants

View Source
const (
	ProxySourceFlag   = "flag"   // --proxy 显式覆盖
	ProxySourceConfig = "config" // config.toml 的 proxy key
)

ProxySource 常量:UseProxy 注入时的来源标记,doctor 据此展示解析链。

Variables

This section is empty.

Functions

func ClearSession

func ClearSession() error

ClearSession 删除本地会话文件,不存在时不算错误。

func ConfigDir

func ConfigDir() (string, error)

ConfigDir 返回 kite 本地状态基目录(所有状态文件派生自此)。 目录在首次写盘时由调用方 MkdirAll(0700);本函数只算路径不创建。

func ConfigKeys

func ConfigKeys() []string

ConfigKeys 返回合法 key 列表(顺序即 config get 无参列表顺序,单一真相)。

func ConfigPath

func ConfigPath() (string, error)

ConfigPath 返回 config.toml 路径 <ConfigDir>/config.toml(只算路径不创建)。

func CurrentCookie

func CurrentCookie(errOut io.Writer) string

CurrentCookie 返回当前生效的 cookie: 环境变量 NETEASE_COOKIE 优先,其次本地会话文件。

func FilenameTemplatePlaceholders

func FilenameTemplatePlaceholders() []string

FilenameTemplatePlaceholders 返回 filename_template 的合法占位符(config schema 的 单一真相)。songdl 的执行侧(songdl.FormatFilename)与这里由 songdl 包的守护测试保持同步——公共层只定义 schema,不夹带下载领域逻辑。

func HistoryPath

func HistoryPath() (string, error)

HistoryPath 返回召回池事件流文件路径 <ConfigDir>/history.jsonl。

召回池(#G,PRD-0014)尚未落地;本函数为它预留 seam,与 session 同源派生, 使 #G 落盘路径决策零额外成本。当前无调用者。

func IsKnownConfigKey

func IsKnownConfigKey(key string) bool

IsKnownConfigKey 判断 key 是否在 configKeys 枚举内(config get/set 命令层校验用)。

func ParseProxyURL

func ParseProxyURL(raw string) (*url.URL, error)

ParseProxyURL 校验并解析代理地址。

  • 空串 → (nil, nil):未设置,不注入任何代理(环境变量层自然生效)。
  • scheme 必须显式写(http:// / https:// / socks5://):裸 host:port 报错并 引导补前缀——猜测默认值是文档陷阱。
  • host 非空;端口可缺省(用 scheme 默认端口)。

func SaveSession

func SaveSession(sess Session) error

SaveSession 把会话写盘(目录 0700 / 文件 0600)。

func SessionPath

func SessionPath() (string, error)

SessionPath 返回会话文件路径 <ConfigDir>/session.json。

func SetConfigKey

func SetConfigKey(key, value string) error

SetConfigKey 校验并写入单个 key(整文件重写,tmp+rename 原子落盘)。 只落显式设置过的 key——未设置的留空,读取侧回退内置默认, 「来源」列(config get 无参列表)才有区分度。 坏文件在手时同样报错——不允许绕过校验覆盖写。

Types

type Config

type Config struct {
	Level            int    // 默认音质,1=standard 2=exhigh 3=lossless 4=hires
	Output           string // "table" | "json"
	DownloadDir      string // 下载目录(--out)
	FilenameTemplate string // 文件名模板,空 = {artist} - {title}
	Workers          int    // playlist download 并发数(--workers)
	Proxy            string // 代理地址,空 = 未设置(回落环境变量层,PRD-0018)
}

Config 是用户偏好的生效值集合(字段与 configKeys 一一对应)。 零值不可直接使用(Workers=0 非法), 一律经 DefaultConfig() 或 LoadConfig() 获得。

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 返回内置默认值(与各 flag 的硬编码默认对齐,独立真相)。

func LoadConfig

func LoadConfig() (Config, error)

LoadConfig 读取 config.toml 并校验。文件不存在返回内置默认(正常态,非错误); 文件存在但解析失败/校验不过返回 error(硬错误,调用方应拒绝执行,PRD-0017)。

func LoadConfigWithSet

func LoadConfigWithSet() (Config, map[string]bool, error)

LoadConfigWithSet 在 LoadConfig 基础上额外返回「文件里显式设置的 key 集」 (config get 无参列表的「来源」列用)。

type Session

type Session struct {
	Cookie  string `json:"cookie"`
	UserID  int64  `json:"user_id,omitempty"`
	SavedAt string `json:"saved_at"`
}

Session 是落盘的登录会话。cookie 属于敏感信息: 目录 0700、文件 0600,且不把完整 cookie 打印到终端。

func LoadSession

func LoadSession(errOut io.Writer) (Session, error)

SessionPath/ConfigDir/HistoryPath 见 paths.go(同包,唯一路径 seam)。

LoadSession 读本地会话。文件不存在或损坏时返回 error。

读前惰性触发旧路径迁移:首次发现新路径无文件时,把 ~/.kite/session.json 搬到新路径(若旧路径存在),见 migrateLegacySession。迁移失败不阻塞——按新路径 无文件处理(未登录)。

Jump to

Keyboard shortcuts

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