ini.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. "strconv"
  26. "strings"
  27. "sync"
  28. "time"
  29. )
  30. const (
  31. // Name for default section. You can use this constant or the string literal.
  32. // In most of cases, an empty string is all you need to access the section.
  33. DEFAULT_SECTION = "DEFAULT"
  34. // Maximum allowed depth when recursively substituing variable names.
  35. _DEPTH_VALUES = 99
  36. _VERSION = "1.24.0"
  37. )
  38. // Version returns current package version literal.
  39. func Version() string {
  40. return _VERSION
  41. }
  42. var (
  43. // Delimiter to determine or compose a new line.
  44. // This variable will be changed to "\r\n" automatically on Windows
  45. // at package init time.
  46. LineBreak = "\n"
  47. // Variable regexp pattern: %(variable)s
  48. varPattern = regexp.MustCompile(`%\(([^\)]+)\)s`)
  49. // Indicate whether to align "=" sign with spaces to produce pretty output
  50. // or reduce all possible spaces for compact format.
  51. PrettyFormat = true
  52. // Explicitly write DEFAULT section header
  53. DefaultHeader = false
  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. // AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
  148. // This type of keys are mostly used in my.cnf.
  149. AllowBooleanKeys bool
  150. // Some INI formats allow group blocks that store a block of raw content that doesn't otherwise
  151. // conform to key/value pairs. Specify the names of those blocks here.
  152. UnparseableSections []string
  153. }
  154. func LoadSources(opts LoadOptions, source interface{}, others ...interface{}) (_ *File, err error) {
  155. sources := make([]dataSource, len(others)+1)
  156. sources[0], err = parseDataSource(source)
  157. if err != nil {
  158. return nil, err
  159. }
  160. for i := range others {
  161. sources[i+1], err = parseDataSource(others[i])
  162. if err != nil {
  163. return nil, err
  164. }
  165. }
  166. f := newFile(sources, opts)
  167. if err = f.Reload(); err != nil {
  168. return nil, err
  169. }
  170. return f, nil
  171. }
  172. // Load loads and parses from INI data sources.
  173. // Arguments can be mixed of file name with string type, or raw data in []byte.
  174. // It will return error if list contains nonexistent files.
  175. func Load(source interface{}, others ...interface{}) (*File, error) {
  176. return LoadSources(LoadOptions{}, source, others...)
  177. }
  178. // LooseLoad has exactly same functionality as Load function
  179. // except it ignores nonexistent files instead of returning error.
  180. func LooseLoad(source interface{}, others ...interface{}) (*File, error) {
  181. return LoadSources(LoadOptions{Loose: true}, source, others...)
  182. }
  183. // InsensitiveLoad has exactly same functionality as Load function
  184. // except it forces all section and key names to be lowercased.
  185. func InsensitiveLoad(source interface{}, others ...interface{}) (*File, error) {
  186. return LoadSources(LoadOptions{Insensitive: true}, source, others...)
  187. }
  188. // Empty returns an empty file object.
  189. func Empty() *File {
  190. // Ignore error here, we sure our data is good.
  191. f, _ := Load([]byte(""))
  192. return f
  193. }
  194. // NewSection creates a new section.
  195. func (f *File) NewSection(name string) (*Section, error) {
  196. if len(name) == 0 {
  197. return nil, errors.New("error creating new section: empty section name")
  198. } else if f.options.Insensitive && name != DEFAULT_SECTION {
  199. name = strings.ToLower(name)
  200. }
  201. if f.BlockMode {
  202. f.lock.Lock()
  203. defer f.lock.Unlock()
  204. }
  205. if inSlice(name, f.sectionList) {
  206. return f.sections[name], nil
  207. }
  208. f.sectionList = append(f.sectionList, name)
  209. f.sections[name] = newSection(f, name)
  210. return f.sections[name], nil
  211. }
  212. // NewRawSection creates a new section with an unparseable body.
  213. func (f *File) NewRawSection(name, body string) (*Section, error) {
  214. section, err := f.NewSection(name)
  215. if err != nil {
  216. return nil, err
  217. }
  218. section.isRawSection = true
  219. section.rawBody = body
  220. return section, nil
  221. }
  222. // NewSections creates a list of sections.
  223. func (f *File) NewSections(names ...string) (err error) {
  224. for _, name := range names {
  225. if _, err = f.NewSection(name); err != nil {
  226. return err
  227. }
  228. }
  229. return nil
  230. }
  231. // GetSection returns section by given name.
  232. func (f *File) GetSection(name string) (*Section, error) {
  233. if len(name) == 0 {
  234. name = DEFAULT_SECTION
  235. } else if f.options.Insensitive {
  236. name = strings.ToLower(name)
  237. }
  238. if f.BlockMode {
  239. f.lock.RLock()
  240. defer f.lock.RUnlock()
  241. }
  242. sec := f.sections[name]
  243. if sec == nil {
  244. return nil, fmt.Errorf("section '%s' does not exist", name)
  245. }
  246. return sec, nil
  247. }
  248. // Section assumes named section exists and returns a zero-value when not.
  249. func (f *File) Section(name string) *Section {
  250. sec, err := f.GetSection(name)
  251. if err != nil {
  252. // Note: It's OK here because the only possible error is empty section name,
  253. // but if it's empty, this piece of code won't be executed.
  254. sec, _ = f.NewSection(name)
  255. return sec
  256. }
  257. return sec
  258. }
  259. // Section returns list of Section.
  260. func (f *File) Sections() []*Section {
  261. sections := make([]*Section, len(f.sectionList))
  262. for i := range f.sectionList {
  263. sections[i] = f.Section(f.sectionList[i])
  264. }
  265. return sections
  266. }
  267. // SectionStrings returns list of section names.
  268. func (f *File) SectionStrings() []string {
  269. list := make([]string, len(f.sectionList))
  270. copy(list, f.sectionList)
  271. return list
  272. }
  273. // DeleteSection deletes a section.
  274. func (f *File) DeleteSection(name string) {
  275. if f.BlockMode {
  276. f.lock.Lock()
  277. defer f.lock.Unlock()
  278. }
  279. if len(name) == 0 {
  280. name = DEFAULT_SECTION
  281. }
  282. for i, s := range f.sectionList {
  283. if s == name {
  284. f.sectionList = append(f.sectionList[:i], f.sectionList[i+1:]...)
  285. delete(f.sections, name)
  286. return
  287. }
  288. }
  289. }
  290. func (f *File) reload(s dataSource) error {
  291. r, err := s.ReadCloser()
  292. if err != nil {
  293. return err
  294. }
  295. defer r.Close()
  296. return f.parse(r)
  297. }
  298. // Reload reloads and parses all data sources.
  299. func (f *File) Reload() (err error) {
  300. for _, s := range f.dataSources {
  301. if err = f.reload(s); err != nil {
  302. // In loose mode, we create an empty default section for nonexistent files.
  303. if os.IsNotExist(err) && f.options.Loose {
  304. f.parse(bytes.NewBuffer(nil))
  305. continue
  306. }
  307. return err
  308. }
  309. }
  310. return nil
  311. }
  312. // Append appends one or more data sources and reloads automatically.
  313. func (f *File) Append(source interface{}, others ...interface{}) error {
  314. ds, err := parseDataSource(source)
  315. if err != nil {
  316. return err
  317. }
  318. f.dataSources = append(f.dataSources, ds)
  319. for _, s := range others {
  320. ds, err = parseDataSource(s)
  321. if err != nil {
  322. return err
  323. }
  324. f.dataSources = append(f.dataSources, ds)
  325. }
  326. return f.Reload()
  327. }
  328. // WriteToIndent writes content into io.Writer with given indention.
  329. // If PrettyFormat has been set to be true,
  330. // it will align "=" sign with spaces under each section.
  331. func (f *File) WriteToIndent(w io.Writer, indent string) (n int64, err error) {
  332. equalSign := "="
  333. if PrettyFormat {
  334. equalSign = " = "
  335. }
  336. // Use buffer to make sure target is safe until finish encoding.
  337. buf := bytes.NewBuffer(nil)
  338. for i, sname := range f.sectionList {
  339. sec := f.Section(sname)
  340. if len(sec.Comment) > 0 {
  341. if sec.Comment[0] != '#' && sec.Comment[0] != ';' {
  342. sec.Comment = "; " + sec.Comment
  343. }
  344. if _, err = buf.WriteString(sec.Comment + LineBreak); err != nil {
  345. return 0, err
  346. }
  347. }
  348. if i > 0 || DefaultHeader {
  349. if _, err = buf.WriteString("[" + sname + "]" + LineBreak); err != nil {
  350. return 0, err
  351. }
  352. } else {
  353. // Write nothing if default section is empty
  354. if len(sec.keyList) == 0 {
  355. continue
  356. }
  357. }
  358. if sec.isRawSection {
  359. if _, err = buf.WriteString(sec.rawBody); err != nil {
  360. return 0, err
  361. }
  362. continue
  363. }
  364. // Count and generate alignment length and buffer spaces using the
  365. // longest key. Keys may be modifed if they contain certain characters so
  366. // we need to take that into account in our calculation.
  367. alignLength := 0
  368. if PrettyFormat {
  369. for _, kname := range sec.keyList {
  370. keyLength := len(kname)
  371. // First case will surround key by ` and second by """
  372. if strings.ContainsAny(kname, "\"=:") {
  373. keyLength += 2
  374. } else if strings.Contains(kname, "`") {
  375. keyLength += 6
  376. }
  377. if keyLength > alignLength {
  378. alignLength = keyLength
  379. }
  380. }
  381. }
  382. alignSpaces := bytes.Repeat([]byte(" "), alignLength)
  383. for _, kname := range sec.keyList {
  384. key := sec.Key(kname)
  385. if len(key.Comment) > 0 {
  386. if len(indent) > 0 && sname != DEFAULT_SECTION {
  387. buf.WriteString(indent)
  388. }
  389. if key.Comment[0] != '#' && key.Comment[0] != ';' {
  390. key.Comment = "; " + key.Comment
  391. }
  392. if _, err = buf.WriteString(key.Comment + LineBreak); err != nil {
  393. return 0, err
  394. }
  395. }
  396. if len(indent) > 0 && sname != DEFAULT_SECTION {
  397. buf.WriteString(indent)
  398. }
  399. switch {
  400. case key.isAutoIncrement:
  401. kname = "-"
  402. case strings.ContainsAny(kname, "\"=:"):
  403. kname = "`" + kname + "`"
  404. case strings.Contains(kname, "`"):
  405. kname = `"""` + kname + `"""`
  406. }
  407. if _, err = buf.WriteString(kname); err != nil {
  408. return 0, err
  409. }
  410. if key.isBooleanType {
  411. if kname != sec.keyList[len(sec.keyList)-1] {
  412. buf.WriteString(LineBreak)
  413. }
  414. continue
  415. }
  416. // Write out alignment spaces before "=" sign
  417. if PrettyFormat {
  418. buf.Write(alignSpaces[:alignLength-len(kname)])
  419. }
  420. val := key.value
  421. // In case key value contains "\n", "`", "\"", "#" or ";"
  422. if strings.ContainsAny(val, "\n`") {
  423. val = `"""` + val + `"""`
  424. } else if strings.ContainsAny(val, "#;") {
  425. val = "`" + val + "`"
  426. }
  427. if _, err = buf.WriteString(equalSign + val + LineBreak); err != nil {
  428. return 0, err
  429. }
  430. }
  431. // Put a line between sections
  432. if _, err = buf.WriteString(LineBreak); err != nil {
  433. return 0, err
  434. }
  435. }
  436. return buf.WriteTo(w)
  437. }
  438. // WriteTo writes file content into io.Writer.
  439. func (f *File) WriteTo(w io.Writer) (int64, error) {
  440. return f.WriteToIndent(w, "")
  441. }
  442. // SaveToIndent writes content to file system with given value indention.
  443. func (f *File) SaveToIndent(filename, indent string) error {
  444. // Note: Because we are truncating with os.Create,
  445. // so it's safer to save to a temporary file location and rename afte done.
  446. tmpPath := filename + "." + strconv.Itoa(time.Now().Nanosecond()) + ".tmp"
  447. defer os.Remove(tmpPath)
  448. fw, err := os.Create(tmpPath)
  449. if err != nil {
  450. return err
  451. }
  452. if _, err = f.WriteToIndent(fw, indent); err != nil {
  453. fw.Close()
  454. return err
  455. }
  456. fw.Close()
  457. // Remove old file and rename the new one.
  458. os.Remove(filename)
  459. return os.Rename(tmpPath, filename)
  460. }
  461. // SaveTo writes content to file system.
  462. func (f *File) SaveTo(filename string) error {
  463. return f.SaveToIndent(filename, "")
  464. }