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