mirror of
https://github.com/xiaoqidun/goini.git
synced 2026-09-03 08:10:22 +08:00
Compare commits
2
Commits
v1.0.8
...
7914cbd743
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7914cbd743 | ||
|
|
99c0d37995 |
No files matched your search
@@ -94,42 +94,6 @@ func (ini *GoINI) SaveFormatFile(filename string, perm os.FileMode) error {
|
|||||||
return nil
|
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 == "" {
|
|
||||||
return errFilenameRequired
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
ini.filename = filename
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// String 获取GoINI对象的字符串形式
|
// String 获取GoINI对象的字符串形式
|
||||||
// 返回: string 配置文件内容
|
// 返回: string 配置文件内容
|
||||||
func (ini *GoINI) String() string {
|
func (ini *GoINI) String() string {
|
||||||
@@ -229,6 +193,24 @@ func (ini *GoINI) GetNameKeys(name string, match string) []string {
|
|||||||
return matchStrings(ini.sectionKeys[name], match)
|
return matchStrings(ini.sectionKeys[name], match)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasSection 判断分区是否存在
|
||||||
|
// 入参: name 分区名称
|
||||||
|
// 返回: bool 是否存在
|
||||||
|
func (ini *GoINI) HasSection(name string) bool {
|
||||||
|
name = ini.normalizeSection(name)
|
||||||
|
_, ok := ini.sectionValues[name]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasKey 判断分区下配置项是否存在
|
||||||
|
// 入参: name 分区名称, key 配置项名称
|
||||||
|
// 返回: bool 是否存在
|
||||||
|
func (ini *GoINI) HasKey(name string, key string) bool {
|
||||||
|
name = ini.normalizeSection(name)
|
||||||
|
_, ok := ini.sectionValues[name][key]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// GetString 获取单个配置项的字符串值
|
// GetString 获取单个配置项的字符串值
|
||||||
// 入参: name 分区名称, key 配置项名称, value 默认值
|
// 入参: name 分区名称, key 配置项名称, value 默认值
|
||||||
// 返回: string 配置项值
|
// 返回: string 配置项值
|
||||||
|
|||||||
+39
-4
@@ -1,6 +1,8 @@
|
|||||||
package goini
|
package goini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -207,9 +209,6 @@ func (ini *GoINI) findSectionLineIndex(sectionName string) (int, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ini *GoINI) findSectionKeyLineIndex(sectionName string, keyName string) (int, bool) {
|
func (ini *GoINI) findSectionKeyLineIndex(sectionName string, keyName string) (int, bool) {
|
||||||
if ini.sectionKeyLineIndexes[sectionName] == nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
index, ok := ini.sectionKeyLineIndexes[sectionName][keyName]
|
index, ok := ini.sectionKeyLineIndexes[sectionName][keyName]
|
||||||
return index, ok
|
return index, ok
|
||||||
}
|
}
|
||||||
@@ -337,7 +336,7 @@ func commentLines(comment string) []iniLine {
|
|||||||
|
|
||||||
func formatComment(comment string) string {
|
func formatComment(comment string) string {
|
||||||
trimmed := strings.TrimLeftFunc(comment, unicode.IsSpace)
|
trimmed := strings.TrimLeftFunc(comment, unicode.IsSpace)
|
||||||
if strings.HasPrefix(trimmed, "#") || strings.HasPrefix(trimmed, ";") {
|
if isComment(trimmed) {
|
||||||
return comment
|
return comment
|
||||||
}
|
}
|
||||||
if comment == "" {
|
if comment == "" {
|
||||||
@@ -431,6 +430,42 @@ func formatINIComment(comment string) string {
|
|||||||
return "# " + comment
|
return "# " + comment
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 == "" {
|
||||||
|
return errFilenameRequired
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
ini.filename = filename
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
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