You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ini.go 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // Copyright 2014 Unknwon
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. // Package ini provides INI file read and write functionality in Go.
  15. package ini
  16. import (
  17. "bytes"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "os"
  23. "regexp"
  24. "runtime"
  25. "strings"
  26. "sync"
  27. )
  28. const (
  29. // Name for default section. You can use this constant or the string literal.
  30. // In most of cases, an empty string is all you need to access the section.
  31. DEFAULT_SECTION = "DEFAULT"
  32. // Maximum allowed depth when recursively substituing variable names.
  33. _DEPTH_VALUES = 99
  34. _VERSION = "1.28.2"
  35. )
  36. // Version returns current package version literal.
  37. func Version() string {
  38. return _VERSION
  39. }
  40. var (
  41. // Delimiter to determine or compose a new line.
  42. // This variable will be changed to "\r\n" automatically on Windows
  43. // at package init time.
  44. LineBreak = "\n"
  45. // Variable regexp pattern: %(variable)s
  46. varPattern = regexp.MustCompile(`%\(([^\)]+)\)s`)
  47. // Indicate whether to align "=" sign with spaces to produce pretty output
  48. // or reduce all possible spaces for compact format.
  49. PrettyFormat = true
  50. // Explicitly write DEFAULT section header
  51. DefaultHeader = false
  52. // Indicate whether to put a line between sections
  53. PrettySection = true
  54. )
  55. func init() {
  56. if runtime.GOOS == "windows" {
  57. LineBreak = "\r\n"
  58. }
  59. }
  60. func inSlice(str string, s []string) bool {
  61. for _, v := range s {
  62. if str == v {
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. // dataSource is an interface that returns object which can be read and closed.
  69. type dataSource interface {
  70. ReadCloser() (io.ReadCloser, error)
  71. }
  72. // sourceFile represents an object that contains content on the local file system.
  73. type sourceFile struct {
  74. name string
  75. }
  76. func (s sourceFile) ReadCloser() (_ io.ReadCloser, err error) {
  77. return os.Open(s.name)
  78. }
  79. type bytesReadCloser struct {
  80. reader io.Reader
  81. }
  82. func (rc *bytesReadCloser) Read(p []byte) (n int, err error) {
  83. return rc.reader.Read(p)
  84. }
  85. func (rc *bytesReadCloser) Close() error {
  86. return nil
  87. }
  88. // sourceData represents an object that contains content in memory.
  89. type sourceData struct {
  90. data []byte
  91. }
  92. func (s *sourceData) ReadCloser() (io.ReadCloser, error) {
  93. return ioutil.NopCloser(bytes.NewReader(s.data)), nil
  94. }
  95. // sourceReadCloser represents an input stream with Close method.
  96. type sourceReadCloser struct {
  97. reader io.ReadCloser
  98. }
  99. func (s *sourceReadCloser) ReadCloser() (io.ReadCloser, error) {
  100. return s.reader, nil
  101. }
  102. // File represents a combination of a or more INI file(s) in memory.
  103. type File struct {
  104. // Should make things safe, but sometimes doesn't matter.
  105. BlockMode bool
  106. // Make sure data is safe in multiple goroutines.
  107. lock sync.RWMutex
  108. // Allow combination of multiple data sources.
  109. dataSources []dataSource
  110. // Actual data is stored here.
  111. sections map[string]*Section
  112. // To keep data in order.
  113. sectionList []string
  114. options LoadOptions
  115. NameMapper
  116. ValueMapper
  117. }
  118. // newFile initializes File object with given data sources.
  119. func newFile(dataSources []dataSource, opts LoadOptions) *File {
  120. return &File{
  121. BlockMode: true,
  122. dataSources: dataSources,
  123. sections: make(map[string]*Section),
  124. sectionList: make([]string, 0, 10),
  125. options: opts,
  126. }
  127. }
  128. func parseDataSource(source interface{}) (dataSource, error) {
  129. switch s := source.(type) {
  130. case string:
  131. return sourceFile{s}, nil
  132. case []byte:
  133. return &sourceData{s}, nil
  134. case io.ReadCloser:
  135. return &sourceReadCloser{s}, nil
  136. default:
  137. return nil, fmt.Errorf("error parsing data source: unknown type '%s'", s)
  138. }
  139. }
  140. type LoadOptions struct {
  141. // Loose indicates whether the parser should ignore nonexistent files or return error.
  142. Loose bool
  143. // Insensitive indicates whether the parser forces all section and key names to lowercase.
  144. Insensitive bool
  145. // IgnoreContinuation indicates whether to ignore continuation lines while parsing.
  146. IgnoreContinuation bool
  147. // IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.
  148. IgnoreInlineComment bool
  149. // AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
  150. // This type of keys are mostly used in my.cnf.
  151. AllowBooleanKeys bool
  152. // AllowShadows indicates whether to keep track of keys with same name under same section.
  153. AllowShadows bool
  154. // Some INI formats allow group blocks that store a block of raw content that doesn't otherwise
  155. // conform to key/value pairs. Specify the names of those blocks here.
  156. UnparseableSections []string
  157. }
  158. func LoadSources(opts LoadOptions, source interface{}, others ...interface{}) (_ *File, err error) {
  159. sources := make([]dataSource, len(others)+1)
  160. sources[0], err = parseDataSource(source)
  161. if err != nil {
  162. return nil, err
  163. }
  164. for i := range others {
  165. sources[i+1], err = parseDataSource(others[i])
  166. if err != nil {
  167. return nil, err
  168. }
  169. }
  170. f := newFile(sources, opts)
  171. if err = f.Reload(); err != nil {
  172. return nil, err
  173. }
  174. return f, nil
  175. }
  176. // Load loads and parses from INI data sources.
  177. // Arguments can be mixed of file name with string type, or raw data in []byte.
  178. // It will return error if list contains nonexistent files.
  179. func Load(source interface{}, others ...interface{}) (*File, error) {
  180. return LoadSources(LoadOptions{}, source, others...)
  181. }
  182. // LooseLoad has exactly same functionality as Load function
  183. // except it ignores nonexistent files instead of returning error.
  184. func LooseLoad(source interface{}, others ...interface{}) (*File, error) {
  185. return LoadSources(LoadOptions{Loose: true}, source, others...)
  186. }
  187. // InsensitiveLoad has exactly same functionality as Load function
  188. // except it forces all section and key names to be lowercased.
  189. func InsensitiveLoad(source interface{}, others ...interface{}) (*File, error) {
  190. return LoadSources(LoadOptions{Insensitive: true}, source, others...)
  191. }
  192. // InsensitiveLoad has exactly same functionality as Load function
  193. // except it allows have shadow keys.
  194. func ShadowLoad(source interface{}, others ...interface{}) (*File, error) {
  195. return LoadSources(LoadOptions{AllowShadows: true}, source, others...)
  196. }
  197. // Empty returns an empty file object.
  198. func Empty() *File {
  199. // Ignore error here, we sure our data is good.
  200. f, _ := Load([]byte(""))
  201. return f
  202. }
  203. // NewSection creates a new section.
  204. func (f *File) NewSection(name string) (*Section, error) {
  205. if len(name) == 0 {
  206. return nil, errors.New("error creating new section: empty section name")
  207. } else if f.options.Insensitive && name != DEFAULT_SECTION {
  208. name = strings.ToLower(name)
  209. }
  210. if f.BlockMode {
  211. f.lock.Lock()
  212. defer f.lock.Unlock()
  213. }
  214. if inSlice(name, f.sectionList) {
  215. return f.sections[name], nil
  216. }
  217. f.sectionList = append(f.sectionList, name)
  218. f.sections[name] = newSection(f, name)
  219. return f.sections[name], nil
  220. }
  221. // NewRawSection creates a new section with an unparseable body.
  222. func (f *File) NewRawSection(name, body string) (*Section, error) {
  223. section, err := f.NewSection(name)
  224. if err != nil {
  225. return nil, err
  226. }
  227. section.isRawSection = true
  228. section.rawBody = body
  229. return section, nil
  230. }
  231. // NewSections creates a list of sections.
  232. func (f *File) NewSections(names ...string) (err error) {
  233. for _, name := range names {
  234. if _, err = f.NewSection(name); err != nil {
  235. return err
  236. }
  237. }
  238. return nil
  239. }
  240. // GetSection returns section by given name.
  241. func (f *File) GetSection(name string) (*Section, error) {
  242. if len(name) == 0 {
  243. name = DEFAULT_SECTION
  244. } else if f.options.Insensitive {
  245. name = strings.ToLower(name)
  246. }
  247. if f.BlockMode {
  248. f.lock.RLock()
  249. defer f.lock.RUnlock()
  250. }
  251. sec := f.sections[name]
  252. if sec == nil {
  253. return nil, fmt.Errorf("section '%s' does not exist", name)
  254. }
  255. return sec, nil
  256. }
  257. // Section assumes named section exists and returns a zero-value when not.
  258. func (f *File) Section(name string) *Section {
  259. sec, err := f.GetSection(name)
  260. if err != nil {
  261. // Note: It's OK here because the only possible error is empty section name,
  262. // but if it's empty, this piece of code won't be executed.
  263. sec, _ = f.NewSection(name)
  264. return sec
  265. }
  266. return sec
  267. }
  268. // Section returns list of Section.
  269. func (f *File) Sections() []*Section {
  270. sections := make([]*Section, len(f.sectionList))
  271. for i := range f.sectionList {
  272. sections[i] = f.Section(f.sectionList[i])
  273. }
  274. return sections
  275. }
  276. // ChildSections returns a list of child sections of given section name.
  277. func (f *File) ChildSections(name string) []*Section {
  278. return f.Section(name).ChildSections()
  279. }
  280. // SectionStrings returns list of section names.
  281. func (f *File) SectionStrings() []string {
  282. list := make([]string, len(f.sectionList))
  283. copy(list, f.sectionList)
  284. return list
  285. }
  286. // DeleteSection deletes a section.
  287. func (f *File) DeleteSection(name string) {
  288. if f.BlockMode {
  289. f.lock.Lock()
  290. defer f.lock.Unlock()
  291. }
  292. if len(name) == 0 {
  293. name = DEFAULT_SECTION
  294. }
  295. for i, s := range f.sectionList {
  296. if s == name {
  297. f.sectionList = append(f.sectionList[:i], f.sectionList[i+1:]...)
  298. delete(f.sections, name)
  299. return
  300. }
  301. }
  302. }
  303. func (f *File) reload(s dataSource) error {
  304. r, err := s.ReadCloser()
  305. if err != nil {
  306. return err
  307. }
  308. defer r.Close()
  309. return f.parse(r)
  310. }
  311. // Reload reloads and parses all data sources.
  312. func (f *File) Reload() (err error) {
  313. for _, s := range f.dataSources {
  314. if err = f.reload(s); err != nil {
  315. // In loose mode, we create an empty default section for nonexistent files.
  316. if os.IsNotExist(err) && f.options.Loose {
  317. f.parse(bytes.NewBuffer(nil))
  318. continue
  319. }
  320. return err
  321. }
  322. }
  323. return nil
  324. }
  325. // Append appends one or more data sources and reloads automatically.
  326. func (f *File) Append(source interface{}, others ...interface{}) error {
  327. ds, err := parseDataSource(source)
  328. if err != nil {
  329. return err
  330. }
  331. f.dataSources = append(f.dataSources, ds)
  332. for _, s := range others {
  333. ds, err = parseDataSource(s)
  334. if err != nil {
  335. return err
  336. }
  337. f.dataSources = append(f.dataSources, ds)
  338. }
  339. return f.Reload()
  340. }
  341. func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
  342. equalSign := "="
  343. if PrettyFormat {
  344. equalSign = " = "
  345. }
  346. // Use buffer to make sure target is safe until finish encoding.
  347. buf := bytes.NewBuffer(nil)
  348. for i, sname := range f.sectionList {
  349. sec := f.Section(sname)
  350. if len(sec.Comment) > 0 {
  351. if sec.Comment[0] != '#' && sec.Comment[0] != ';' {
  352. sec.Comment = "; " + sec.Comment
  353. }
  354. if _, err := buf.WriteString(sec.Comment + LineBreak); err != nil {
  355. return nil, err
  356. }
  357. }
  358. if i > 0 || DefaultHeader {
  359. if _, err := buf.WriteString("[" + sname + "]" + LineBreak); err != nil {
  360. return nil, err
  361. }
  362. } else {
  363. // Write nothing if default section is empty
  364. if len(sec.keyList) == 0 {
  365. continue
  366. }
  367. }
  368. if sec.isRawSection {
  369. if _, err := buf.WriteString(sec.rawBody); err != nil {
  370. return nil, err
  371. }
  372. continue
  373. }
  374. // Count and generate alignment length and buffer spaces using the
  375. // longest key. Keys may be modifed if they contain certain characters so
  376. // we need to take that into account in our calculation.
  377. alignLength := 0
  378. if PrettyFormat {
  379. for _, kname := range sec.keyList {
  380. keyLength := len(kname)
  381. // First case will surround key by ` and second by """
  382. if strings.ContainsAny(kname, "\"=:") {
  383. keyLength += 2
  384. } else if strings.Contains(kname, "`") {
  385. keyLength += 6
  386. }
  387. if keyLength > alignLength {
  388. alignLength = keyLength
  389. }
  390. }
  391. }
  392. alignSpaces := bytes.Repeat([]byte(" "), alignLength)
  393. KEY_LIST:
  394. for _, kname := range sec.keyList {
  395. key := sec.Key(kname)
  396. if len(key.Comment) > 0 {
  397. if len(indent) > 0 && sname != DEFAULT_SECTION {
  398. buf.WriteString(indent)
  399. }
  400. if key.Comment[0] != '#' && key.Comment[0] != ';' {
  401. key.Comment = "; " + key.Comment
  402. }
  403. if _, err := buf.WriteString(key.Comment + LineBreak); err != nil {
  404. return nil, err
  405. }
  406. }
  407. if len(indent) > 0 && sname != DEFAULT_SECTION {
  408. buf.WriteString(indent)
  409. }
  410. switch {
  411. case key.isAutoIncrement:
  412. kname = "-"
  413. case strings.ContainsAny(kname, "\"=:"):
  414. kname = "`" + kname + "`"
  415. case strings.Contains(kname, "`"):
  416. kname = `"""` + kname + `"""`
  417. }
  418. for _, val := range key.ValueWithShadows() {
  419. if _, err := buf.WriteString(kname); err != nil {
  420. return nil, err
  421. }
  422. if key.isBooleanType {
  423. if kname != sec.keyList[len(sec.keyList)-1] {
  424. buf.WriteString(LineBreak)
  425. }
  426. continue KEY_LIST
  427. }
  428. // Write out alignment spaces before "=" sign
  429. if PrettyFormat {
  430. buf.Write(alignSpaces[:alignLength-len(kname)])
  431. }
  432. // In case key value contains "\n", "`", "\"", "#" or ";"
  433. if strings.ContainsAny(val, "\n`") {
  434. val = `"""` + val + `"""`
  435. } else if !f.options.IgnoreInlineComment && strings.ContainsAny(val, "#;") {
  436. val = "`" + val + "`"
  437. }
  438. if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
  439. return nil, err
  440. }
  441. }
  442. }
  443. if PrettySection {
  444. // Put a line between sections
  445. if _, err := buf.WriteString(LineBreak); err != nil {
  446. return nil, err
  447. }
  448. }
  449. }
  450. return buf, nil
  451. }
  452. // WriteToIndent writes content into io.Writer with given indention.
  453. // If PrettyFormat has been set to be true,
  454. // it will align "=" sign with spaces under each section.
  455. func (f *File) WriteToIndent(w io.Writer, indent string) (int64, error) {
  456. buf, err := f.writeToBuffer(indent)
  457. if err != nil {
  458. return 0, err
  459. }
  460. return buf.WriteTo(w)
  461. }
  462. // WriteTo writes file content into io.Writer.
  463. func (f *File) WriteTo(w io.Writer) (int64, error) {
  464. return f.WriteToIndent(w, "")
  465. }
  466. // SaveToIndent writes content to file system with given value indention.
  467. func (f *File) SaveToIndent(filename, indent string) error {
  468. // Note: Because we are truncating with os.Create,
  469. // so it's safer to save to a temporary file location and rename afte done.
  470. buf, err := f.writeToBuffer(indent)
  471. if err != nil {
  472. return err
  473. }
  474. return ioutil.WriteFile(filename, buf.Bytes(), 0666)
  475. }
  476. // SaveTo writes content to file system.
  477. func (f *File) SaveTo(filename string) error {
  478. return f.SaveToIndent(filename, "")
  479. }