file.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // Copyright 2017 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
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "os"
  22. "strings"
  23. "sync"
  24. )
  25. // File represents a combination of one or more INI files in memory.
  26. type File struct {
  27. options LoadOptions
  28. dataSources []dataSource
  29. // Should make things safe, but sometimes doesn't matter.
  30. BlockMode bool
  31. lock sync.RWMutex
  32. // To keep data in order.
  33. sectionList []string
  34. // To keep track of the index of a section with same name.
  35. // This meta list is only used with non-unique section names are allowed.
  36. sectionIndexes []int
  37. // Actual data is stored here.
  38. sections map[string][]*Section
  39. NameMapper
  40. ValueMapper
  41. }
  42. // newFile initializes File object with given data sources.
  43. func newFile(dataSources []dataSource, opts LoadOptions) *File {
  44. if len(opts.KeyValueDelimiters) == 0 {
  45. opts.KeyValueDelimiters = "=:"
  46. }
  47. if len(opts.KeyValueDelimiterOnWrite) == 0 {
  48. opts.KeyValueDelimiterOnWrite = "="
  49. }
  50. if len(opts.ChildSectionDelimiter) == 0 {
  51. opts.ChildSectionDelimiter = "."
  52. }
  53. return &File{
  54. BlockMode: true,
  55. dataSources: dataSources,
  56. sections: make(map[string][]*Section),
  57. options: opts,
  58. }
  59. }
  60. // Empty returns an empty file object.
  61. func Empty(opts ...LoadOptions) *File {
  62. var opt LoadOptions
  63. if len(opts) > 0 {
  64. opt = opts[0]
  65. }
  66. // Ignore error here, we are sure our data is good.
  67. f, _ := LoadSources(opt, []byte(""))
  68. return f
  69. }
  70. // NewSection creates a new section.
  71. func (f *File) NewSection(name string) (*Section, error) {
  72. if len(name) == 0 {
  73. return nil, errors.New("empty section name")
  74. }
  75. if (f.options.Insensitive || f.options.InsensitiveSections) && name != DefaultSection {
  76. name = strings.ToLower(name)
  77. }
  78. if f.BlockMode {
  79. f.lock.Lock()
  80. defer f.lock.Unlock()
  81. }
  82. if !f.options.AllowNonUniqueSections && inSlice(name, f.sectionList) {
  83. return f.sections[name][0], nil
  84. }
  85. f.sectionList = append(f.sectionList, name)
  86. // NOTE: Append to indexes must happen before appending to sections,
  87. // otherwise index will have off-by-one problem.
  88. f.sectionIndexes = append(f.sectionIndexes, len(f.sections[name]))
  89. sec := newSection(f, name)
  90. f.sections[name] = append(f.sections[name], sec)
  91. return sec, nil
  92. }
  93. // NewRawSection creates a new section with an unparseable body.
  94. func (f *File) NewRawSection(name, body string) (*Section, error) {
  95. section, err := f.NewSection(name)
  96. if err != nil {
  97. return nil, err
  98. }
  99. section.isRawSection = true
  100. section.rawBody = body
  101. return section, nil
  102. }
  103. // NewSections creates a list of sections.
  104. func (f *File) NewSections(names ...string) (err error) {
  105. for _, name := range names {
  106. if _, err = f.NewSection(name); err != nil {
  107. return err
  108. }
  109. }
  110. return nil
  111. }
  112. // GetSection returns section by given name.
  113. func (f *File) GetSection(name string) (*Section, error) {
  114. secs, err := f.SectionsByName(name)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return secs[0], err
  119. }
  120. // SectionsByName returns all sections with given name.
  121. func (f *File) SectionsByName(name string) ([]*Section, error) {
  122. if len(name) == 0 {
  123. name = DefaultSection
  124. }
  125. if f.options.Insensitive || f.options.InsensitiveSections {
  126. name = strings.ToLower(name)
  127. }
  128. if f.BlockMode {
  129. f.lock.RLock()
  130. defer f.lock.RUnlock()
  131. }
  132. secs := f.sections[name]
  133. if len(secs) == 0 {
  134. return nil, fmt.Errorf("section %q does not exist", name)
  135. }
  136. return secs, nil
  137. }
  138. // Section assumes named section exists and returns a zero-value when not.
  139. func (f *File) Section(name string) *Section {
  140. sec, err := f.GetSection(name)
  141. if err != nil {
  142. // Note: It's OK here because the only possible error is empty section name,
  143. // but if it's empty, this piece of code won't be executed.
  144. sec, _ = f.NewSection(name)
  145. return sec
  146. }
  147. return sec
  148. }
  149. // SectionWithIndex assumes named section exists and returns a new section when not.
  150. func (f *File) SectionWithIndex(name string, index int) *Section {
  151. secs, err := f.SectionsByName(name)
  152. if err != nil || len(secs) <= index {
  153. // NOTE: It's OK here because the only possible error is empty section name,
  154. // but if it's empty, this piece of code won't be executed.
  155. newSec, _ := f.NewSection(name)
  156. return newSec
  157. }
  158. return secs[index]
  159. }
  160. // Sections returns a list of Section stored in the current instance.
  161. func (f *File) Sections() []*Section {
  162. if f.BlockMode {
  163. f.lock.RLock()
  164. defer f.lock.RUnlock()
  165. }
  166. sections := make([]*Section, len(f.sectionList))
  167. for i, name := range f.sectionList {
  168. sections[i] = f.sections[name][f.sectionIndexes[i]]
  169. }
  170. return sections
  171. }
  172. // ChildSections returns a list of child sections of given section name.
  173. func (f *File) ChildSections(name string) []*Section {
  174. return f.Section(name).ChildSections()
  175. }
  176. // SectionStrings returns list of section names.
  177. func (f *File) SectionStrings() []string {
  178. list := make([]string, len(f.sectionList))
  179. copy(list, f.sectionList)
  180. return list
  181. }
  182. // DeleteSection deletes a section or all sections with given name.
  183. func (f *File) DeleteSection(name string) {
  184. secs, err := f.SectionsByName(name)
  185. if err != nil {
  186. return
  187. }
  188. for i := 0; i < len(secs); i++ {
  189. // For non-unique sections, it is always needed to remove the first one so
  190. // in the next iteration, the subsequent section continue having index 0.
  191. // Ignoring the error as index 0 never returns an error.
  192. _ = f.DeleteSectionWithIndex(name, 0)
  193. }
  194. }
  195. // DeleteSectionWithIndex deletes a section with given name and index.
  196. func (f *File) DeleteSectionWithIndex(name string, index int) error {
  197. if !f.options.AllowNonUniqueSections && index != 0 {
  198. return fmt.Errorf("delete section with non-zero index is only allowed when non-unique sections is enabled")
  199. }
  200. if len(name) == 0 {
  201. name = DefaultSection
  202. }
  203. if f.options.Insensitive || f.options.InsensitiveSections {
  204. name = strings.ToLower(name)
  205. }
  206. if f.BlockMode {
  207. f.lock.Lock()
  208. defer f.lock.Unlock()
  209. }
  210. // Count occurrences of the sections
  211. occurrences := 0
  212. sectionListCopy := make([]string, len(f.sectionList))
  213. copy(sectionListCopy, f.sectionList)
  214. for i, s := range sectionListCopy {
  215. if s != name {
  216. continue
  217. }
  218. if occurrences == index {
  219. if len(f.sections[name]) <= 1 {
  220. delete(f.sections, name) // The last one in the map
  221. } else {
  222. f.sections[name] = append(f.sections[name][:index], f.sections[name][index+1:]...)
  223. }
  224. // Fix section lists
  225. f.sectionList = append(f.sectionList[:i], f.sectionList[i+1:]...)
  226. f.sectionIndexes = append(f.sectionIndexes[:i], f.sectionIndexes[i+1:]...)
  227. } else if occurrences > index {
  228. // Fix the indices of all following sections with this name.
  229. f.sectionIndexes[i-1]--
  230. }
  231. occurrences++
  232. }
  233. return nil
  234. }
  235. func (f *File) reload(s dataSource) error {
  236. r, err := s.ReadCloser()
  237. if err != nil {
  238. return err
  239. }
  240. defer r.Close()
  241. return f.parse(r)
  242. }
  243. // Reload reloads and parses all data sources.
  244. func (f *File) Reload() (err error) {
  245. for _, s := range f.dataSources {
  246. if err = f.reload(s); err != nil {
  247. // In loose mode, we create an empty default section for nonexistent files.
  248. if os.IsNotExist(err) && f.options.Loose {
  249. _ = f.parse(bytes.NewBuffer(nil))
  250. continue
  251. }
  252. return err
  253. }
  254. if f.options.ShortCircuit {
  255. return nil
  256. }
  257. }
  258. return nil
  259. }
  260. // Append appends one or more data sources and reloads automatically.
  261. func (f *File) Append(source interface{}, others ...interface{}) error {
  262. ds, err := parseDataSource(source)
  263. if err != nil {
  264. return err
  265. }
  266. f.dataSources = append(f.dataSources, ds)
  267. for _, s := range others {
  268. ds, err = parseDataSource(s)
  269. if err != nil {
  270. return err
  271. }
  272. f.dataSources = append(f.dataSources, ds)
  273. }
  274. return f.Reload()
  275. }
  276. func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
  277. equalSign := DefaultFormatLeft + f.options.KeyValueDelimiterOnWrite + DefaultFormatRight
  278. if PrettyFormat || PrettyEqual {
  279. equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimiterOnWrite)
  280. }
  281. // Use buffer to make sure target is safe until finish encoding.
  282. buf := bytes.NewBuffer(nil)
  283. for i, sname := range f.sectionList {
  284. sec := f.SectionWithIndex(sname, f.sectionIndexes[i])
  285. if len(sec.Comment) > 0 {
  286. // Support multiline comments
  287. lines := strings.Split(sec.Comment, LineBreak)
  288. for i := range lines {
  289. if lines[i][0] != '#' && lines[i][0] != ';' {
  290. lines[i] = "; " + lines[i]
  291. } else {
  292. lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
  293. }
  294. if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
  295. return nil, err
  296. }
  297. }
  298. }
  299. if i > 0 || DefaultHeader || (i == 0 && strings.ToUpper(sec.name) != DefaultSection) {
  300. if _, err := buf.WriteString("[" + sname + "]" + LineBreak); err != nil {
  301. return nil, err
  302. }
  303. } else {
  304. // Write nothing if default section is empty
  305. if len(sec.keyList) == 0 {
  306. continue
  307. }
  308. }
  309. if sec.isRawSection {
  310. if _, err := buf.WriteString(sec.rawBody); err != nil {
  311. return nil, err
  312. }
  313. if PrettySection {
  314. // Put a line between sections
  315. if _, err := buf.WriteString(LineBreak); err != nil {
  316. return nil, err
  317. }
  318. }
  319. continue
  320. }
  321. // Count and generate alignment length and buffer spaces using the
  322. // longest key. Keys may be modified if they contain certain characters so
  323. // we need to take that into account in our calculation.
  324. alignLength := 0
  325. if PrettyFormat {
  326. for _, kname := range sec.keyList {
  327. keyLength := len(kname)
  328. // First case will surround key by ` and second by """
  329. if strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters) {
  330. keyLength += 2
  331. } else if strings.Contains(kname, "`") {
  332. keyLength += 6
  333. }
  334. if keyLength > alignLength {
  335. alignLength = keyLength
  336. }
  337. }
  338. }
  339. alignSpaces := bytes.Repeat([]byte(" "), alignLength)
  340. KeyList:
  341. for _, kname := range sec.keyList {
  342. key := sec.Key(kname)
  343. if len(key.Comment) > 0 {
  344. if len(indent) > 0 && sname != DefaultSection {
  345. buf.WriteString(indent)
  346. }
  347. // Support multiline comments
  348. lines := strings.Split(key.Comment, LineBreak)
  349. for i := range lines {
  350. if lines[i][0] != '#' && lines[i][0] != ';' {
  351. lines[i] = "; " + strings.TrimSpace(lines[i])
  352. } else {
  353. lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
  354. }
  355. if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
  356. return nil, err
  357. }
  358. }
  359. }
  360. if len(indent) > 0 && sname != DefaultSection {
  361. buf.WriteString(indent)
  362. }
  363. switch {
  364. case key.isAutoIncrement:
  365. kname = "-"
  366. case strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters):
  367. kname = "`" + kname + "`"
  368. case strings.Contains(kname, "`"):
  369. kname = `"""` + kname + `"""`
  370. }
  371. for _, val := range key.ValueWithShadows() {
  372. if _, err := buf.WriteString(kname); err != nil {
  373. return nil, err
  374. }
  375. if key.isBooleanType {
  376. if kname != sec.keyList[len(sec.keyList)-1] {
  377. buf.WriteString(LineBreak)
  378. }
  379. continue KeyList
  380. }
  381. // Write out alignment spaces before "=" sign
  382. if PrettyFormat {
  383. buf.Write(alignSpaces[:alignLength-len(kname)])
  384. }
  385. // In case key value contains "\n", "`", "\"", "#" or ";"
  386. if strings.ContainsAny(val, "\n`") {
  387. val = `"""` + val + `"""`
  388. } else if !f.options.IgnoreInlineComment && strings.ContainsAny(val, "#;") {
  389. val = "`" + val + "`"
  390. } else if len(strings.TrimSpace(val)) != len(val) {
  391. val = `"` + val + `"`
  392. }
  393. if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
  394. return nil, err
  395. }
  396. }
  397. for _, val := range key.nestedValues {
  398. if _, err := buf.WriteString(indent + " " + val + LineBreak); err != nil {
  399. return nil, err
  400. }
  401. }
  402. }
  403. if PrettySection {
  404. // Put a line between sections
  405. if _, err := buf.WriteString(LineBreak); err != nil {
  406. return nil, err
  407. }
  408. }
  409. }
  410. return buf, nil
  411. }
  412. // WriteToIndent writes content into io.Writer with given indention.
  413. // If PrettyFormat has been set to be true,
  414. // it will align "=" sign with spaces under each section.
  415. func (f *File) WriteToIndent(w io.Writer, indent string) (int64, error) {
  416. buf, err := f.writeToBuffer(indent)
  417. if err != nil {
  418. return 0, err
  419. }
  420. return buf.WriteTo(w)
  421. }
  422. // WriteTo writes file content into io.Writer.
  423. func (f *File) WriteTo(w io.Writer) (int64, error) {
  424. return f.WriteToIndent(w, "")
  425. }
  426. // SaveToIndent writes content to file system with given value indention.
  427. func (f *File) SaveToIndent(filename, indent string) error {
  428. // Note: Because we are truncating with os.Create,
  429. // so it's safer to save to a temporary file location and rename after done.
  430. buf, err := f.writeToBuffer(indent)
  431. if err != nil {
  432. return err
  433. }
  434. return ioutil.WriteFile(filename, buf.Bytes(), 0666)
  435. }
  436. // SaveTo writes content to file system.
  437. func (f *File) SaveTo(filename string) error {
  438. return f.SaveToIndent(filename, "")
  439. }