confval

package
v3.3.13 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package confval 读写行式配置文件中的键值项

Index

Constants

This section is empty.

Variables

View Source
var (
	// Directive 「键 值」,用于 redis、valkey
	Directive = Codec{Assign: " ", Comments: []string{"#"}}
	// FTP 「键 值」,pure-ftpd 的配置项必须带值
	FTP = Codec{Assign: " ", Comments: []string{"#"}, RequireValue: true}
	// Properties 「键=值」,用于 kafka、rocketmq
	Properties = Codec{Delim: "=", Assign: "=", Comments: []string{"#"}}
	// Nginx 「键 值;」,支持行尾注释
	Nginx = Codec{Assign: " ", Terminator: ";", Comments: []string{"#"}, InlineComment: true, RequireValue: true, AppendIndent: "    "}
	// Postgres 「键 = '值'」,支持行尾注释
	Postgres = Codec{Delim: "=", Assign: " = ", Quote: "'", Comments: []string{"#"}, InlineComment: true}
	// SectionINI 分组感知的 INI,用于 grafana
	SectionINI = Codec{Delim: "=", Assign: " = ", Comments: []string{"#", ";"}, Sections: true}
	// PHPINI PHP 的 ini,注释优先使用 ";"
	PHPINI = Codec{Delim: "=", Assign: " = ", Comments: []string{";", "#"}}
	// Supervisor 「键=值」,注释同时支持 ";" 与 "#"
	Supervisor = Codec{Delim: "=", Assign: "=", Comments: []string{";", "#"}}
)

各应用配置文件的书写约定

Functions

func GetYAML

func GetYAML(cfg map[string]any, key string) string

GetYAML 读取 YAML 配置项,优先平铺键(安装脚本用 sed 生成的格式),回退到嵌套键

func JVMHeap added in v3.3.13

func JVMHeap(opts string) (initSize, maxSize string)

JVMHeap 从 JVM 参数中取出 -Xms 与 -Xmx 的值,jvm.options 里被注释掉的示例行不算

func LoadYAML added in v3.3.13

func LoadYAML(path string) (map[string]any, error)

LoadYAML 读取 YAML 配置文件,文件不存在、内容为空或只有注释时得到空 map,返回的 map 始终非 nil

func ReadTOML added in v3.3.13

func ReadTOML(content string, fields []Field)

ReadTOML 按字段表读出根表配置项

func SetYAML

func SetYAML(cfg map[string]any, key string, value string)

SetYAML 以平铺键写入 YAML 配置项,同时清理可能残留的同名嵌套键

func SetYAMLNested added in v3.3.13

func SetYAMLNested(cfg map[string]any, key string, value string)

SetYAMLNested 以嵌套键写入 YAML 配置项,逐层补建 map,同时清理可能残留的同名平铺键

func WriteTOML added in v3.3.13

func WriteTOML(content string, fields []Field) string

WriteTOML 按字段表写回根表配置项

Types

type Codec

type Codec struct {
	// Delim 读取时分隔键与值的字符,空表示按空白切分
	Delim string
	// Assign 写入时的键值分隔符,如 " "、"="、" = "
	Assign string
	// Terminator 行尾终止符,如 nginx 的 ";"
	Terminator string
	// Quote 写入时给值添加的引号,读取时一并去除
	Quote string
	// Comments 识别为注释的前缀,第一个用于注释掉配置项
	Comments []string
	// Sections 是否按 [section] 分组匹配
	Sections bool
	// InlineComment 该格式支持行尾注释
	// 仅对确实支持的格式开启:redis.conf 与 java properties 不支持,
	// 值里的 "#" 是数据(如密码),误判会损坏配置
	InlineComment bool
	// RequireValue 匹配时要求该行已带值,用于区分「开关」与「键 值」
	RequireValue bool
	// AppendIndent 追加新配置项时使用的缩进
	AppendIndent string
}

Codec 描述一种行式配置文件的书写约定

func (Codec) Get

func (c Codec) Get(content, key string) string

Get 读取配置项,未命中返回空串

func (Codec) GetIn

func (c Codec) GetIn(content, section, key string) string

GetIn 在指定分组内读取配置项,section 为空表示不限分组

func (Codec) Read added in v3.3.13

func (c Codec) Read(content string, fields []Field)

Read 按字段表读出各配置项

func (Codec) Set

func (c Codec) Set(content, key, value string) string

Set 写入配置项,值为空时注释掉该项

func (Codec) SetIn

func (c Codec) SetIn(content, section, key, value string) string

SetIn 在指定分组内写入配置项,分组不存在时在末尾补建

func (Codec) Write added in v3.3.13

func (c Codec) Write(content string, fields []Field) string

Write 按字段表写回各配置项

type Field added in v3.3.13

type Field struct {
	// Section 所在分组,仅分组感知的 Codec 使用
	Section string
	Key     string
	Value   *string
	// Kind 仅 TOML 使用
	Kind Kind
}

Field 一个配置项与承载其值的表单字段的对应关系

type Kind added in v3.3.13

type Kind uint8

Kind 字段写回 TOML 时的字面量类型,表单一律用字符串收值

const (
	String Kind = iota
	Int
	Bool
)

Jump to

Keyboard shortcuts

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