ini.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain 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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "bufio"
  17. "bytes"
  18. "errors"
  19. "io"
  20. "io/ioutil"
  21. "os"
  22. "os/user"
  23. "path/filepath"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. )
  28. var (
  29. defaultSection = "default" // default section means if some ini items not in a section, make them in default section,
  30. bNumComment = []byte{'#'} // number signal
  31. bSemComment = []byte{';'} // semicolon signal
  32. bEmpty = []byte{}
  33. bEqual = []byte{'='} // equal signal
  34. bDQuote = []byte{'"'} // quote signal
  35. sectionStart = []byte{'['} // section start signal
  36. sectionEnd = []byte{']'} // section end signal
  37. lineBreak = "\n"
  38. )
  39. // IniConfig implements Config to parse ini file.
  40. type IniConfig struct {
  41. }
  42. // Parse creates a new Config and parses the file configuration from the named file.
  43. func (ini *IniConfig) Parse(name string) (Configer, error) {
  44. return ini.parseFile(name)
  45. }
  46. func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
  47. data, err := ioutil.ReadFile(name)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return ini.parseData(filepath.Dir(name), data)
  52. }
  53. func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, error) {
  54. cfg := &IniConfigContainer{
  55. data: make(map[string]map[string]string),
  56. sectionComment: make(map[string]string),
  57. keyComment: make(map[string]string),
  58. RWMutex: sync.RWMutex{},
  59. }
  60. cfg.Lock()
  61. defer cfg.Unlock()
  62. var comment bytes.Buffer
  63. buf := bufio.NewReader(bytes.NewBuffer(data))
  64. // check the BOM
  65. head, err := buf.Peek(3)
  66. if err == nil && head[0] == 239 && head[1] == 187 && head[2] == 191 {
  67. for i := 1; i <= 3; i++ {
  68. buf.ReadByte()
  69. }
  70. }
  71. section := defaultSection
  72. tmpBuf := bytes.NewBuffer(nil)
  73. for {
  74. tmpBuf.Reset()
  75. shouldBreak := false
  76. for {
  77. tmp, isPrefix, err := buf.ReadLine()
  78. if err == io.EOF {
  79. shouldBreak = true
  80. break
  81. }
  82. //It might be a good idea to throw a error on all unknonw errors?
  83. if _, ok := err.(*os.PathError); ok {
  84. return nil, err
  85. }
  86. tmpBuf.Write(tmp)
  87. if isPrefix {
  88. continue
  89. }
  90. if !isPrefix {
  91. break
  92. }
  93. }
  94. if shouldBreak {
  95. break
  96. }
  97. line := tmpBuf.Bytes()
  98. line = bytes.TrimSpace(line)
  99. if bytes.Equal(line, bEmpty) {
  100. continue
  101. }
  102. var bComment []byte
  103. switch {
  104. case bytes.HasPrefix(line, bNumComment):
  105. bComment = bNumComment
  106. case bytes.HasPrefix(line, bSemComment):
  107. bComment = bSemComment
  108. }
  109. if bComment != nil {
  110. line = bytes.TrimLeft(line, string(bComment))
  111. // Need append to a new line if multi-line comments.
  112. if comment.Len() > 0 {
  113. comment.WriteByte('\n')
  114. }
  115. comment.Write(line)
  116. continue
  117. }
  118. if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) {
  119. section = strings.ToLower(string(line[1 : len(line)-1])) // section name case insensitive
  120. if comment.Len() > 0 {
  121. cfg.sectionComment[section] = comment.String()
  122. comment.Reset()
  123. }
  124. if _, ok := cfg.data[section]; !ok {
  125. cfg.data[section] = make(map[string]string)
  126. }
  127. continue
  128. }
  129. if _, ok := cfg.data[section]; !ok {
  130. cfg.data[section] = make(map[string]string)
  131. }
  132. keyValue := bytes.SplitN(line, bEqual, 2)
  133. key := string(bytes.TrimSpace(keyValue[0])) // key name case insensitive
  134. key = strings.ToLower(key)
  135. // handle include "other.conf"
  136. if len(keyValue) == 1 && strings.HasPrefix(key, "include") {
  137. includefiles := strings.Fields(key)
  138. if includefiles[0] == "include" && len(includefiles) == 2 {
  139. otherfile := strings.Trim(includefiles[1], "\"")
  140. if !filepath.IsAbs(otherfile) {
  141. otherfile = filepath.Join(dir, otherfile)
  142. }
  143. i, err := ini.parseFile(otherfile)
  144. if err != nil {
  145. return nil, err
  146. }
  147. for sec, dt := range i.data {
  148. if _, ok := cfg.data[sec]; !ok {
  149. cfg.data[sec] = make(map[string]string)
  150. }
  151. for k, v := range dt {
  152. cfg.data[sec][k] = v
  153. }
  154. }
  155. for sec, comm := range i.sectionComment {
  156. cfg.sectionComment[sec] = comm
  157. }
  158. for k, comm := range i.keyComment {
  159. cfg.keyComment[k] = comm
  160. }
  161. continue
  162. }
  163. }
  164. if len(keyValue) != 2 {
  165. return nil, errors.New("read the content error: \"" + string(line) + "\", should key = val")
  166. }
  167. val := bytes.TrimSpace(keyValue[1])
  168. if bytes.HasPrefix(val, bDQuote) {
  169. val = bytes.Trim(val, `"`)
  170. }
  171. cfg.data[section][key] = ExpandValueEnv(string(val))
  172. if comment.Len() > 0 {
  173. cfg.keyComment[section+"."+key] = comment.String()
  174. comment.Reset()
  175. }
  176. }
  177. return cfg, nil
  178. }
  179. // ParseData parse ini the data
  180. // When include other.conf,other.conf is either absolute directory
  181. // or under beego in default temporary directory(/tmp/beego[-username]).
  182. func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
  183. dir := "beego"
  184. currentUser, err := user.Current()
  185. if err == nil {
  186. dir = "beego-" + currentUser.Username
  187. }
  188. dir = filepath.Join(os.TempDir(), dir)
  189. if err = os.MkdirAll(dir, os.ModePerm); err != nil {
  190. return nil, err
  191. }
  192. return ini.parseData(dir, data)
  193. }
  194. // IniConfigContainer A Config represents the ini configuration.
  195. // When set and get value, support key as section:name type.
  196. type IniConfigContainer struct {
  197. data map[string]map[string]string // section=> key:val
  198. sectionComment map[string]string // section : comment
  199. keyComment map[string]string // id: []{comment, key...}; id 1 is for main comment.
  200. sync.RWMutex
  201. }
  202. // Bool returns the boolean value for a given key.
  203. func (c *IniConfigContainer) Bool(key string) (bool, error) {
  204. return ParseBool(c.getdata(key))
  205. }
  206. // DefaultBool returns the boolean value for a given key.
  207. // if err != nil return defaultval
  208. func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool {
  209. v, err := c.Bool(key)
  210. if err != nil {
  211. return defaultval
  212. }
  213. return v
  214. }
  215. // Int returns the integer value for a given key.
  216. func (c *IniConfigContainer) Int(key string) (int, error) {
  217. return strconv.Atoi(c.getdata(key))
  218. }
  219. // DefaultInt returns the integer value for a given key.
  220. // if err != nil return defaultval
  221. func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int {
  222. v, err := c.Int(key)
  223. if err != nil {
  224. return defaultval
  225. }
  226. return v
  227. }
  228. // Int64 returns the int64 value for a given key.
  229. func (c *IniConfigContainer) Int64(key string) (int64, error) {
  230. return strconv.ParseInt(c.getdata(key), 10, 64)
  231. }
  232. // DefaultInt64 returns the int64 value for a given key.
  233. // if err != nil return defaultval
  234. func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
  235. v, err := c.Int64(key)
  236. if err != nil {
  237. return defaultval
  238. }
  239. return v
  240. }
  241. // Float returns the float value for a given key.
  242. func (c *IniConfigContainer) Float(key string) (float64, error) {
  243. return strconv.ParseFloat(c.getdata(key), 64)
  244. }
  245. // DefaultFloat returns the float64 value for a given key.
  246. // if err != nil return defaultval
  247. func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
  248. v, err := c.Float(key)
  249. if err != nil {
  250. return defaultval
  251. }
  252. return v
  253. }
  254. // String returns the string value for a given key.
  255. func (c *IniConfigContainer) String(key string) string {
  256. return c.getdata(key)
  257. }
  258. // DefaultString returns the string value for a given key.
  259. // if err != nil return defaultval
  260. func (c *IniConfigContainer) DefaultString(key string, defaultval string) string {
  261. v := c.String(key)
  262. if v == "" {
  263. return defaultval
  264. }
  265. return v
  266. }
  267. // Strings returns the []string value for a given key.
  268. // Return nil if config value does not exist or is empty.
  269. func (c *IniConfigContainer) Strings(key string) []string {
  270. v := c.String(key)
  271. if v == "" {
  272. return nil
  273. }
  274. return strings.Split(v, ";")
  275. }
  276. // DefaultStrings returns the []string value for a given key.
  277. // if err != nil return defaultval
  278. func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string {
  279. v := c.Strings(key)
  280. if v == nil {
  281. return defaultval
  282. }
  283. return v
  284. }
  285. // GetSection returns map for the given section
  286. func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) {
  287. if v, ok := c.data[section]; ok {
  288. return v, nil
  289. }
  290. return nil, errors.New("not exist section")
  291. }
  292. // SaveConfigFile save the config into file.
  293. //
  294. // BUG(env): The environment variable config item will be saved with real value in SaveConfigFile Function.
  295. func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
  296. // Write configuration file by filename.
  297. f, err := os.Create(filename)
  298. if err != nil {
  299. return err
  300. }
  301. defer f.Close()
  302. // Get section or key comments. Fixed #1607
  303. getCommentStr := func(section, key string) string {
  304. var (
  305. comment string
  306. ok bool
  307. )
  308. if len(key) == 0 {
  309. comment, ok = c.sectionComment[section]
  310. } else {
  311. comment, ok = c.keyComment[section+"."+key]
  312. }
  313. if ok {
  314. // Empty comment
  315. if len(comment) == 0 || len(strings.TrimSpace(comment)) == 0 {
  316. return string(bNumComment)
  317. }
  318. prefix := string(bNumComment)
  319. // Add the line head character "#"
  320. return prefix + strings.Replace(comment, lineBreak, lineBreak+prefix, -1)
  321. }
  322. return ""
  323. }
  324. buf := bytes.NewBuffer(nil)
  325. // Save default section at first place
  326. if dt, ok := c.data[defaultSection]; ok {
  327. for key, val := range dt {
  328. if key != " " {
  329. // Write key comments.
  330. if v := getCommentStr(defaultSection, key); len(v) > 0 {
  331. if _, err = buf.WriteString(v + lineBreak); err != nil {
  332. return err
  333. }
  334. }
  335. // Write key and value.
  336. if _, err = buf.WriteString(key + string(bEqual) + val + lineBreak); err != nil {
  337. return err
  338. }
  339. }
  340. }
  341. // Put a line between sections.
  342. if _, err = buf.WriteString(lineBreak); err != nil {
  343. return err
  344. }
  345. }
  346. // Save named sections
  347. for section, dt := range c.data {
  348. if section != defaultSection {
  349. // Write section comments.
  350. if v := getCommentStr(section, ""); len(v) > 0 {
  351. if _, err = buf.WriteString(v + lineBreak); err != nil {
  352. return err
  353. }
  354. }
  355. // Write section name.
  356. if _, err = buf.WriteString(string(sectionStart) + section + string(sectionEnd) + lineBreak); err != nil {
  357. return err
  358. }
  359. for key, val := range dt {
  360. if key != " " {
  361. // Write key comments.
  362. if v := getCommentStr(section, key); len(v) > 0 {
  363. if _, err = buf.WriteString(v + lineBreak); err != nil {
  364. return err
  365. }
  366. }
  367. // Write key and value.
  368. if _, err = buf.WriteString(key + string(bEqual) + val + lineBreak); err != nil {
  369. return err
  370. }
  371. }
  372. }
  373. // Put a line between sections.
  374. if _, err = buf.WriteString(lineBreak); err != nil {
  375. return err
  376. }
  377. }
  378. }
  379. _, err = buf.WriteTo(f)
  380. return err
  381. }
  382. // Set writes a new value for key.
  383. // if write to one section, the key need be "section::key".
  384. // if the section is not existed, it panics.
  385. func (c *IniConfigContainer) Set(key, value string) error {
  386. c.Lock()
  387. defer c.Unlock()
  388. if len(key) == 0 {
  389. return errors.New("key is empty")
  390. }
  391. var (
  392. section, k string
  393. sectionKey = strings.Split(strings.ToLower(key), "::")
  394. )
  395. if len(sectionKey) >= 2 {
  396. section = sectionKey[0]
  397. k = sectionKey[1]
  398. } else {
  399. section = defaultSection
  400. k = sectionKey[0]
  401. }
  402. if _, ok := c.data[section]; !ok {
  403. c.data[section] = make(map[string]string)
  404. }
  405. c.data[section][k] = value
  406. return nil
  407. }
  408. // DIY returns the raw value by a given key.
  409. func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
  410. if v, ok := c.data[strings.ToLower(key)]; ok {
  411. return v, nil
  412. }
  413. return v, errors.New("key not find")
  414. }
  415. // section.key or key
  416. func (c *IniConfigContainer) getdata(key string) string {
  417. if len(key) == 0 {
  418. return ""
  419. }
  420. c.RLock()
  421. defer c.RUnlock()
  422. var (
  423. section, k string
  424. sectionKey = strings.Split(strings.ToLower(key), "::")
  425. )
  426. if len(sectionKey) >= 2 {
  427. section = sectionKey[0]
  428. k = sectionKey[1]
  429. } else {
  430. section = defaultSection
  431. k = sectionKey[0]
  432. }
  433. if v, ok := c.data[section]; ok {
  434. if vv, ok := v[k]; ok {
  435. return vv
  436. }
  437. }
  438. return ""
  439. }
  440. func init() {
  441. Register("ini", &IniConfig{})
  442. }