-
Notifications
You must be signed in to change notification settings - Fork 38
/
config.go
132 lines (113 loc) · 3.63 KB
/
config.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
/* Goiardi configuration. */
/*
* Copyright (c) 2013, Jeremy Bingham (<jbingham@gmail.com>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"github.com/jessevdk/go-flags"
"github.com/BurntSushi/toml"
"os"
"log"
"fmt"
)
/* Master struct for configuration. */
type Conf struct {
Ipaddress string
Port int
Hostname string
ConfFile string
DebugLevel int
}
/* Struct for command line options. */
type Options struct {
Version bool `short:"v" long:"version" description:"Print version info."`
Verbose []bool `short:"V" long:"verbose" description:"Show verbose debug information. (not implemented)"`
ConfFile string `short:"c" long:"config" description:"Specify a config file to use."`
Ipaddress string `short:"I" long:"ipaddress" description:"Listen on a specific IP address."`
Hostname string `short:"H" long:"hostname" description:"Hostname to use for this server. Defaults to hostname reported by the kernel."`
Port int `short:"P" long:"port" description:"Port to listen on. (default: 4545)"`
}
// The goiardi version
const Version = "0.2.1"
// The chef version we're at least aiming for, even if it's not complete yet
const ChefVersion = "11.0.8"
/* The general plan is to read the command-line options, then parse the config
* file, fill in the config struct with those values, then apply the
* command-line options to the config struct. We read the cli options first so
* we know to look for a different config file if needed, but otherwise the
* command line options override what's in the config file. */
func InitConfig() *Conf { return &Conf{ } }
var Config = InitConfig()
// Read and apply arguments from the command line.
func ParseConfigOptions() error {
var opts = &Options{ }
_, err := flags.Parse(opts)
if err != nil {
if err.(*flags.Error).Type == flags.ErrHelp {
os.Exit(0)
} else {
log.Println(err)
os.Exit(1)
}
}
if opts.Version {
fmt.Printf("goiardi version %s (aiming for compatibility with Chef Server version %s).\n", Version, ChefVersion)
os.Exit(0)
}
/* Load the config file. Command-line options have precedence over
* config file options. */
if opts.ConfFile != "" {
if _, err := toml.DecodeFile(opts.ConfFile, Config); err != nil {
panic(err)
os.Exit(1)
}
}
if opts.Hostname != "" {
Config.Hostname = opts.Hostname
} else {
if Config.Hostname == "" {
Config.Hostname, err = os.Hostname()
if err != nil {
log.Println(err)
Config.Hostname = "localhost"
}
}
}
Config.Ipaddress = opts.Ipaddress
if opts.Port != 0 {
Config.Port = opts.Port
}
if Config.Port == 0 {
Config.Port = 4545
}
Config.DebugLevel = len(opts.Verbose)
return nil
}
// The address and port goiardi is configured to listen on.
func ListenAddr() string {
listen_addr := fmt.Sprintf("%s:%d", Config.Ipaddress, Config.Port)
return listen_addr
}
// The hostname and port goiardi is configured to use.
func ServerHostname() string {
hostname := fmt.Sprintf("%s:%d", Config.Hostname, Config.Port)
return hostname
}
// The base URL
func ServerBaseURL() string {
/* TODO: allow configuring using http vs. https */
url := fmt.Sprintf("http://%s", ServerHostname())
return url
}