config

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteAccess added in v0.0.9

func DeleteAccess() error

DeleteAccess 删除本地 access.json 文件。

实现逻辑:

  • 通过 getAccessPath() 获取 access.json 路径(复用现有逻辑)
  • 文件不存在时返回 nil(幂等,不报错)
  • 文件存在时调用 os.Remove 删除
  • 删除失败时返回 error,由调用方决定如何处理

安全说明:access.json 包含用户的 access token 和 refresh token, 登出时必须彻底删除以防止 token 泄露(T-09-01)。

func GetAccessToken

func GetAccessToken() string

func GetCacheDir added in v0.0.5

func GetCacheDir() (string, error)

GetCacheDir 获取缓存目录。 若配置未初始化,返回 error。

func GetConfigPath

func GetConfigPath() string

func GetDBFile added in v0.0.5

func GetDBFile() (string, error)

GetDBFile 返回 SQLite 数据库文件 DSN。 若配置未初始化,返回 error。

func GetDefaultConfigPath added in v0.0.3

func GetDefaultConfigPath() (string, error)

func GetLogFile added in v0.0.5

func GetLogFile() (string, error)

GetLogFile 返回日志文件路径。 若配置未初始化,返回 error。

func Init

func Init(configFile string) error

初始化配置 功能需求: - 如果 config 变量有值,直接返回 - 初始化的过程使用 sync.Once 保证只做一次 - 先通过 configYml 加载配置 config - 如果 configFile 地址存在

  • 如果内容不是 Yml 配置,则报错
  • 是配置文件,则对 config 进行合并,字段值以文件中优先

- *_dir/*_file 后缀的字段,使用 homedir.Expand 做 ~/ 地址开头解析 - 如果 DataDir 为空,需要使用 utils.GetUserDataRoot 获取默认值

单元测试见本包 _test

func SaveAccess

func SaveAccess(a Access) error

func SaveCredential

func SaveCredential(c Credential) error

func Set

func Set(conf *Config)

func SetConfigPath added in v0.0.3

func SetConfigPath(path string)

func Show

func Show(hiddenFields ...string) string

Show 将全局配置序列化为 YAML 格式字符串,并隐藏敏感字段。 hiddenFields 参数指定额外需要隐藏的字段名(与默认隐藏列表合并)。 若 config 为 nil 或序列化失败,返回空字符串。

Types

type Access

type Access struct {
	AccessToken      string `yaml:"accessToken" json:"accessToken"`
	ExpiresIn        int    `yaml:"expiresIn" json:"expiresIn"`
	RefreshTimestamp int    `yaml:"refreshTimestamp" json:"refreshTimestamp"`
	RefreshToken     string `yaml:"refreshToken" json:"refreshToken"`
}

func GetAccess added in v0.0.3

func GetAccess() (*Access, error)

func (*Access) IsExpired

func (a *Access) IsExpired() bool

func (*Access) IsNil

func (a *Access) IsNil() bool

type App

type App struct {
	Name  string `yaml:"name" json:"name"`
	Scope string `yaml:"scope" json:"scope"`
}

type Config

type Config struct {
	App     App    `yaml:"app" json:"app"`
	DataDir string `yaml:"data_dir" json:"data_dir" mapstructure:"data_dir"`
}

func Get

func Get() (*Config, error)

Get 返回全局配置实例。 若配置未初始化(Init 未调用),返回 error 而非 panic。

type Credential

type Credential struct {
	AppID     int    `yaml:"appId" json:"appId"`
	AppKey    string `yaml:"appKey" json:"appKey"`
	SecretKey string `yaml:"secretKey" json:"secretKey"`
	SignKey   string `yaml:"signKey" json:"signKey"`
}

func GetCredential added in v0.0.5

func GetCredential() (*Credential, error)

func (*Credential) IsNil

func (c *Credential) IsNil() bool

Jump to

Keyboard shortcuts

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