aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/staticresp.go
diff options
context:
space:
mode:
authorFrancis Lavoie <[email protected]>2023-02-24 18:09:12 -0500
committerGitHub <[email protected]>2023-02-24 16:09:12 -0700
commit9e6919550be5689628d0020ec14e90ea6f527716 (patch)
tree81865f63e5ef549f8548cd6e22e9aa89d4eaf207 /modules/caddyhttp/staticresp.go
parent167981d258f41b7ef931d510056a5a5fdc9cbd0d (diff)
downloadcaddy-9e6919550be5689628d0020ec14e90ea6f527716.tar.gz
caddy-9e6919550be5689628d0020ec14e90ea6f527716.zip
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 <[email protected]> * Apply suggestions from code review Co-authored-by: Matt Holt <[email protected]> --------- Co-authored-by: Emily Lange <[email protected]> Co-authored-by: Matt Holt <[email protected]>
Diffstat (limited to 'modules/caddyhttp/staticresp.go')
-rw-r--r--modules/caddyhttp/staticresp.go31
1 files changed, 15 insertions, 16 deletions
diff --git a/modules/caddyhttp/staticresp.go b/modules/caddyhttp/staticresp.go
index add5b121b..67614c2bb 100644
--- a/modules/caddyhttp/staticresp.go
+++ b/modules/caddyhttp/staticresp.go
@@ -17,7 +17,6 @@ package caddyhttp
import (
"bytes"
"encoding/json"
- "flag"
"fmt"
"io"
"net/http"
@@ -32,6 +31,7 @@ import (
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
+ "github.com/spf13/cobra"
"go.uber.org/zap"
)
@@ -39,7 +39,6 @@ func init() {
caddy.RegisterModule(StaticResponse{})
caddycmd.RegisterCommand(caddycmd.Command{
Name: "respond",
- Func: cmdRespond,
Usage: `[--status <code>] [--body <content>] [--listen <addr>] [--access-log] [--debug] [--header "Field: value"] <body|status>`,
Short: "Simple, hard-coded HTTP responses for development and testing",
Long: `
@@ -71,16 +70,15 @@ Access/request logging and more verbose debug logging can also be enabled.
Response headers may be added using the --header flag for each header field.
`,
- Flags: func() *flag.FlagSet {
- fs := flag.NewFlagSet("respond", flag.ExitOnError)
- fs.String("listen", ":0", "The address to which to bind the listener")
- fs.Int("status", http.StatusOK, "The response status code")
- fs.String("body", "", "The body of the HTTP response")
- fs.Bool("access-log", false, "Enable the access log")
- fs.Bool("debug", false, "Enable more verbose debug-level logging")
- fs.Var(&respondCmdHeaders, "header", "Set a header on the response (format: \"Field: value\"")
- return fs
- }(),
+ CobraFunc: func(cmd *cobra.Command) {
+ cmd.Flags().StringP("listen", "l", ":0", "The address to which to bind the listener")
+ cmd.Flags().IntP("status", "s", http.StatusOK, "The response status code")
+ cmd.Flags().StringP("body", "b", "", "The body of the HTTP response")
+ cmd.Flags().BoolP("access-log", "", false, "Enable the access log")
+ cmd.Flags().BoolP("debug", "v", false, "Enable more verbose debug-level logging")
+ cmd.Flags().StringSliceP("header", "H", []string{}, "Set a header on the response (format: \"Field: value\")")
+ cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdRespond)
+ },
})
}
@@ -318,8 +316,12 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
}
// build headers map
+ headers, err := fl.GetStringSlice("header")
+ if err != nil {
+ return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid header flag: %v", err)
+ }
hdr := make(http.Header)
- for i, h := range respondCmdHeaders {
+ for i, h := range headers {
key, val, found := strings.Cut(h, ":")
key, val = strings.TrimSpace(key), strings.TrimSpace(val)
if !found || key == "" || val == "" {
@@ -432,9 +434,6 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
select {}
}
-// respondCmdHeaders holds the parsed values from repeated use of the --header flag.
-var respondCmdHeaders caddycmd.StringSlice
-
// Interface guards
var (
_ MiddlewareHandler = (*StaticResponse)(nil)