Skip to content

Commit e4c14ec

Browse files
committed
config: flags configurable via config file
1 parent 8cbfcf5 commit e4c14ec

File tree

5 files changed

+60
-99
lines changed

5 files changed

+60
-99
lines changed

cmd/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/DSpeichert/netbootd/config"
66
"github.com/spf13/cobra"
7+
"github.com/spf13/viper"
78
"os"
89
)
910

@@ -17,8 +18,11 @@ var (
1718

1819
func init() {
1920
cobra.OnInitialize(config.InitConfig)
20-
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "enable debug logging")
21-
rootCmd.Flags().BoolVar(&trace, "trace", false, "enable trace logging")
21+
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug logging")
22+
viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug"))
23+
24+
rootCmd.PersistentFlags().BoolVar(&trace, "trace", false, "enable trace logging")
25+
viper.BindPFlag("trace", rootCmd.Flags().Lookup("trace"))
2226
}
2327

2428
var rootCmd = &cobra.Command{

cmd/server.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,36 @@ var (
2828

2929
func init() {
3030
serverCmd.Flags().StringVarP(&addr, "address", "a", "", "IP address to listen on (DHCP, TFTP, HTTP)")
31+
viper.BindPFlag("address", serverCmd.Flags().Lookup("address"))
32+
3133
serverCmd.Flags().IntVarP(&httpPort, "http-port", "p", 8080, "HTTP port to listen on")
34+
viper.BindPFlag("http.port", serverCmd.Flags().Lookup("http-port"))
35+
3236
serverCmd.Flags().IntVarP(&apiPort, "api-port", "r", 8081, "HTTP API port to listen on")
37+
viper.BindPFlag("api.port", serverCmd.Flags().Lookup("api-port"))
38+
3339
serverCmd.Flags().StringVar(&apiTlsCert, "api-tls-cert", "", "Path to TLS certificate API")
40+
viper.BindPFlag("api.TLSCertificatePath", serverCmd.Flags().Lookup("api-tls-cert"))
41+
3442
serverCmd.Flags().StringVar(&apiTlsKey, "api-tls-key", "", "Path to TLS certificate for API")
43+
viper.BindPFlag("api.TLSPrivateKeyPath", serverCmd.Flags().Lookup("api-tls-key"))
44+
3545
serverCmd.Flags().StringVarP(&ifname, "interface", "i", "", "interface to listen on, e.g. eth0 (DHCP)")
46+
viper.BindPFlag("interface", serverCmd.Flags().Lookup("interface"))
47+
3648
serverCmd.Flags().StringVarP(&manifestPath, "manifests", "m", "", "load manifests from directory")
49+
viper.BindPFlag("manifestPath", serverCmd.Flags().Lookup("manifests"))
3750

38-
viper.BindPFlag("api.TLSCertificatePath", serverCmd.Flags().Lookup("api-tls-cert"))
39-
viper.BindPFlag("api.TLSPrivateKeyPath", serverCmd.Flags().Lookup("api-tls-key"))
4051
rootCmd.AddCommand(serverCmd)
4152
}
4253

4354
var serverCmd = &cobra.Command{
4455
Use: "server",
4556
Run: func(cmd *cobra.Command, args []string) {
4657
// configure logging
47-
if trace {
58+
if viper.GetBool("trace") {
4859
zerolog.SetGlobalLevel(zerolog.TraceLevel)
49-
} else if debug {
60+
} else if viper.GetBool("debug") {
5061
zerolog.SetGlobalLevel(zerolog.DebugLevel)
5162
} else {
5263
zerolog.SetGlobalLevel(zerolog.InfoLevel)
@@ -57,14 +68,14 @@ var serverCmd = &cobra.Command{
5768
// TODO: config
5869
PersistenceDirectory: "",
5970
})
60-
if manifestPath != "" {
61-
log.Info().Str("path", manifestPath).Msg("Loading manifests")
62-
_ = store.LoadFromDirectory(manifestPath)
71+
if viper.GetString("manifestPath") != "" {
72+
log.Info().Str("path", viper.GetString("manifestPath")).Msg("Loading manifests")
73+
_ = store.LoadFromDirectory(viper.GetString("manifestPath"))
6374
}
64-
store.GlobalHints.HttpPort = httpPort
75+
store.GlobalHints.HttpPort = viper.GetInt("http.port")
6576

6677
// DHCP
67-
dhcpServer, err := dhcpd.NewServer(addr, ifname, store)
78+
dhcpServer, err := dhcpd.NewServer(viper.GetString("address"), viper.GetString("interface"), store)
6879
if err != nil {
6980
log.Fatal().Err(err)
7081
}
@@ -76,7 +87,7 @@ var serverCmd = &cobra.Command{
7687
log.Fatal().Err(err)
7788
}
7889
connTftp, err := net.ListenUDP("udp", &net.UDPAddr{
79-
IP: net.ParseIP(addr),
90+
IP: net.ParseIP(viper.GetString("address")),
8091
Port: 69, // TFTP
8192
})
8293
if err != nil {
@@ -90,8 +101,8 @@ var serverCmd = &cobra.Command{
90101
log.Fatal().Err(err)
91102
}
92103
connHttp, err := net.ListenTCP("tcp", &net.TCPAddr{
93-
IP: net.ParseIP(addr),
94-
Port: httpPort, // HTTP
104+
IP: net.ParseIP(viper.GetString("address")),
105+
Port: viper.GetInt("http.port"), // HTTP
95106
})
96107
if err != nil {
97108
log.Fatal().Err(err)
@@ -105,8 +116,8 @@ var serverCmd = &cobra.Command{
105116
log.Fatal().Err(err)
106117
}
107118
connApi, err := net.ListenTCP("tcp", &net.TCPAddr{
108-
IP: net.ParseIP(addr),
109-
Port: apiPort, // HTTP
119+
IP: net.ParseIP(viper.GetString("address")),
120+
Port: viper.GetInt("api.port"), // HTTP
110121
})
111122
if err != nil {
112123
log.Fatal().Err(err)
@@ -121,7 +132,7 @@ var serverCmd = &cobra.Command{
121132
go apiServer.Serve(connApi)
122133
log.Info().Interface("api", connApi.Addr()).Msg("HTTP API listening...")
123134
go func() {
124-
go apiServer.Serve(connApi)
135+
err := apiServer.Serve(connApi)
125136
log.Error().Err(err).Msg("Error initializing HTTP API listener!")
126137
}()
127138
}

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ require (
1616
github.com/mitchellh/copystructure v1.2.0 // indirect
1717
github.com/pin/tftp v2.1.0+incompatible
1818
github.com/rs/zerolog v1.23.0
19-
github.com/spf13/cobra v1.1.3
19+
github.com/spf13/cobra v1.2.1
2020
github.com/spf13/viper v1.8.1
21+
github.com/stretchr/objx v0.1.1 // indirect
2122
github.com/u-root/u-root v7.0.0+incompatible
2223
github.com/u-root/uio v0.0.0-20210528151154-e40b768296a7 // indirect
2324
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
2425
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
25-
golang.org/x/sys v0.0.0-20210629170331-7dc0b73dc9fb
26+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
2627
gopkg.in/yaml.v2 v2.4.0
2728
)
2829

0 commit comments

Comments
 (0)