config

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 12 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 config 提供按模块划分的明文 JSON 配置存储。

每个模块对应目录下的一个 <name>.json 文件。写入采用"临时文件 + rename"保证原子性, 文件权限为 0600,目录权限为 0700。配置保持明文,便于用户审计、备份和纳入 git。

Index

Constants

This section is empty.

Variables

View Source
var ErrNoVersion = errors.New("历史版本不存在")

ErrNoVersion 表示指定的历史版本不存在。

Functions

This section is empty.

Types

type DiffLine added in v0.5.0

type DiffLine struct {
	Op   string `json:"op"`
	Text string `json:"text"`
}

DiffLine 是行级差异中的一行。Op 为 " "(相同)、"+"(新增)、"-"(删除)、"@"(省略的未变更行数)。

func Diff added in v0.5.0

func Diff(oldText, newText string) []DiffLine

Diff 对两段文本做行级比较,返回带上下文的差异。相同的长段落折叠为一行 "@"。

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store 是配置目录的句柄,可被多个模块并发使用。

func Open

func Open(dir string) (*Store, error)

Open 打开(必要时创建)配置目录,并把它登记为受保护目录:目录里有会话签名密钥、令牌与私钥, 存储挂载、静态站点与文件服务据此拒绝把它(或包含它的目录)提供出去,见 fsperm.Protect。

func (*Store) Dir

func (s *Store) Dir() string

Dir 返回配置目录路径。

func (*Store) EnableHistory added in v0.5.0

func (s *Store) EnableHistory(keep int, skip ...string)

EnableHistory 开启历史版本:每次覆盖配置文件前,把旧内容保存为一个版本, 每个配置最多保留 keep 个,skip 中的配置不记录(例如高频写入或含账号凭据的文件)。

func (*Store) HistoryNames added in v0.5.0

func (s *Store) HistoryNames() []string

HistoryNames 返回存在历史版本的配置名。

func (*Store) Load

func (s *Store) Load(name string, v any) (bool, error)

Load 读取配置到 v。文件不存在时返回 (false, nil),v 保持调用方设置的默认值。

func (*Store) ReadRaw added in v0.5.0

func (s *Store) ReadRaw(name string) ([]byte, error)

ReadRaw 读取配置文件的当前内容;文件不存在时返回 nil。

func (*Store) ReadVersion added in v0.5.0

func (s *Store) ReadVersion(name, id string) ([]byte, error)

ReadVersion 读取某个历史版本的内容。

func (*Store) Remove added in v0.6.9

func (s *Store) Remove(name string) error

Remove 删除配置文件,删除前把现有内容保存为历史版本,删除本身也可撤销;文件不存在时什么也不做。

func (*Store) RestoreVersion added in v0.5.0

func (s *Store) RestoreVersion(name, id string) error

RestoreVersion 把配置回滚到某个历史版本。当前内容会先保存为新版本,因此回滚本身也可撤销。

func (*Store) Save

func (s *Store) Save(name string, v any) error

Save 将 v 以缩进 JSON 原子写入。

func (*Store) Versions added in v0.5.0

func (s *Store) Versions(name string) ([]Version, error)

Versions 返回某配置的历史版本,最新的在前。

func (*Store) WriteRaw added in v0.2.0

func (s *Store) WriteRaw(name string, data []byte) error

WriteRaw 原子写入已序列化的配置内容(用于从备份恢复)。

type Version added in v0.5.0

type Version struct {
	ID   string    `json:"id"`
	Time time.Time `json:"time"`
	Size int64     `json:"size"`
}

Version 是某个配置文件的一个历史版本(被覆盖前的内容)。

Jump to

Keyboard shortcuts

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