// SaveConfigFile save the config into file.
//
// BUG(env): The environment variable config item will be saved with real value in SaveConfigFile Funcation.
SaveConfigFile save the config into file.

The environment variable config item will be saved with real value in SaveConfigFile Funcation.
// SaveConfigFile save the config into file . // // BUG ( env ) : The environment variable config item will be saved with real value in SaveConfigFile Funcation .
func (c *Configer) SaveConfigFile(filename string) (err error) {
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()
	getCommentStr := func(section, key string) string {
		var (
			comment string
			ok      bool
		)
		if len(key) == 0 {
			comment, ok = c.sectionComment[section]
		} else {
			comment, ok = c.keyComment[section+"."+key]
		}
		if ok {
			if len(comment) == 0 || len(strings.TrimSpace(comment)) == 0 {
				return string(bNumComment)
			}
			prefix := string(bNumComment)
			return prefix + strings.Replace(comment, lineBreak, lineBreak+prefix, -1)
		}
		return ""
	}
	buf := bytes.NewBuffer(nil)
	if dt, ok := c.data[defaultSection]; ok {
		for key, val := range dt {
			if key != " " {
				if v := getCommentStr(defaultSection, key); len(v) > 0 {
					if _, err = buf.WriteString(v + lineBreak); err != nil {
						return err
					}
				}
				if _, err = buf.WriteString(key + string(bEqual) + val + lineBreak); err != nil {
					return err
				}
			}
		}
		if _, err = buf.WriteString(lineBreak); err != nil {
			return err
		}
	}
	for section, dt := range c.data {
		if section != defaultSection {
			if v := getCommentStr(section, ""); len(v) > 0 {
				if _, err = buf.WriteString(v + lineBreak); err != nil {
					return err
				}
			}
			if _, err = buf.WriteString(string(sectionStart) + section + string(sectionEnd) + lineBreak); err != nil {
				return err
			}
			for key, val := range dt {
				if key != " " {
					if v := getCommentStr(section, key); len(v) > 0 {
						if _, err = buf.WriteString(v + lineBreak); err != nil {
							return err
						}
					}
					if _, err = buf.WriteString(key + string(bEqual) + val + lineBreak); err != nil {
						return err
					}
				}
			}
			if _, err = buf.WriteString(lineBreak); err != nil {
				return err
			}
		}
	}
	_, err = buf.WriteTo(f)
	return err
}
func ( c * Configer ) SaveConfigFile ( filename string ) ( err error ) { f , err := os . Create ( filename ) if err != nil { return err } defer f . Close ( ) getCommentStr := func ( section , key string ) string { var ( comment string ok bool ) if len ( key ) == 0 { comment , ok = c . sectionComment [ section ] } else { comment , ok = c . keyComment [ section + " " + key ] } if ok { if len ( comment ) == 0 || len ( strings . TrimSpace ( comment ) ) == 0 { return string ( bNumComment ) } prefix := string ( bNumComment ) return prefix + strings . Replace ( comment , lineBreak , lineBreak + prefix , - 1 ) } return " " } buf := bytes . NewBuffer ( nil ) if dt , ok := c . data [ defaultSection ] ; ok { for key , val := range dt { if key != " " { if v := getCommentStr ( defaultSection , key ) ; len ( v ) > 0 { if _ , err = buf . WriteString ( v + lineBreak ) ; err != nil { return err } } if _ , err = buf . WriteString ( key + string ( bEqual ) + val + lineBreak ) ; err != nil { return err } } } if _ , err = buf . WriteString ( lineBreak ) ; err != nil { return err } } for section , dt := range c . data { if section != defaultSection { if v := getCommentStr ( section , " " ) ; len ( v ) > 0 { if _ , err = buf . WriteString ( v + lineBreak ) ; err != nil { return err } } if _ , err = buf . WriteString ( string ( sectionStart ) + section + string ( sectionEnd ) + lineBreak ) ; err != nil { return err } for key , val := range dt { if key != " " { if v := getCommentStr ( section , key ) ; len ( v ) > 0 { if _ , err = buf . WriteString ( v + lineBreak ) ; err != nil { return err } } if _ , err = buf . WriteString ( key + string ( bEqual ) + val + lineBreak ) ; err != nil { return err } } } if _ , err = buf . WriteString ( lineBreak ) ; err != nil { return err } } } _ , err = buf . WriteTo ( f ) return err }
SaveConfigFile save the config into file.
SaveConfigFile save the config into file .
// Write configuration file by filename. // Get section or key comments. Fixed #1607 // Empty comment // Add the line head character "#" // Save default section at first place // Write key comments. // Write key and value. // Put a line between sections. // Save named sections // Write section comments. // Write section name. // Write key comments. // Write key and value. // Put a line between sections.