mirror of
https://github.com/xiaoqidun/goini.git
synced 2026-08-23 06:14:41 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c2ceb7dea7 | ||
|
|
1f9d169cf1 | ||
|
|
ee27859f7e | ||
|
|
96e3375441 |
@@ -1,6 +1,6 @@
|
|||||||
# GoINI [](https://pkg.go.dev/github.com/xiaoqidun/goini)
|
# GoINI [](https://pkg.go.dev/github.com/xiaoqidun/goini)
|
||||||
|
|
||||||
零依赖、够简单的 Go 语言 INI 配置文件读写库
|
够简单、零依赖的 Go 语言 INI 配置文件读写库
|
||||||
|
|
||||||
# 安装方法
|
# 安装方法
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ var (
|
|||||||
|
|
||||||
// GoINI 表示INI配置解析器
|
// GoINI 表示INI配置解析器
|
||||||
type GoINI struct {
|
type GoINI struct {
|
||||||
data []byte
|
|
||||||
filename string
|
filename string
|
||||||
fileLines []iniLine
|
fileLines []iniLine
|
||||||
structTag string
|
structTag string
|
||||||
@@ -43,8 +42,7 @@ func NewGoINI() *GoINI {
|
|||||||
// 入参: fileData 配置文件内容
|
// 入参: fileData 配置文件内容
|
||||||
func (ini *GoINI) SetData(fileData []byte) {
|
func (ini *GoINI) SetData(fileData []byte) {
|
||||||
ini.filename = ""
|
ini.filename = ""
|
||||||
ini.data = append(ini.data[:0], fileData...)
|
ini.parse(string(fileData))
|
||||||
ini.parse()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadFile 从文件读取配置并解析
|
// LoadFile 从文件读取配置并解析
|
||||||
@@ -56,28 +54,76 @@ func (ini *GoINI) LoadFile(filename string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ini.filename = filename
|
ini.filename = filename
|
||||||
ini.data = append(ini.data[:0], b...)
|
ini.parse(string(b))
|
||||||
ini.parse()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save 将配置写回LoadFile读取的文件
|
// Save 将配置写回LoadFile读取的文件
|
||||||
// 返回: error 错误信息
|
// 返回: error 错误信息
|
||||||
func (ini *GoINI) Save() error {
|
func (ini *GoINI) Save() error {
|
||||||
if ini.filename == "" {
|
return ini.save(ini.String())
|
||||||
return errFilenameRequired
|
}
|
||||||
|
|
||||||
|
// SaveFormat 将配置格式化并写回LoadFile读取的文件
|
||||||
|
// 返回: error 错误信息
|
||||||
|
func (ini *GoINI) SaveFormat() error {
|
||||||
|
formatted := ini.formatString()
|
||||||
|
if err := ini.save(formatted); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return ini.SaveFile(ini.filename)
|
ini.parse(formatted)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveFile 将配置写入指定文件
|
// SaveFile 将配置写入指定文件
|
||||||
// 入参: filename 配置文件路径
|
// 入参: filename 配置文件路径, perm 文件权限
|
||||||
// 返回: error 错误信息
|
// 返回: error 错误信息
|
||||||
func (ini *GoINI) SaveFile(filename string) error {
|
func (ini *GoINI) SaveFile(filename string, perm os.FileMode) error {
|
||||||
|
return ini.saveFile(filename, ini.String(), perm)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveFormatFile 将配置格式化并写入指定文件
|
||||||
|
// 入参: filename 配置文件路径, perm 文件权限
|
||||||
|
// 返回: error 错误信息
|
||||||
|
func (ini *GoINI) SaveFormatFile(filename string, perm os.FileMode) error {
|
||||||
|
formatted := ini.formatString()
|
||||||
|
if err := ini.saveFile(filename, formatted, perm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ini.parse(formatted)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ini *GoINI) save(data string) error {
|
||||||
|
if ini.filename == "" {
|
||||||
|
return errFilenameRequired
|
||||||
|
}
|
||||||
|
info, err := os.Stat(ini.filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return ini.saveFile(ini.filename, data, info.Mode().Perm())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ini *GoINI) saveFile(filename string, data string, perm os.FileMode) error {
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
return errFilenameRequired
|
return errFilenameRequired
|
||||||
}
|
}
|
||||||
if err := os.WriteFile(filename, []byte(ini.String()), 0o666); err != nil {
|
perm = perm.Perm()
|
||||||
|
writePerm := perm | 0o200
|
||||||
|
if info, err := os.Stat(filename); err == nil {
|
||||||
|
if info.Mode().IsRegular() {
|
||||||
|
if err := os.Chmod(filename, writePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filename, []byte(data), writePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.Chmod(filename, perm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ini.filename = filename
|
ini.filename = filename
|
||||||
@@ -288,3 +334,19 @@ func (ini *GoINI) MapToStruct(ptr interface{}) error {
|
|||||||
ini.mapStruct(v)
|
ini.mapStruct(v)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MapSectionToStruct 将指定分区映射到一个结构体
|
||||||
|
// 入参: name 分区名称, ptr 结构体指针
|
||||||
|
// 返回: error 错误信息
|
||||||
|
func (ini *GoINI) MapSectionToStruct(name string, ptr interface{}) error {
|
||||||
|
v := reflect.ValueOf(ptr)
|
||||||
|
if !v.IsValid() || v.Kind() != reflect.Ptr || v.IsNil() {
|
||||||
|
return errStructPointerRequired
|
||||||
|
}
|
||||||
|
v = v.Elem()
|
||||||
|
if v.Kind() != reflect.Struct {
|
||||||
|
return errStructPointerRequired
|
||||||
|
}
|
||||||
|
ini.mapSection(v, ini.normalizeSection(name))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+39
-4
@@ -36,14 +36,14 @@ type parsedKeyValue struct {
|
|||||||
separator string
|
separator string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ini *GoINI) parse() {
|
func (ini *GoINI) parse(data string) {
|
||||||
ini.fileLines = nil
|
ini.fileLines = nil
|
||||||
currentSection := ini.rootSection
|
currentSection := ini.rootSection
|
||||||
if len(ini.data) == 0 {
|
if data == "" {
|
||||||
ini.rebuildIndexes()
|
ini.rebuildIndexes()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, rawLine := range strings.Split(string(ini.data), "\n") {
|
for _, rawLine := range strings.Split(data, "\n") {
|
||||||
line, section := parseRawLine(rawLine, currentSection)
|
line, section := parseRawLine(rawLine, currentSection)
|
||||||
currentSection = section
|
currentSection = section
|
||||||
ini.fileLines = append(ini.fileLines, line)
|
ini.fileLines = append(ini.fileLines, line)
|
||||||
@@ -187,7 +187,7 @@ func (ini *GoINI) addSectionKey(sectionName string, keyName string, keyValue str
|
|||||||
|
|
||||||
func (ini *GoINI) ensureParsed() {
|
func (ini *GoINI) ensureParsed() {
|
||||||
if ini.sectionValues == nil {
|
if ini.sectionValues == nil {
|
||||||
ini.parse()
|
ini.parse("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -377,6 +377,41 @@ func formatValue(value string) string {
|
|||||||
return quote + value + quote
|
return quote + value + quote
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ini *GoINI) formatString() string {
|
||||||
|
formattedLines := make([]string, 0, len(ini.fileLines))
|
||||||
|
pendingComments := make([]string, 0)
|
||||||
|
for _, line := range ini.fileLines {
|
||||||
|
switch line.kind {
|
||||||
|
case lineSection:
|
||||||
|
if len(formattedLines) > 0 {
|
||||||
|
formattedLines = append(formattedLines, "")
|
||||||
|
}
|
||||||
|
formattedLines = append(formattedLines, pendingComments...)
|
||||||
|
pendingComments = pendingComments[:0]
|
||||||
|
formattedLines = append(formattedLines, "["+line.sectionName+"]")
|
||||||
|
case lineKeyValue:
|
||||||
|
formattedLines = append(formattedLines, pendingComments...)
|
||||||
|
pendingComments = pendingComments[:0]
|
||||||
|
formattedLines = append(formattedLines, line.keyName+defaultSeparator+formatValue(line.keyValue))
|
||||||
|
case lineComment:
|
||||||
|
pendingComments = append(pendingComments, formatINIComment(line.text))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
formattedLines = append(formattedLines, pendingComments...)
|
||||||
|
if len(formattedLines) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.Join(formattedLines, "\n") + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatINIComment(comment string) string {
|
||||||
|
comment = stripComment(comment)
|
||||||
|
if comment == "" {
|
||||||
|
return "#"
|
||||||
|
}
|
||||||
|
return "# " + comment
|
||||||
|
}
|
||||||
|
|
||||||
func matchStrings(items []string, match string) []string {
|
func matchStrings(items []string, match string) []string {
|
||||||
if match == "" {
|
if match == "" {
|
||||||
return cloneStrings(items)
|
return cloneStrings(items)
|
||||||
|
|||||||
Reference in New Issue
Block a user