~ubuntu-branches/ubuntu/utopic/hockeypuck/utopic-proposed

« back to all changes in this revision

Viewing changes to config.go

  • Committer: Package Import Robot
  • Author(s): Casey Marshall
  • Date: 2014-04-13 20:06:01 UTC
  • Revision ID: package-import@ubuntu.com-20140413200601-oxdlqn1gy0x8m55u
Tags: 1.0~rel20140413+7a1892a~trusty
Hockeypuck 1.0 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Hockeypuck - OpenPGP key server
 
3
   Copyright (C) 2012-2014  Casey Marshall
 
4
 
 
5
   This program is free software: you can redistribute it and/or modify
 
6
   it under the terms of the GNU Affero General Public License as published by
 
7
   the Free Software Foundation, version 3.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU Affero General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Affero General Public License
 
15
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
// Package hockeypuck provides common configuration, logging and
 
19
// static content for the keyserver.
 
20
package hockeypuck
 
21
 
 
22
import (
 
23
        "bytes"
 
24
        "fmt"
 
25
        "io"
 
26
        "strconv"
 
27
 
 
28
        "github.com/pelletier/go-toml"
 
29
)
 
30
 
 
31
var config *Settings
 
32
 
 
33
// Config returns the global Settings for an application built with Hockeypuck.
 
34
func Config() *Settings {
 
35
        return config
 
36
}
 
37
 
 
38
// Settings stores configuration options for Hockeypuck.
 
39
type Settings struct {
 
40
        *toml.TomlTree
 
41
}
 
42
 
 
43
// GetString returns the string value for the configuration key if set,
 
44
// otherwise the empty string.
 
45
func (s *Settings) GetString(key string) string {
 
46
        return s.GetStringDefault(key, "")
 
47
}
 
48
 
 
49
// GetStringDefault returns the string value for the configuration key if set,
 
50
// otherwise the default value.
 
51
func (s *Settings) GetStringDefault(key string, defaultValue string) string {
 
52
        if s, is := s.Get(key).(string); is {
 
53
                return s
 
54
        }
 
55
        return defaultValue
 
56
}
 
57
 
 
58
// MustGetInt returns the int value for the configuration key if set and valid,
 
59
// otherwise panics.
 
60
func (s *Settings) MustGetInt(key string) int {
 
61
        if v, err := s.getInt(key); err == nil {
 
62
                return v
 
63
        } else {
 
64
                panic(err)
 
65
        }
 
66
}
 
67
 
 
68
// GetIntDefault returns the int value for the configuration key if set and valid,
 
69
// otherwise the default value.
 
70
func (s *Settings) GetIntDefault(key string, defaultValue int) int {
 
71
        if v, err := s.getInt(key); err == nil {
 
72
                return v
 
73
        } else {
 
74
                return defaultValue
 
75
        }
 
76
}
 
77
 
 
78
func (s *Settings) getInt(key string) (int, error) {
 
79
        switch v := s.Get(key).(type) {
 
80
        case int:
 
81
                return v, nil
 
82
        case int64:
 
83
                return int(v), nil
 
84
        default:
 
85
                if i, err := strconv.Atoi(fmt.Sprintf("%v", v)); err != nil {
 
86
                        return 0, err
 
87
                } else {
 
88
                        s.Set(key, i)
 
89
                        return i, nil
 
90
                }
 
91
        }
 
92
        panic("unreachable")
 
93
}
 
94
 
 
95
// GetBool returns the boolean value for the configuration key if set,
 
96
// otherwise false.
 
97
func (s *Settings) GetBool(key string) bool {
 
98
        var result bool
 
99
        switch v := s.Get(key).(type) {
 
100
        case bool:
 
101
                return v
 
102
        case int:
 
103
                result = v != 0
 
104
        case string:
 
105
                b, err := strconv.ParseBool(v)
 
106
                result = err == nil && b
 
107
        default:
 
108
                result = false
 
109
        }
 
110
        s.Set(key, result)
 
111
        return result
 
112
}
 
113
 
 
114
// GetStrings returns a []string slice for the configuration key if set,
 
115
// otherwise an empty slice.
 
116
func (s *Settings) GetStrings(key string) (value []string) {
 
117
        if strs, is := s.Get(key).([]interface{}); is {
 
118
                for _, v := range strs {
 
119
                        if str, is := v.(string); is {
 
120
                                value = append(value, str)
 
121
                        }
 
122
                }
 
123
        }
 
124
        return
 
125
}
 
126
 
 
127
// SetConfig sets the global configuration to the TOML-formatted string contents.
 
128
func SetConfig(contents string) (err error) {
 
129
        var tree *toml.TomlTree
 
130
        if tree, err = toml.Load(contents); err != nil {
 
131
                return
 
132
        }
 
133
        config = &Settings{tree}
 
134
        return
 
135
}
 
136
 
 
137
// LoadConfig sets the global configuration to the TOML-formatted reader contents.
 
138
func LoadConfig(r io.Reader) (err error) {
 
139
        buf := bytes.NewBuffer(nil)
 
140
        _, err = io.Copy(buf, r)
 
141
        if err != nil {
 
142
                return
 
143
        }
 
144
        var tree *toml.TomlTree
 
145
        if tree, err = toml.Load(buf.String()); err != nil {
 
146
                return
 
147
        }
 
148
        config = &Settings{tree}
 
149
        return
 
150
}
 
151
 
 
152
// LoadConfigFile sets the global configuration to the contents from the TOML file path.
 
153
func LoadConfigFile(path string) (err error) {
 
154
        var tree *toml.TomlTree
 
155
        if tree, err = toml.LoadFile(path); err != nil {
 
156
                return
 
157
        }
 
158
        config = &Settings{tree}
 
159
        return
 
160
}