Skip to content

Commit

Permalink
Feature: variable config dir
Browse files Browse the repository at this point in the history
  • Loading branch information
chopmann authored and xsteadfastx committed Jan 6, 2022
1 parent d459840 commit db73a57
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -18,6 +19,7 @@ var (
)

var (
cfgDir string
iface string
verbose bool
protocol int
Expand Down Expand Up @@ -112,7 +114,7 @@ func loadConfig(cfg string) (*wgquick.Config, logrus.FieldLogger) {
log = logrus.WithField("iface", iface)
}

cfg = "/etc/wireguard/" + cfg + ".conf"
cfg = fmt.Sprintf("%s/%s.conf", cfgDir, cfg)

_, err = os.Stat(cfg)
if err != nil {
Expand Down Expand Up @@ -140,6 +142,9 @@ func loadConfig(cfg string) (*wgquick.Config, logrus.FieldLogger) {
}

func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgDir, "config-dir", "", "config directory (default is /etc/wireguard)")
rootCmd.PersistentFlags().StringVarP(&iface, "iface", "i", "", "if interface name should differ from config")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose")
rootCmd.PersistentFlags().IntVarP(&protocol, "route-protocol", "p", 0, "route protocol to use for our routes")
Expand All @@ -157,6 +162,14 @@ func init() {
rootCmd.AddCommand(showCmd)
}

func initConfig() {
if cfgDir != "" {
cfgDir = "/etc/wireguard"
}

cfgDir = strings.TrimSuffix(cfgDir, "/")
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
logrus.Fatal(err)
Expand Down

0 comments on commit db73a57

Please sign in to comment.