~hockeypuck/hockeypuck/0.8

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
/*
   Hockeypuck - OpenPGP key server
   Copyright (C) 2012  Casey Marshall

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, version 3.

   This program 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 Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package hockeypuck

import (
	"bufio"
	"bytes"
	"errors"
	"flag"
	"fmt"
	"log"
	"os"
	"strings"
)

func LogCfg(l *log.Logger) {
	flag.VisitAll(func(f *flag.Flag) {
		l.Println(f.Name, "=", f.Value)
	})
}

func ParseCfg() {
	var err error
	var f *os.File
	var bf *bufio.Reader
	var mkline *bytes.Buffer
	var fi os.FileInfo
	if fi, err = os.Stat(CONFIG_PATH); err != nil || fi.IsDir() {
		// no config file or not found
		goto CFGERR
		return
	}
	f, err = os.Open(CONFIG_PATH)
	if err != nil {
		goto CFGERR
		return
	}
	fmt.Fprintf(os.Stderr, "Reading configuration from %v\n", f.Name())
	bf = bufio.NewReader(f)
	mkline = bytes.NewBuffer([]byte{})
	for {
		part, prefix, err := bf.ReadLine()
		if err != nil {
			break
		}
		mkline.Write(part)
		if !prefix {
			err = ParseCfgLine(string(mkline.Bytes()))
			if err != nil {
				panic(fmt.Sprintf(
					"Error in configuration file %v: %v\n",
					CONFIG_PATH, err))
			}
			mkline.Reset()
		}
	}
	return
CFGERR:
	fmt.Fprintf(os.Stderr, "%v\n", err)
	return
}

func ParseCfgLine(line string) (err error) {
	line = strings.TrimSpace(line)
	if line[0] == '#' {
		return
	}
	parts := strings.Split(line, "=")
	if len(parts) != 2 {
		return errors.New(fmt.Sprintf(
			"Expected line of form 'key = value', got: %v", line))
	}
	key, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
	flag.Set(key, value)
	return
}