-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
183 lines (169 loc) · 3.87 KB
/
utils.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
package kabuta
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"reflect"
"strings"
"sync"
)
var (
onceEnv sync.Once
onceConfig sync.Once
// Holds environment variables
environ map[string]string
)
func returnErrorf(s string, args ...interface{}) gdbMiResponse {
return gdbMiResponse{err: NewError(s, args)}
}
func returnError(err error) gdbMiResponse {
return gdbMiResponse{err: err}
}
func MakeGdbResult(x interface{}) string {
switch x.(type) {
// TODO this really calls for a default fallthrough behavior
// being a part of switch statement. I've written too many of these...
case int:
fallthrough
case int8:
fallthrough
case int16:
fallthrough
case int32:
fallthrough
case int64:
return f("\"%d\"", x)
case string:
return f("\"%s\"", x)
default:
t := reflect.TypeOf(x)
switch t.Kind() {
case reflect.Map:
panic("Don't know how to deal with %T %s", x, t)
case reflect.Array:
panic("Don't know how to deal with %T %s", x, t)
case reflect.Slice:
panic("Don't know how to deal with %T %s", x, t)
case reflect.Struct:
panic("Don't know how to deal with %T %s", x, t)
default:
panic("Don't know how to deal with %T %s", x, t)
}
}
}
// Environ is similar to os.Environ() but
// returning environment as a map instead of an
// array of strings.
func Environ() map[string]string {
onceEnv.Do(initEnviron)
return environ
}
// Simple utility to provide a string blabla
func String(x interface{}) string {
// TODO string
b, e := json.Marshal(x)
if e == nil {
return string(b)
} else {
return f("%+v", x)
}
}
func initEnviron() {
environ = make(map[string]string)
for _, kv := range os.Environ() {
keyValue := strings.Split(kv, "=")
environ[keyValue[0]] = keyValue[1]
}
}
// Holds config
var config map[string]string
// Config returns Kabuta's configuration -- which consists
// of the environment variable values, overridden with
// values from ~/.kabutainit. If values are not found, they default
// as follows:
// EnvKabutaLogFile: to ~/kabuta.log
func Config() (map[string]string, error) {
var err error
onceConfig.Do(func() {
err = initConfig()
})
return config, err
}
func initConfig() error {
var err error
env := Environ()
config = make(map[string]string)
envVars := []string{EnvKabutaDlvPath, EnvKabutaLogFile, EnvKabutaDlvPort, EnvKabutaPath}
for _, k := range envVars {
config[k] = env[k]
}
user, err := user.Current()
if err != nil {
return err
}
configFileName := filepath.Join(user.HomeDir, KabutaInitFile)
// If the file doesn't exist, it's not an error, just don't do anything
if _, err = os.Stat(configFileName); os.IsNotExist(err) {
return nil
}
configFile, err := os.Open(configFileName)
if err != nil {
return err
}
defer configFile.Close()
reader := bufio.NewReader(configFile)
lineNo := 0
for {
lineNo += 1
line, err := reader.ReadString('\n')
line = strings.TrimSpace(line)
// Ignore empty lines
if line == "" {
continue
}
// Ignore comments
if strings.HasPrefix(line, "#") {
continue
}
kv := strings.Split(line, "=")
if len(kv) < 2 {
return NewError("Error in %s on line %d: cannot parse %s", configFileName, lineNo, line)
}
key := kv[0]
value := strings.Join(kv[1:], "=")
config[key] = value
if err == io.EOF {
break
}
if err != nil {
return err
}
}
if config[EnvKabutaLogFile] == "" {
config[EnvKabutaLogFile] = filepath.Join(user.HomeDir, DefaultKabutaLogFile)
}
if config[EnvKabutaDlvPort] == "" {
config[EnvKabutaDlvPort] = DefaultDlvPort
}
return nil
}
// f is a shortcut for fmt.Sprintf
func f(s string, args ...interface{}) string {
if args == nil || len(args) == 0 {
return s
}
return fmt.Sprintf(s, args...)
}
func NewError(err string, args ...interface{}) error {
if args == nil || len(args) == 0 {
return errors.New(err)
}
msg := f(err, args...)
out := errors.New(msg)
return out
}