From 205b142614d5de08ffc33a04ae4cfc00e65b5dfc Mon Sep 17 00:00:00 2001 From: Matt Holt Date: Mon, 10 Apr 2023 13:55:45 -0600 Subject: cmd: Support `'` quotes in envfile parsing (#5437) --- cmd/main.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index 17da8bd..96debdf 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -374,18 +374,19 @@ func parseEnvFile(envInput io.Reader) (map[string]string, error) { } // quoted value: support newlines - if strings.HasPrefix(val, `"`) { - for !(strings.HasSuffix(line, `"`) && !strings.HasSuffix(line, `\"`)) { - val = strings.ReplaceAll(val, `\"`, `"`) + if strings.HasPrefix(val, `"`) || strings.HasPrefix(val, "'") { + quote := string(val[0]) + for !(strings.HasSuffix(line, quote) && !strings.HasSuffix(line, `\`+quote)) { + val = strings.ReplaceAll(val, `\`+quote, quote) if !scanner.Scan() { break } lineNumber++ - line = strings.ReplaceAll(scanner.Text(), `\"`, `"`) + line = strings.ReplaceAll(scanner.Text(), `\`+quote, quote) val += "\n" + line } - val = strings.TrimPrefix(val, `"`) - val = strings.TrimSuffix(val, `"`) + val = strings.TrimPrefix(val, quote) + val = strings.TrimSuffix(val, quote) } envMap[key] = val -- cgit v1.2.3 From 571fc034d3838d8c2a33ab8424d44eb5b30f4ecd Mon Sep 17 00:00:00 2001 From: Yehonatan Ezron <37303618+jonatan5524@users.noreply.github.com> Date: Tue, 9 May 2023 01:49:16 +0300 Subject: feature: watch include directory (#5521) Co-authored-by: Matt Holt --- cmd/main.go | 53 ++++++++++++++--------------------------------------- 1 file changed, 14 insertions(+), 39 deletions(-) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index 96debdf..97326ba 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -174,6 +174,8 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) { // blocks indefinitely; it only quits if the poller has errors for // long enough time. The filename passed in must be the actual // config file used, not one to be discovered. +// Each second the config files is loaded and parsed into an object +// and is compared to the last config object that was loaded func watchConfigFile(filename, adapterName string) { defer func() { if err := recover(); err != nil { @@ -189,64 +191,37 @@ func watchConfigFile(filename, adapterName string) { With(zap.String("config_file", filename)) } - // get the initial timestamp on the config file - info, err := os.Stat(filename) + // get current config + lastCfg, _, err := LoadConfig(filename, adapterName) if err != nil { - logger().Error("cannot watch config file", zap.Error(err)) + logger().Error("unable to load latest config", zap.Error(err)) return } - lastModified := info.ModTime() logger().Info("watching config file for changes") - // if the file disappears or something, we can - // stop polling if the error lasts long enough - var lastErr time.Time - finalError := func(err error) bool { - if lastErr.IsZero() { - lastErr = time.Now() - return false - } - if time.Since(lastErr) > 30*time.Second { - logger().Error("giving up watching config file; too many errors", - zap.Error(err)) - return true - } - return false - } - // begin poller //nolint:staticcheck for range time.Tick(1 * time.Second) { - // get the file info - info, err := os.Stat(filename) + + // get current config + newCfg, _, err := LoadConfig(filename, adapterName) if err != nil { - if finalError(err) { - return - } - continue + logger().Error("unable to load latest config", zap.Error(err)) + return } - lastErr = time.Time{} // no error, so clear any memory of one // if it hasn't changed, nothing to do - if !info.ModTime().After(lastModified) { + if bytes.Equal(lastCfg, newCfg) { continue } - logger().Info("config file changed; reloading") - // remember this timestamp - lastModified = info.ModTime() - - // load the contents of the file - config, _, err := LoadConfig(filename, adapterName) - if err != nil { - logger().Error("unable to load latest config", zap.Error(err)) - continue - } + // remember the current config + lastCfg = newCfg // apply the updated config - err = caddy.Load(config, false) + err = caddy.Load(lastCfg, false) if err != nil { logger().Error("applying latest config", zap.Error(err)) continue -- cgit v1.2.3 From 5ebb7d496da765fd0263161cd306ecf86b609485 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 12 May 2023 11:04:02 -0600 Subject: cmd: Reduce spammy logs from --watch --- cmd/main.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index 97326ba..7db2092 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -89,6 +89,10 @@ func handlePingbackConn(conn net.Conn, expect []byte) error { // and returns the resulting JSON config bytes along with // the name of the loaded config file (if any). func LoadConfig(configFile, adapterName string) ([]byte, string, error) { + return loadConfigWithLogger(caddy.Log(), configFile, adapterName) +} + +func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, error) { // specifying an adapter without a config file is ambiguous if adapterName != "" && configFile == "" { return nil, "", fmt.Errorf("cannot adapt config without config file (use --config)") @@ -107,9 +111,11 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) { if err != nil { return nil, "", fmt.Errorf("reading config file: %v", err) } - caddy.Log().Info("using provided configuration", - zap.String("config_file", configFile), - zap.String("config_adapter", adapterName)) + if logger != nil { + logger.Info("using provided configuration", + zap.String("config_file", configFile), + zap.String("config_adapter", adapterName)) + } } else if adapterName == "" { // as a special case when no config file or adapter // is specified, see if the Caddyfile adapter is @@ -126,7 +132,9 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) { } else { // success reading default Caddyfile configFile = "Caddyfile" - caddy.Log().Info("using adjacent Caddyfile") + if logger != nil { + logger.Info("using adjacent Caddyfile") + } } } } @@ -161,7 +169,9 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) { if warn.Directive != "" { msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message) } - caddy.Log().Warn(msg, zap.String("adapter", adapterName), zap.String("file", warn.File), zap.Int("line", warn.Line)) + if logger != nil { + logger.Warn(msg, zap.String("adapter", adapterName), zap.String("file", warn.File), zap.Int("line", warn.Line)) + } } config = adaptedConfig } @@ -192,7 +202,7 @@ func watchConfigFile(filename, adapterName string) { } // get current config - lastCfg, _, err := LoadConfig(filename, adapterName) + lastCfg, _, err := loadConfigWithLogger(nil, filename, adapterName) if err != nil { logger().Error("unable to load latest config", zap.Error(err)) return @@ -203,7 +213,6 @@ func watchConfigFile(filename, adapterName string) { // begin poller //nolint:staticcheck for range time.Tick(1 * time.Second) { - // get current config newCfg, _, err := LoadConfig(filename, adapterName) if err != nil { -- cgit v1.2.3 From 38cb587e0f1b38db2c9fb422b4892e48753f00c0 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 17 May 2023 16:13:15 -0600 Subject: cmd: Avoid spammy log messages (fix #5538) I forgot there are two calls to LoadConfig() here that needed replacing. --- cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index 7db2092..bfbd0bb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -214,7 +214,7 @@ func watchConfigFile(filename, adapterName string) { //nolint:staticcheck for range time.Tick(1 * time.Second) { // get current config - newCfg, _, err := LoadConfig(filename, adapterName) + newCfg, _, err := loadConfigWithLogger(nil, filename, adapterName) if err != nil { logger().Error("unable to load latest config", zap.Error(err)) return -- cgit v1.2.3 From d8135505d38ca872d507418d2017f4dbf352f06a Mon Sep 17 00:00:00 2001 From: pistasjis <57069715+pistasjis@users.noreply.github.com> Date: Wed, 9 Aug 2023 19:40:37 +0200 Subject: cmd: Require config for caddy validate (fix #5612) (#5614) * Require config for caddy validate - fixes #5612 Signed-off-by: Pistasj * Try making adjacent Caddyfile check its own function Signed-off-by: Pistasj * add Francis' suggestion Co-authored-by: Francis Lavoie * Refactor * Fix borked commit, sigh --------- Signed-off-by: Pistasj Co-authored-by: Francis Lavoie Co-authored-by: Matthew Holt --- cmd/main.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index bfbd0bb..53b8d67 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -117,9 +117,8 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([ zap.String("config_adapter", adapterName)) } } else if adapterName == "" { - // as a special case when no config file or adapter - // is specified, see if the Caddyfile adapter is - // plugged in, and if so, try using a default Caddyfile + // if the Caddyfile adapter is plugged in, we can try using an + // adjacent Caddyfile by default cfgAdapter = caddyconfig.GetAdapter("caddyfile") if cfgAdapter != nil { config, err = os.ReadFile("Caddyfile") -- cgit v1.2.3 From d6f86cccf5fa5b4eb30141da390cf2439746c5da Mon Sep 17 00:00:00 2001 From: Jacob Gadikian Date: Mon, 14 Aug 2023 23:41:15 +0800 Subject: ci: use gci linter (#5708) * use gofmput to format code * use gci to format imports * reconfigure gci * linter autofixes * rearrange imports a little * export GOOS=windows golangci-lint run ./... --fix --- cmd/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index 53b8d67..1d6478a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -30,11 +30,12 @@ import ( "strings" "time" - "github.com/caddyserver/caddy/v2" - "github.com/caddyserver/caddy/v2/caddyconfig" "github.com/caddyserver/certmagic" "github.com/spf13/pflag" "go.uber.org/zap" + + "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig" ) func init() { -- cgit v1.2.3 From f2ab7099db6d8386299796e6eef8e30f65b21bcc Mon Sep 17 00:00:00 2001 From: Evan Van Dam Date: Wed, 6 Sep 2023 19:19:24 -0700 Subject: cmd: Prevent overwriting existing env vars with `--envfile` (#5803) Co-authored-by: Francis Lavoie --- cmd/main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index 1d6478a..b4e3fdc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -300,8 +300,12 @@ func loadEnvFromFile(envFile string) error { } for k, v := range envMap { - if err := os.Setenv(k, v); err != nil { - return fmt.Errorf("setting environment variables: %v", err) + // do not overwrite existing environment variables + _, exists := os.LookupEnv(k) + if !exists { + if err := os.Setenv(k, v); err != nil { + return fmt.Errorf("setting environment variables: %v", err) + } } } -- cgit v1.2.3 From 9c419f1e1a4a82a8ed49bac3d54050890cb3e58e Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Wed, 11 Oct 2023 11:46:18 -0400 Subject: cmd: Fix exiting with custom status code, add `caddy -v` (#5874) * Simplify variables for commands * Add --envfile support for adapt command * Carry custom status code for commands to os.Exit() * cmd: add `-v` and `--version` to root caddy command * Add `--envfile` to `caddy environ`, extract flag parsing to func --------- Co-authored-by: Mohammed Al Sahaf --- cmd/main.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index b4e3fdc..fa15c08 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,6 +17,7 @@ package caddycmd import ( "bufio" "bytes" + "errors" "flag" "fmt" "io" @@ -63,6 +64,10 @@ func Main() { } if err := rootCmd.Execute(); err != nil { + var exitError *exitError + if errors.As(err, &exitError) { + os.Exit(exitError.ExitCode) + } os.Exit(1) } } -- cgit v1.2.3