-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
139 lines (118 loc) · 4.24 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2014 Charles Gentry All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package gofig
import (
"errors"
"strings"
"strconv"
//"fmt"
)
func conformOption( option string ) string {
// Get rid of spaces surrounding option
option = strings.TrimSpace(option)
optLen := len(option)
// Remove matching quote marks ("......")
if optLen > 0 {
if option[0:1] == "'" || option[0:1] == `"` {
if option[0:1] == option[optLen-1:]{
option = option[1:optLen-1]
}
}
}
return option
}
// DeleteOption will delete an option within a given section. If the section or
// option doesn't exist, the request will be ignored
func (config *Configuration) DeleteOption(sectionName, optionName string) *Configuration {
mm, ok := config.ConfigMap[sectionName]
if ok {
delete(mm, optionName)
}
return config
}
// MergeOptions This will merge a source section into a target section. This is
// normally only used with the cascade-style functions in a section: [A : B : C]
func (config *Configuration) MergeOptions(targetSection, sourceSection string) *Configuration {
ts := config.AddSection(targetSection)
if ss, found := config.GetSection(sourceSection); found {
for key, value := range ss {
ts[key] = value
}
}
return config
}
// SetString will insert an option and value into a section. If the section
// doesn't exist, it will be created
func (config *Configuration) SetString(sectionName, optionName, value string) {
mm := config.AddSection(sectionName)
mm[conformOption(optionName)] = conformOption(value)
}
// IsOption return true if a section and option exists in the config
func (config *Configuration ) IsOption( sectionName, optionName string ) bool {
mm , found := config.ConfigMap[sectionName]
if found {
_ , found = mm[optionName]
}
return found
}
// GetString will search a section for a specific option. If the option
// or section doesn't exist, an error will be returned.
func (config *Configuration) GetString(sectionName, optionName string) (string, error) {
mm, ok := config.GetSection( sectionName )
if !ok {
return "", errors.New("Section '" + sectionName + "' not found")
}
value, ok := mm[optionName]
if !ok {
return "", errors.New("Option '" + optionName + "' not found")
}
return value, nil
}
// GetStringWithDefault will search a section for a specific option. If the
// section or option doesn't exist, a default value (passed into the routine)
// will be returned instead.
func (config *Configuration) GetStringWithDefault(sectionName, optionName, defaultValue string) ( string ) {
value, error := config.GetString(sectionName, optionName)
if error != nil {
config.SetString("_default", sectionName, optionName)
if config.OnDefaultAddToSection {
config.SetString( sectionName , optionName , defaultValue)
}
return defaultValue
}
return value
}
// GetInt will return an int64 value of the number, convertered
func (config *Configuration) GetInt(sectionName , optionName string ) (int64 , error ){
mm , err := config.GetString( sectionName , optionName )
if err != nil {
return 0, err
}
return strconv.ParseInt( mm , 0 , 64 )
}
// GetIntWithDefault will return an int64 or the default value if nothing is available
func (config *Configuration) GetIntWithDefault( sectionName, optionName string , defaultValue int64) (int64 , error){
i := strconv.FormatInt( defaultValue , 10 )
mm := config.GetStringWithDefault( sectionName , optionName , i )
return strconv.ParseInt( mm , 0 , 64 )
}
// GetBool will return an boolean value of the string, converted
// Boolean values must conform to the strconv.ParseBool values (1/0, true/false,etc)
// The values 'yes' and 'no' do not work
func (config *Configuration) GetBool(sectionName , optionName string ) (bool , error ){
mm , err := config.GetString( sectionName , optionName )
if err != nil {
return false, err
}
return strconv.ParseBool( mm )
}
// GetBoolWithDefault will return an bool or the default value if nothing is available
func (config *Configuration) GetBoolWithDefault( sectionName, optionName string , defaultValue bool) (bool , error){
mm , err := config.GetString(sectionName, optionName)
if err != nil {
return defaultValue,nil
}
return strconv.ParseBool( mm )
}