2017-11-14 22:34:42 -05:00
|
|
|
// Copyright 2017 Unknwon
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
|
|
|
package ini
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2020-08-17 23:23:45 -05:00
|
|
|
// File represents a combination of one or more INI files in memory.
|
2017-11-14 22:34:42 -05:00
|
|
|
type File struct {
|
|
|
|
options LoadOptions
|
|
|
|
dataSources []dataSource
|
|
|
|
|
|
|
|
// Should make things safe, but sometimes doesn't matter.
|
|
|
|
BlockMode bool
|
|
|
|
lock sync.RWMutex
|
|
|
|
|
|
|
|
// To keep data in order.
|
|
|
|
sectionList []string
|
2020-08-17 23:23:45 -05:00
|
|
|
// To keep track of the index of a section with same name.
|
|
|
|
// This meta list is only used with non-unique section names are allowed.
|
|
|
|
sectionIndexes []int
|
|
|
|
|
2017-11-14 22:34:42 -05:00
|
|
|
// Actual data is stored here.
|
2020-08-17 23:23:45 -05:00
|
|
|
sections map[string][]*Section
|
2017-11-14 22:34:42 -05:00
|
|
|
|
|
|
|
NameMapper
|
|
|
|
ValueMapper
|
|
|
|
}
|
|
|
|
|
|
|
|
// newFile initializes File object with given data sources.
|
|
|
|
func newFile(dataSources []dataSource, opts LoadOptions) *File {
|
2019-05-13 10:38:53 -05:00
|
|
|
if len(opts.KeyValueDelimiters) == 0 {
|
|
|
|
opts.KeyValueDelimiters = "=:"
|
|
|
|
}
|
2020-08-17 23:23:45 -05:00
|
|
|
if len(opts.KeyValueDelimiterOnWrite) == 0 {
|
|
|
|
opts.KeyValueDelimiterOnWrite = "="
|
|
|
|
}
|
|
|
|
|
2017-11-14 22:34:42 -05:00
|
|
|
return &File{
|
|
|
|
BlockMode: true,
|
|
|
|
dataSources: dataSources,
|
2020-08-17 23:23:45 -05:00
|
|
|
sections: make(map[string][]*Section),
|
2017-11-14 22:34:42 -05:00
|
|
|
options: opts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty returns an empty file object.
|
2020-08-17 23:23:45 -05:00
|
|
|
func Empty(opts ...LoadOptions) *File {
|
|
|
|
var opt LoadOptions
|
|
|
|
if len(opts) > 0 {
|
|
|
|
opt = opts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore error here, we are sure our data is good.
|
|
|
|
f, _ := LoadSources(opt, []byte(""))
|
2017-11-14 22:34:42 -05:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSection creates a new section.
|
|
|
|
func (f *File) NewSection(name string) (*Section, error) {
|
|
|
|
if len(name) == 0 {
|
2020-08-17 23:23:45 -05:00
|
|
|
return nil, errors.New("empty section name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.options.Insensitive && name != DefaultSection {
|
2017-11-14 22:34:42 -05:00
|
|
|
name = strings.ToLower(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.BlockMode {
|
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-08-17 23:23:45 -05:00
|
|
|
if !f.options.AllowNonUniqueSections && inSlice(name, f.sectionList) {
|
|
|
|
return f.sections[name][0], nil
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f.sectionList = append(f.sectionList, name)
|
2020-08-17 23:23:45 -05:00
|
|
|
|
|
|
|
// NOTE: Append to indexes must happen before appending to sections,
|
|
|
|
// otherwise index will have off-by-one problem.
|
|
|
|
f.sectionIndexes = append(f.sectionIndexes, len(f.sections[name]))
|
|
|
|
|
|
|
|
sec := newSection(f, name)
|
|
|
|
f.sections[name] = append(f.sections[name], sec)
|
|
|
|
|
|
|
|
return sec, nil
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRawSection creates a new section with an unparseable body.
|
|
|
|
func (f *File) NewRawSection(name, body string) (*Section, error) {
|
|
|
|
section, err := f.NewSection(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
section.isRawSection = true
|
|
|
|
section.rawBody = body
|
|
|
|
return section, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSections creates a list of sections.
|
|
|
|
func (f *File) NewSections(names ...string) (err error) {
|
|
|
|
for _, name := range names {
|
|
|
|
if _, err = f.NewSection(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSection returns section by given name.
|
|
|
|
func (f *File) GetSection(name string) (*Section, error) {
|
2020-08-17 23:23:45 -05:00
|
|
|
secs, err := f.SectionsByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return secs[0], err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SectionsByName returns all sections with given name.
|
|
|
|
func (f *File) SectionsByName(name string) ([]*Section, error) {
|
2017-11-14 22:34:42 -05:00
|
|
|
if len(name) == 0 {
|
2019-08-23 11:40:30 -05:00
|
|
|
name = DefaultSection
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
if f.options.Insensitive {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.BlockMode {
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
}
|
|
|
|
|
2020-08-17 23:23:45 -05:00
|
|
|
secs := f.sections[name]
|
|
|
|
if len(secs) == 0 {
|
|
|
|
return nil, fmt.Errorf("section %q does not exist", name)
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
2020-08-17 23:23:45 -05:00
|
|
|
|
|
|
|
return secs, nil
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Section assumes named section exists and returns a zero-value when not.
|
|
|
|
func (f *File) Section(name string) *Section {
|
|
|
|
sec, err := f.GetSection(name)
|
|
|
|
if err != nil {
|
|
|
|
// Note: It's OK here because the only possible error is empty section name,
|
|
|
|
// but if it's empty, this piece of code won't be executed.
|
|
|
|
sec, _ = f.NewSection(name)
|
|
|
|
return sec
|
|
|
|
}
|
|
|
|
return sec
|
|
|
|
}
|
|
|
|
|
2020-08-17 23:23:45 -05:00
|
|
|
// SectionWithIndex assumes named section exists and returns a new section when not.
|
|
|
|
func (f *File) SectionWithIndex(name string, index int) *Section {
|
|
|
|
secs, err := f.SectionsByName(name)
|
|
|
|
if err != nil || len(secs) <= index {
|
|
|
|
// NOTE: It's OK here because the only possible error is empty section name,
|
|
|
|
// but if it's empty, this piece of code won't be executed.
|
|
|
|
newSec, _ := f.NewSection(name)
|
|
|
|
return newSec
|
|
|
|
}
|
|
|
|
|
|
|
|
return secs[index]
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:40:30 -05:00
|
|
|
// Sections returns a list of Section stored in the current instance.
|
2017-11-14 22:34:42 -05:00
|
|
|
func (f *File) Sections() []*Section {
|
2019-05-13 10:38:53 -05:00
|
|
|
if f.BlockMode {
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
}
|
|
|
|
|
2017-11-14 22:34:42 -05:00
|
|
|
sections := make([]*Section, len(f.sectionList))
|
2019-05-13 10:38:53 -05:00
|
|
|
for i, name := range f.sectionList {
|
2020-08-17 23:23:45 -05:00
|
|
|
sections[i] = f.sections[name][f.sectionIndexes[i]]
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
return sections
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChildSections returns a list of child sections of given section name.
|
|
|
|
func (f *File) ChildSections(name string) []*Section {
|
|
|
|
return f.Section(name).ChildSections()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SectionStrings returns list of section names.
|
|
|
|
func (f *File) SectionStrings() []string {
|
|
|
|
list := make([]string, len(f.sectionList))
|
|
|
|
copy(list, f.sectionList)
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2020-08-17 23:23:45 -05:00
|
|
|
// DeleteSection deletes a section or all sections with given name.
|
2017-11-14 22:34:42 -05:00
|
|
|
func (f *File) DeleteSection(name string) {
|
2020-08-17 23:23:45 -05:00
|
|
|
secs, err := f.SectionsByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(secs); i++ {
|
|
|
|
// For non-unique sections, it is always needed to remove the first one so
|
|
|
|
// in the next iteration, the subsequent section continue having index 0.
|
|
|
|
// Ignoring the error as index 0 never returns an error.
|
|
|
|
_ = f.DeleteSectionWithIndex(name, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSectionWithIndex deletes a section with given name and index.
|
|
|
|
func (f *File) DeleteSectionWithIndex(name string, index int) error {
|
|
|
|
if !f.options.AllowNonUniqueSections && index != 0 {
|
|
|
|
return fmt.Errorf("delete section with non-zero index is only allowed when non-unique sections is enabled")
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) == 0 {
|
2019-08-23 11:40:30 -05:00
|
|
|
name = DefaultSection
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
2020-08-17 23:23:45 -05:00
|
|
|
if f.options.Insensitive {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.BlockMode {
|
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count occurrences of the sections
|
|
|
|
occurrences := 0
|
|
|
|
|
|
|
|
sectionListCopy := make([]string, len(f.sectionList))
|
|
|
|
copy(sectionListCopy, f.sectionList)
|
|
|
|
|
|
|
|
for i, s := range sectionListCopy {
|
|
|
|
if s != name {
|
|
|
|
continue
|
|
|
|
}
|
2017-11-14 22:34:42 -05:00
|
|
|
|
2020-08-17 23:23:45 -05:00
|
|
|
if occurrences == index {
|
|
|
|
if len(f.sections[name]) <= 1 {
|
|
|
|
delete(f.sections, name) // The last one in the map
|
|
|
|
} else {
|
|
|
|
f.sections[name] = append(f.sections[name][:index], f.sections[name][index+1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix section lists
|
2017-11-14 22:34:42 -05:00
|
|
|
f.sectionList = append(f.sectionList[:i], f.sectionList[i+1:]...)
|
2020-08-17 23:23:45 -05:00
|
|
|
f.sectionIndexes = append(f.sectionIndexes[:i], f.sectionIndexes[i+1:]...)
|
|
|
|
|
|
|
|
} else if occurrences > index {
|
|
|
|
// Fix the indices of all following sections with this name.
|
|
|
|
f.sectionIndexes[i-1]--
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
2020-08-17 23:23:45 -05:00
|
|
|
|
|
|
|
occurrences++
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
2020-08-17 23:23:45 -05:00
|
|
|
|
|
|
|
return nil
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) reload(s dataSource) error {
|
|
|
|
r, err := s.ReadCloser()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
return f.parse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload reloads and parses all data sources.
|
|
|
|
func (f *File) Reload() (err error) {
|
|
|
|
for _, s := range f.dataSources {
|
|
|
|
if err = f.reload(s); err != nil {
|
|
|
|
// In loose mode, we create an empty default section for nonexistent files.
|
|
|
|
if os.IsNotExist(err) && f.options.Loose {
|
2020-08-17 23:23:45 -05:00
|
|
|
_ = f.parse(bytes.NewBuffer(nil))
|
2017-11-14 22:34:42 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append appends one or more data sources and reloads automatically.
|
|
|
|
func (f *File) Append(source interface{}, others ...interface{}) error {
|
|
|
|
ds, err := parseDataSource(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.dataSources = append(f.dataSources, ds)
|
|
|
|
for _, s := range others {
|
|
|
|
ds, err = parseDataSource(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.dataSources = append(f.dataSources, ds)
|
|
|
|
}
|
|
|
|
return f.Reload()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
|
2020-08-17 23:23:45 -05:00
|
|
|
equalSign := DefaultFormatLeft + f.options.KeyValueDelimiterOnWrite + DefaultFormatRight
|
2019-05-13 10:38:53 -05:00
|
|
|
|
|
|
|
if PrettyFormat || PrettyEqual {
|
2020-08-17 23:23:45 -05:00
|
|
|
equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimiterOnWrite)
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use buffer to make sure target is safe until finish encoding.
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
for i, sname := range f.sectionList {
|
2020-08-17 23:23:45 -05:00
|
|
|
sec := f.SectionWithIndex(sname, f.sectionIndexes[i])
|
2017-11-14 22:34:42 -05:00
|
|
|
if len(sec.Comment) > 0 {
|
2019-05-13 10:38:53 -05:00
|
|
|
// Support multiline comments
|
|
|
|
lines := strings.Split(sec.Comment, LineBreak)
|
|
|
|
for i := range lines {
|
|
|
|
if lines[i][0] != '#' && lines[i][0] != ';' {
|
|
|
|
lines[i] = "; " + lines[i]
|
|
|
|
} else {
|
|
|
|
lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 || DefaultHeader {
|
|
|
|
if _, err := buf.WriteString("[" + sname + "]" + LineBreak); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Write nothing if default section is empty
|
|
|
|
if len(sec.keyList) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sec.isRawSection {
|
|
|
|
if _, err := buf.WriteString(sec.rawBody); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if PrettySection {
|
|
|
|
// Put a line between sections
|
|
|
|
if _, err := buf.WriteString(LineBreak); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count and generate alignment length and buffer spaces using the
|
2020-08-17 23:23:45 -05:00
|
|
|
// longest key. Keys may be modified if they contain certain characters so
|
2017-11-14 22:34:42 -05:00
|
|
|
// we need to take that into account in our calculation.
|
|
|
|
alignLength := 0
|
|
|
|
if PrettyFormat {
|
|
|
|
for _, kname := range sec.keyList {
|
|
|
|
keyLength := len(kname)
|
|
|
|
// First case will surround key by ` and second by """
|
2019-05-13 10:38:53 -05:00
|
|
|
if strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters) {
|
2017-11-14 22:34:42 -05:00
|
|
|
keyLength += 2
|
|
|
|
} else if strings.Contains(kname, "`") {
|
|
|
|
keyLength += 6
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyLength > alignLength {
|
|
|
|
alignLength = keyLength
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alignSpaces := bytes.Repeat([]byte(" "), alignLength)
|
|
|
|
|
2019-10-15 10:45:39 -05:00
|
|
|
KeyList:
|
2017-11-14 22:34:42 -05:00
|
|
|
for _, kname := range sec.keyList {
|
|
|
|
key := sec.Key(kname)
|
|
|
|
if len(key.Comment) > 0 {
|
2019-08-23 11:40:30 -05:00
|
|
|
if len(indent) > 0 && sname != DefaultSection {
|
2017-11-14 22:34:42 -05:00
|
|
|
buf.WriteString(indent)
|
|
|
|
}
|
2019-05-13 10:38:53 -05:00
|
|
|
|
|
|
|
// Support multiline comments
|
|
|
|
lines := strings.Split(key.Comment, LineBreak)
|
|
|
|
for i := range lines {
|
|
|
|
if lines[i][0] != '#' && lines[i][0] != ';' {
|
|
|
|
lines[i] = "; " + strings.TrimSpace(lines[i])
|
|
|
|
} else {
|
|
|
|
lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:40:30 -05:00
|
|
|
if len(indent) > 0 && sname != DefaultSection {
|
2017-11-14 22:34:42 -05:00
|
|
|
buf.WriteString(indent)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case key.isAutoIncrement:
|
|
|
|
kname = "-"
|
2019-05-13 10:38:53 -05:00
|
|
|
case strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters):
|
2017-11-14 22:34:42 -05:00
|
|
|
kname = "`" + kname + "`"
|
|
|
|
case strings.Contains(kname, "`"):
|
|
|
|
kname = `"""` + kname + `"""`
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range key.ValueWithShadows() {
|
|
|
|
if _, err := buf.WriteString(kname); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.isBooleanType {
|
|
|
|
if kname != sec.keyList[len(sec.keyList)-1] {
|
|
|
|
buf.WriteString(LineBreak)
|
|
|
|
}
|
2019-10-15 10:45:39 -05:00
|
|
|
continue KeyList
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write out alignment spaces before "=" sign
|
|
|
|
if PrettyFormat {
|
|
|
|
buf.Write(alignSpaces[:alignLength-len(kname)])
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case key value contains "\n", "`", "\"", "#" or ";"
|
|
|
|
if strings.ContainsAny(val, "\n`") {
|
|
|
|
val = `"""` + val + `"""`
|
|
|
|
} else if !f.options.IgnoreInlineComment && strings.ContainsAny(val, "#;") {
|
|
|
|
val = "`" + val + "`"
|
|
|
|
}
|
|
|
|
if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 10:38:53 -05:00
|
|
|
|
|
|
|
for _, val := range key.nestedValues {
|
|
|
|
if _, err := buf.WriteString(indent + " " + val + LineBreak); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 22:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if PrettySection {
|
|
|
|
// Put a line between sections
|
|
|
|
if _, err := buf.WriteString(LineBreak); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToIndent writes content into io.Writer with given indention.
|
|
|
|
// If PrettyFormat has been set to be true,
|
|
|
|
// it will align "=" sign with spaces under each section.
|
|
|
|
func (f *File) WriteToIndent(w io.Writer, indent string) (int64, error) {
|
|
|
|
buf, err := f.writeToBuffer(indent)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return buf.WriteTo(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo writes file content into io.Writer.
|
|
|
|
func (f *File) WriteTo(w io.Writer) (int64, error) {
|
|
|
|
return f.WriteToIndent(w, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveToIndent writes content to file system with given value indention.
|
|
|
|
func (f *File) SaveToIndent(filename, indent string) error {
|
|
|
|
// Note: Because we are truncating with os.Create,
|
|
|
|
// so it's safer to save to a temporary file location and rename afte done.
|
|
|
|
buf, err := f.writeToBuffer(indent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(filename, buf.Bytes(), 0666)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveTo writes content to file system.
|
|
|
|
func (f *File) SaveTo(filename string) error {
|
|
|
|
return f.SaveToIndent(filename, "")
|
|
|
|
}
|