diff options
| author | Francis Lavoie <lavofr@gmail.com> | 2023-02-24 18:09:12 -0500 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-24 16:09:12 -0700 | 
| commit | 9e6919550be5689628d0020ec14e90ea6f527716 (patch) | |
| tree | 81865f63e5ef549f8548cd6e22e9aa89d4eaf207 /modules/caddyhttp/reverseproxy/command.go | |
| parent | 167981d258f41b7ef931d510056a5a5fdc9cbd0d (diff) | |
cmd: Expand cobra support, add short flags (#5379)
* cmd: Expand cobra support
* Convert commands to cobra, add short flags
* Fix version command typo
Co-authored-by: Emily Lange <git@indeednotjames.com>
* Apply suggestions from code review
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
---------
Co-authored-by: Emily Lange <git@indeednotjames.com>
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Diffstat (limited to 'modules/caddyhttp/reverseproxy/command.go')
| -rw-r--r-- | modules/caddyhttp/reverseproxy/command.go | 46 | 
1 files changed, 25 insertions, 21 deletions
| diff --git a/modules/caddyhttp/reverseproxy/command.go b/modules/caddyhttp/reverseproxy/command.go index 04fb9b4..8c171ec 100644 --- a/modules/caddyhttp/reverseproxy/command.go +++ b/modules/caddyhttp/reverseproxy/command.go @@ -16,7 +16,6 @@ package reverseproxy  import (  	"encoding/json" -	"flag"  	"fmt"  	"net/http"  	"strconv" @@ -28,14 +27,14 @@ import (  	"github.com/caddyserver/caddy/v2/modules/caddyhttp"  	"github.com/caddyserver/caddy/v2/modules/caddyhttp/headers"  	"github.com/caddyserver/caddy/v2/modules/caddytls" +	"github.com/spf13/cobra"  	"go.uber.org/zap"  )  func init() {  	caddycmd.RegisterCommand(caddycmd.Command{  		Name:  "reverse-proxy", -		Func:  cmdReverseProxy, -		Usage: "[--from <addr>] [--to <addr>] [--change-host-header] [--insecure] [--internal-certs] [--disable-redirects]", +		Usage: "[--from <addr>] [--to <addr>] [--change-host-header] [--insecure] [--internal-certs] [--disable-redirects] [--access-log]",  		Short: "A quick and production-ready reverse proxy",  		Long: `  A simple but production-ready reverse proxy. Useful for quick deployments, @@ -63,17 +62,17 @@ For proxying:    --insecure disables TLS verification with the upstream. WARNING: THIS      DISABLES SECURITY BY NOT VERIFYING THE UPSTREAM'S CERTIFICATE.  `, -		Flags: func() *flag.FlagSet { -			fs := flag.NewFlagSet("reverse-proxy", flag.ExitOnError) -			fs.String("from", "localhost", "Address on which to receive traffic") -			fs.Var(&reverseProxyCmdTo, "to", "Upstream address(es) to which traffic should be sent") -			fs.Bool("change-host-header", false, "Set upstream Host header to address of upstream") -			fs.Bool("insecure", false, "Disable TLS verification (WARNING: DISABLES SECURITY BY NOT VERIFYING TLS CERTIFICATES!)") -			fs.Bool("internal-certs", false, "Use internal CA for issuing certs") -			fs.Bool("debug", false, "Enable verbose debug logs") -			fs.Bool("disable-redirects", false, "Disable HTTP->HTTPS redirects") -			return fs -		}(), +		CobraFunc: func(cmd *cobra.Command) { +			cmd.Flags().StringP("from", "f", "localhost", "Address on which to receive traffic") +			cmd.Flags().StringSliceP("to", "t", []string{}, "Upstream address(es) to which traffic should be sent") +			cmd.Flags().BoolP("change-host-header", "c", false, "Set upstream Host header to address of upstream") +			cmd.Flags().BoolP("insecure", "", false, "Disable TLS verification (WARNING: DISABLES SECURITY BY NOT VERIFYING TLS CERTIFICATES!)") +			cmd.Flags().BoolP("disable-redirects", "r", false, "Disable HTTP->HTTPS redirects") +			cmd.Flags().BoolP("internal-certs", "i", false, "Use internal CA for issuing certs") +			cmd.Flags().BoolP("access-log", "", false, "Enable the access log") +			cmd.Flags().BoolP("debug", "v", false, "Enable verbose debug logs") +			cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdReverseProxy) +		},  	})  } @@ -83,14 +82,19 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {  	from := fs.String("from")  	changeHost := fs.Bool("change-host-header")  	insecure := fs.Bool("insecure") +	disableRedir := fs.Bool("disable-redirects")  	internalCerts := fs.Bool("internal-certs") +	accessLog := fs.Bool("access-log")  	debug := fs.Bool("debug") -	disableRedir := fs.Bool("disable-redirects")  	httpPort := strconv.Itoa(caddyhttp.DefaultHTTPPort)  	httpsPort := strconv.Itoa(caddyhttp.DefaultHTTPSPort) -	if len(reverseProxyCmdTo) == 0 { +	to, err := fs.GetStringSlice("to") +	if err != nil { +		return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid to flag: %v", err) +	} +	if len(to) == 0 {  		return caddy.ExitCodeFailedStartup, fmt.Errorf("--to is required")  	} @@ -119,9 +123,9 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {  	// set up the upstream address; assume missing information from given parts  	// mixing schemes isn't supported, so use first defined (if available) -	toAddresses := make([]string, len(reverseProxyCmdTo)) +	toAddresses := make([]string, len(to))  	var toScheme string -	for i, toLoc := range reverseProxyCmdTo { +	for i, toLoc := range to {  		addr, scheme, err := parseUpstreamDialAddress(toLoc)  		if err != nil {  			return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid upstream address %s: %v", toLoc, err) @@ -180,6 +184,9 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {  		Routes: caddyhttp.RouteList{route},  		Listen: []string{":" + fromAddr.Port},  	} +	if accessLog { +		server.Logs = &caddyhttp.ServerLogConfig{} +	}  	if fromAddr.Scheme == "http" {  		server.AutoHTTPS = &caddyhttp.AutoHTTPSConfig{Disabled: true} @@ -238,6 +245,3 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {  	select {}  } - -// reverseProxyCmdTo holds the parsed values from repeated use of the --to flag. -var reverseProxyCmdTo caddycmd.StringSlice | 
