/
common.go
39 lines (33 loc) · 902 Bytes
/
common.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/codegangsta/cli"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
)
func defaultConfigPath(c *cli.Context, file string) string {
path := filepath.Join(os.Getenv("HOME"), ".config/fproxy", file)
if args := c.Args(); len(args) > 0 {
path = args[0]
}
return path
}
// Returns the first element of args, or defaultArg if args is an empty slice.
func firstOrDefault(args []string, defaultArg string) string {
if len(args) > 0 {
return args[0]
}
return defaultArg
}
func readConfig(configPath string, msg proto.Message) {
protoBytes, err := ioutil.ReadFile(configPath)
if err != nil {
glog.Fatalf("Could not read config at path %v: %v", configPath, err)
}
err = proto.UnmarshalText(string(protoBytes), msg)
if err != nil {
glog.Fatalf("Could not parse config at path %v: %v", configPath, err)
}
}