config

package
v0.8.8 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package config 提供 dscli 配置管理,包括全局配置目录 (~/.dscli) 和项目级 .dscli/ 目录的初始化工。

Package conf supports a configuration file format used by gnatsd. It is a flexible format that combines the best of traditional configuration formats and newer styles such as JSON and YAML.

Index

Constants

This section is empty.

Variables

View Source
var (

	// BuildTime is set via ldflags at build time, e.g.
	//   -X github.com/dscli/dscli/internal/config.BuildTime=2026-05-20T08:00:00Z
	BuildTime string
)
View Source
var ConfigDir = func() string {
	return getGlobalConfig().ConfigDir()
}()

ConfigDir 配置目录(向后兼容变量) 注意:这是一个函数调用,返回配置目录路径

Functions

func EnsureProjectGitignore added in v0.8.6

func EnsureProjectGitignore(projectRoot string) error

EnsureProjectGitignore 确保项目级 .dscli/.gitignore 存在。

projectRoot 是项目的根目录(通常为 context.ProjectRoot)。

策略:

  • 仅当 .dscli/ 目录已存在时工作(不因创建 .gitignore 而反创建父目录)
  • 如果 .gitignore 已存在,跳过(保留用户自定义修改)
  • 幂等,可安全重复调用

func Get

func Get(name, defaultValue string, alias ...string) string

Get 获取配置值(向后兼容接口) 使用全局配置实例,支持懒加载

func GetBool

func GetBool(name string, defaultValue bool, alias ...string) bool

支持 strconv.ParseBool 的格式:1/t/T/TRUE/true/True → true,0/f/F/FALSE/false/False → false 未配置或解析失败时返回 defaultValue

func GetInt

func GetInt(name string, defaultValue int, alias ...string) int

GetInt 获取整数配置值 未配置或解析失败时返回 defaultValue

func GetValue added in v0.8.8

func GetValue(name string) any

GetValue 获取全局配置值,返回原始类型(支持 map、slice 等结构化数据)。 未配置时返回 nil。

func Parse added in v0.8.8

func Parse(data string) (map[string]any, error)

Parse will return a map of keys to any, although concrete types underly them. The values supported are string, bool, int64, float64, DateTime. Arrays and nested Maps are also supported.

func ParseFile added in v0.8.8

func ParseFile(fp string) (map[string]any, error)

ParseFile is a helper to open file, etc. and parse the contents.

func ParseFileWithChecks added in v0.8.8

func ParseFileWithChecks(fp string) (map[string]any, error)

ParseFileWithChecks is equivalent to ParseFile but runs in pedantic mode.

func ParseFileWithChecksDigest added in v0.8.8

func ParseFileWithChecksDigest(fp string) (map[string]any, string, error)

ParseFileWithChecksDigest returns the processed config and a digest that represents the configuration.

func ParseWithChecks added in v0.8.8

func ParseWithChecks(data string) (map[string]any, error)

ParseWithChecks is equivalent to Parse but runs in pedantic mode.

func Set

func Set(name, value string)

Set 设置全局配置值(仅内存中,不持久化到文件)。 主要用于测试。

func SetValue added in v0.8.8

func SetValue(name string, value any)

SetValue 设置全局配置值(仅内存中,不持久化到文件)。 支持结构化数据(map、slice 等),主要用于测试。

Types

type Config

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

Config 配置管理器

func New

func New() (*Config, error)

New 创建新的配置管理器

func NewWithDir

func NewWithDir(dir string) (*Config, error)

NewWithDir 使用指定目录创建配置管理器

func (*Config) ConfigDir

func (c *Config) ConfigDir() string

ConfigDir 返回配置目录

func (*Config) Get

func (c *Config) Get(name, defaultValue string, alias ...string) string

Get 获取配置值

func (*Config) GetValue added in v0.8.8

func (c *Config) GetValue(name string) any

GetValue 获取配置值,返回原始类型(支持 map、slice 等结构化数据)。 未配置时返回 nil。

func (*Config) Save

func (c *Config) Save() error

Save 保存配置到文件

func (*Config) Set

func (c *Config) Set(name, value string)

Set 设置配置值(仅内存中)

func (*Config) SetValue added in v0.8.8

func (c *Config) SetValue(name string, value any)

SetValue 设置配置值,支持结构化数据(map、slice 等)。 仅在内存中设置,不持久化。调用 Save() 写入文件。

Jump to

Keyboard shortcuts

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