file.go 13 KB

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