From 5cd4fab9dd37ba663297f63a8e7d1ea5d1c2cc8e Mon Sep 17 00:00:00 2001 From: Austin Cherry Date: Fri, 5 Oct 2018 14:58:21 -0500 Subject: [PATCH] TUN-1086: fixed config option --- cmd/cloudflared/main.go | 22 +------------------ cmd/cloudflared/tunnel/cmd.go | 41 +++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/cmd/cloudflared/main.go b/cmd/cloudflared/main.go index e9b296fc..c74c2bc2 100644 --- a/cmd/cloudflared/main.go +++ b/cmd/cloudflared/main.go @@ -8,7 +8,6 @@ import ( "golang.org/x/crypto/ssh/terminal" "github.com/cloudflare/cloudflared/cmd/cloudflared/access" - "github.com/cloudflare/cloudflared/cmd/cloudflared/config" "github.com/cloudflare/cloudflared/cmd/cloudflared/tunnel" "github.com/cloudflare/cloudflared/cmd/cloudflared/updater" "github.com/cloudflare/cloudflared/log" @@ -17,7 +16,6 @@ import ( "github.com/getsentry/raven-go" "github.com/mitchellh/go-homedir" "gopkg.in/urfave/cli.v2" - "gopkg.in/urfave/cli.v2/altsrc" "github.com/pkg/errors" ) @@ -56,7 +54,7 @@ func main() { and configure access control.` app.Flags = flags() app.Action = action(Version, shutdownC, graceShutdownC) - app.Before = before(app.Flags) + app.Before = tunnel.Before app.Commands = commands() tunnel.Init(Version, shutdownC, graceShutdownC) // we need this to support the tunnel sub command... @@ -105,24 +103,6 @@ func action(version string, shutdownC, graceShutdownC chan struct{}) cli.ActionF } } -func before(flags []cli.Flag) cli.BeforeFunc { - return func(context *cli.Context) error { - inputSource, err := config.FindInputSourceContext(context) - if err != nil { - logger.WithError(err).Infof("Cannot load configuration from %s", context.String("config")) - return err - } else if inputSource != nil { - err := altsrc.ApplyInputSourceValues(context, inputSource, flags) - if err != nil { - logger.WithError(err).Infof("Cannot apply configuration from %s", context.String("config")) - return err - } - logger.Infof("Applied configuration from %s", context.String("config")) - } - return nil - } -} - func userHomeDir() (string, error) { // This returns the home dir of the executing user using OS-specific method // for discovering the home dir. It's not recommended to call this function diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 63b88aba..02e5ada8 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -121,25 +121,8 @@ func Commands() []*cli.Command { } return err }, - Before: func(c *cli.Context) error { - if c.String("config") == "" { - logger.Warnf("Cannot determine default configuration path. No file %v in %v", config.DefaultConfigFiles, config.DefaultConfigDirs) - } - inputSource, err := config.FindInputSourceContext(c) - if err != nil { - logger.WithError(err).Infof("Cannot load configuration from %s", c.String("config")) - return err - } else if inputSource != nil { - err := altsrc.ApplyInputSourceValues(c, inputSource, c.App.Flags) - if err != nil { - logger.WithError(err).Infof("Cannot apply configuration from %s", c.String("config")) - return err - } - logger.Infof("Applied configuration from %s", c.String("config")) - } - return nil - }, - Usage: "SQL Gateway is an SQL over HTTP reverse proxy", + Before: Before, + Usage: "SQL Gateway is an SQL over HTTP reverse proxy", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "db", @@ -166,6 +149,7 @@ func Commands() []*cli.Command { cmds = append(cmds, &cli.Command{ Name: "tunnel", Action: tunnel, + Before: Before, Category: "Tunnel", Usage: "Make a locally-running web service accessible over the internet using Argo Tunnel.", ArgsUsage: "[origin-url]", @@ -338,6 +322,25 @@ func StartServer(c *cli.Context, version string, shutdownC, graceShutdownC chan return waitToShutdown(&wg, errC, shutdownC, graceShutdownC, c.Duration("grace-period")) } +func Before(c *cli.Context) error { + if c.String("config") == "" { + logger.Warnf("Cannot determine default configuration path. No file %v in %v", config.DefaultConfigFiles, config.DefaultConfigDirs) + } + inputSource, err := config.FindInputSourceContext(c) + if err != nil { + logger.WithError(err).Infof("Cannot load configuration from %s", c.String("config")) + return err + } else if inputSource != nil { + err := altsrc.ApplyInputSourceValues(c, inputSource, c.App.Flags) + if err != nil { + logger.WithError(err).Infof("Cannot apply configuration from %s", c.String("config")) + return err + } + logger.Infof("Applied configuration from %s", c.String("config")) + } + return nil +} + func waitToShutdown(wg *sync.WaitGroup, errC chan error, shutdownC, graceShutdownC chan struct{},