confval

package
v3.3.8 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: BSD-3-Clause Imports: 4 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}
	// INI 「键 = 值」,忽略分组,用于 mysql
	INI = Codec{Delim: "=", Assign: " = ", Comments: []string{"#", ";"}}
	// 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 GetTOML

func GetTOML(content, key string) string

GetTOML 读取根表配置项,兼容点号键(auth.token = "x")与分组写法([auth] 下的 token = "x")

func GetYAML

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

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

func SetTOML

func SetTOML(content, key string, value any) string

SetTOML 写入根表配置项,值为空时注释掉该项;新键以点号形式插入到首个表头之前

func SetYAML

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

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

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) 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 在指定分组内写入配置项,分组不存在时在末尾补建

Jump to

Keyboard shortcuts

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