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

common.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package config
  2. // New creates a new config instance.
  3. func New() *Config {
  4. return &Config{}
  5. }
  6. // Config contains all the sections, comments and includes from a config file.
  7. type Config struct {
  8. Comment *Comment
  9. Sections Sections
  10. Includes Includes
  11. }
  12. // Includes is a list of Includes in a config file.
  13. type Includes []*Include
  14. // Include is a reference to an included config file.
  15. type Include struct {
  16. Path string
  17. Config *Config
  18. }
  19. // Comment string without the prefix '#' or ';'.
  20. type Comment string
  21. const (
  22. // NoSubsection token is passed to Config.Section and Config.SetSection to
  23. // represent the absence of a section.
  24. NoSubsection = ""
  25. )
  26. // Section returns a existing section with the given name or creates a new one.
  27. func (c *Config) Section(name string) *Section {
  28. for i := len(c.Sections) - 1; i >= 0; i-- {
  29. s := c.Sections[i]
  30. if s.IsName(name) {
  31. return s
  32. }
  33. }
  34. s := &Section{Name: name}
  35. c.Sections = append(c.Sections, s)
  36. return s
  37. }
  38. // AddOption adds an option to a given section and subsection. Use the
  39. // NoSubsection constant for the subsection argument if no subsection is wanted.
  40. func (c *Config) AddOption(section string, subsection string, key string, value string) *Config {
  41. if subsection == "" {
  42. c.Section(section).AddOption(key, value)
  43. } else {
  44. c.Section(section).Subsection(subsection).AddOption(key, value)
  45. }
  46. return c
  47. }
  48. // SetOption sets an option to a given section and subsection. Use the
  49. // NoSubsection constant for the subsection argument if no subsection is wanted.
  50. func (c *Config) SetOption(section string, subsection string, key string, value string) *Config {
  51. if subsection == "" {
  52. c.Section(section).SetOption(key, value)
  53. } else {
  54. c.Section(section).Subsection(subsection).SetOption(key, value)
  55. }
  56. return c
  57. }
  58. // RemoveSection removes a section from a config file.
  59. func (c *Config) RemoveSection(name string) *Config {
  60. result := Sections{}
  61. for _, s := range c.Sections {
  62. if !s.IsName(name) {
  63. result = append(result, s)
  64. }
  65. }
  66. c.Sections = result
  67. return c
  68. }
  69. // RemoveSubsection remove s a subsection from a config file.
  70. func (c *Config) RemoveSubsection(section string, subsection string) *Config {
  71. for _, s := range c.Sections {
  72. if s.IsName(section) {
  73. result := Subsections{}
  74. for _, ss := range s.Subsections {
  75. if !ss.IsName(subsection) {
  76. result = append(result, ss)
  77. }
  78. }
  79. s.Subsections = result
  80. }
  81. }
  82. return c
  83. }