forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties.go
219 lines (192 loc) · 5.92 KB
/
properties.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/getgauge/common"
"github.com/getgauge/gauge/version"
)
const comment = `This file contains Gauge specific internal configurations. Do not delete`
type property struct {
Key string `json:"key"`
Value string `json:"value"`
description string
defaultValue string
}
type properties struct {
p map[string]*property
}
func (p *properties) set(k, v string) error {
if _, ok := p.p[k]; ok {
p.p[k].Value = v
return nil
}
return fmt.Errorf("config '%s' doesn't exist", k)
}
func (p *properties) get(k string) (string, error) {
if _, ok := p.p[k]; ok {
return p.p[k].Value, nil
}
return "", fmt.Errorf("config '%s' doesn't exist", k)
}
func (p *properties) Format(f formatter) (string, error) {
var all []property
for _, v := range p.p {
all = append(all, *v)
}
return f.format(all)
}
func (p *properties) String() string {
var buffer bytes.Buffer
buffer.WriteString("# Version " + version.FullVersion())
buffer.WriteString("\n")
buffer.WriteString("# ")
buffer.WriteString(comment)
buffer.WriteString("\n")
var keys []string
for k := range p.p {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := p.p[k]
buffer.WriteString("\n")
buffer.WriteString("# ")
buffer.WriteString(v.description)
buffer.WriteString("\n")
buffer.WriteString(v.Key)
buffer.WriteString(" = ")
buffer.WriteString(v.Value)
buffer.WriteString("\n")
}
return buffer.String()
}
func (p *properties) Write(w io.Writer) (int, error) {
return w.Write([]byte(p.String()))
}
func Properties() *properties {
return &properties{p: map[string]*property{
gaugeRepositoryURL: newProperty(gaugeRepositoryURL, "https://downloads.gauge.org/plugin", "Url to get plugin versions"),
gaugeTemplatesURL: newProperty(gaugeTemplatesURL, "https://templates.gauge.org", "Url to get templates list"),
runnerConnectionTimeout: newProperty(runnerConnectionTimeout, "30000", "Timeout in milliseconds for making a connection to the language runner."),
pluginConnectionTimeout: newProperty(pluginConnectionTimeout, "10000", "Timeout in milliseconds for making a connection to plugins."),
pluginKillTimeOut: newProperty(pluginKillTimeOut, "4000", "Timeout in milliseconds for a plugin to stop after a kill message has been sent."),
runnerRequestTimeout: newProperty(runnerRequestTimeout, "30000", "Timeout in milliseconds for requests from the language runner."),
ideRequestTimeout: newProperty(ideRequestTimeout, "30000", "Timeout in milliseconds for requests from runner when invoked for ide."),
checkUpdates: newProperty(checkUpdates, "true", "Allow Gauge and its plugin updates to be notified."),
telemetryEnabled: newProperty(telemetryEnabled, "true", "Allow Gauge to collect anonymous usage statistics"),
telemetryLoggingEnabled: newProperty(telemetryLoggingEnabled, "false", "Log request sent to Gauge telemetry engine"),
telemetryConsent: newProperty(telemetryConsent, "false", "Record user opt in/out for telemetry"),
}}
}
func MergedProperties() *properties {
p := Properties()
config, err := common.GetGaugeConfiguration()
if err != nil {
return p
}
for k, v := range config {
p.set(k, v)
}
return p
}
func Update(name, value string) error {
p := MergedProperties()
err := p.set(name, value)
if err != nil {
return err
}
return writeConfig(p)
}
func UpdateTelemetry(value string) error {
return Update(telemetryEnabled, value)
}
func UpdateTelemetryLoggging(value string) error {
return Update(telemetryLoggingEnabled, value)
}
func Merge() error {
v, err := gaugeVersionInProperties()
if err != nil || version.CompareVersions(v, version.CurrentGaugeVersion, version.LesserThanFunc) {
return writeConfig(MergedProperties())
}
return nil
}
func GetProperty(name string) (string, error) {
return MergedProperties().get(name)
}
func List(machineReadable bool) (string, error) {
var f formatter
f = textFormatter{}
if machineReadable {
f = &jsonFormatter{}
}
return MergedProperties().Format(f)
}
func newProperty(key, defaultValue, description string) *property {
return &property{
Key: key,
defaultValue: defaultValue,
description: description,
Value: defaultValue,
}
}
func writeConfig(p *properties) error {
gaugePropertiesFile, err := gaugePropertiesFile()
if err != nil {
return err
}
var f *os.File
if _, err = os.Stat(gaugePropertiesFile); err != nil {
f, err = os.Create(gaugePropertiesFile)
} else {
f, err = os.OpenFile(gaugePropertiesFile, os.O_WRONLY, os.ModeExclusive)
if err != nil {
return err
}
}
defer f.Close()
_, err = p.Write(f)
return err
}
func gaugePropertiesFile() (string, error) {
dir, err := common.GetConfigurationDir()
if err != nil {
return "", err
}
return filepath.Join(dir, common.GaugePropertiesFile), err
}
func gaugeVersionInProperties() (*version.Version, error) {
var v *version.Version
pf, err := gaugePropertiesFile()
if err != nil {
return v, err
}
f, err := os.Open(pf)
if err != nil {
return v, err
}
defer f.Close()
r := bufio.NewReader(f)
l, _, err := r.ReadLine()
if err != nil {
return v, err
}
return version.ParseVersion(strings.TrimLeft(string(l), "# Version "))
}