github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goconfig/write.go (about)

     1  // Copyright 2013 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  
    15  package goconfig
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"strings"
    21  )
    22  
    23  // Write spaces around "=" to look better.
    24  var PrettyFormat = true
    25  
    26  // SaveConfigFile writes configuration file to local file system
    27  func SaveConfigFile(c *ConfigFile, filename string) (err error) {
    28  	// Write configuration file by filename.
    29  	var f *os.File
    30  	if f, err = os.Create(filename); err != nil {
    31  		return err
    32  	}
    33  
    34  	equalSign := "="
    35  	if PrettyFormat {
    36  		equalSign = " = "
    37  	}
    38  
    39  	buf := bytes.NewBuffer(nil)
    40  	for _, section := range c.sectionList {
    41  		// Write section comments.
    42  		if len(c.GetSectionComments(section)) > 0 {
    43  			if _, err = buf.WriteString(c.GetSectionComments(section) + LineBreak); err != nil {
    44  				return err
    45  			}
    46  		}
    47  
    48  		if section != DEFAULT_SECTION {
    49  			// Write section name.
    50  			if _, err = buf.WriteString("[" + section + "]" + LineBreak); err != nil {
    51  				return err
    52  			}
    53  		}
    54  
    55  		for _, key := range c.keyList[section] {
    56  			if key != " " {
    57  				// Write key comments.
    58  				if len(c.GetKeyComments(section, key)) > 0 {
    59  					if _, err = buf.WriteString(c.GetKeyComments(section, key) + LineBreak); err != nil {
    60  						return err
    61  					}
    62  				}
    63  
    64  				keyName := key
    65  				// Check if it's auto increment.
    66  				if keyName[0] == '#' {
    67  					keyName = "-"
    68  				}
    69  				//[SWH|+]:支持键名包含等号和冒号
    70  				if strings.Contains(keyName, `=`) || strings.Contains(keyName, `:`) {
    71  					if strings.Contains(keyName, "`") {
    72  						if strings.Contains(keyName, `"`) {
    73  							keyName = `"""` + keyName + `"""`
    74  						} else {
    75  							keyName = `"` + keyName + `"`
    76  						}
    77  					} else {
    78  						keyName = "`" + keyName + "`"
    79  					}
    80  				}
    81  				value := c.data[section][key]
    82  				// In case key value contains "`" or "\"".
    83  				if strings.Contains(value, "`") {
    84  					if strings.Contains(value, `"`) {
    85  						value = `"""` + value + `"""`
    86  					} else {
    87  						value = `"` + value + `"`
    88  					}
    89  				}
    90  
    91  				// Write key and value.
    92  				if _, err = buf.WriteString(keyName + equalSign + value + LineBreak); err != nil {
    93  					return err
    94  				}
    95  			}
    96  		}
    97  
    98  		// Put a line between sections.
    99  		if _, err = buf.WriteString(LineBreak); err != nil {
   100  			return err
   101  		}
   102  	}
   103  
   104  	if _, err = buf.WriteTo(f); err != nil {
   105  		return err
   106  	}
   107  	return f.Close()
   108  }