bouncer/internal/command/admin/proxy/flag/flag.go

80 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-04-24 20:52:12 +02:00
package flag
import (
clientFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/flag"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
func WithProxyFlags(flags ...cli.Flag) []cli.Flag {
baseFlags := clientFlag.ComposeFlags(
2023-06-23 19:08:35 +02:00
ProxyName(),
2023-04-24 20:52:12 +02:00
)
flags = append(flags, baseFlags...)
return flags
}
2023-06-23 19:08:35 +02:00
const KeyProxyName = "proxy-name"
func ProxyName() cli.Flag {
return &cli.StringFlag{
Name: KeyProxyName,
Usage: "use `PROXY_NAME` as targeted proxy",
Value: "",
Required: true,
}
}
const KeyProxyTo = "proxy-to"
func ProxyTo(required bool) cli.Flag {
2023-06-23 19:08:35 +02:00
return &cli.StringFlag{
Name: KeyProxyTo,
Usage: "Set `PROXY_TO` as proxy's destination url",
Value: "",
Required: required,
2023-06-23 19:08:35 +02:00
}
}
const KeyProxyFrom = "proxy-from"
func ProxyFrom() cli.Flag {
return &cli.StringSliceFlag{
Name: KeyProxyFrom,
Usage: "Set `PROXY_FROM` as proxy's patterns to match incoming requests",
Value: cli.NewStringSlice("*"),
}
}
const KeyProxyWeight = "proxy-weight"
func ProxyWeight() cli.Flag {
return &cli.IntFlag{
Name: KeyProxyWeight,
Usage: "Set `PROXY_WEIGHT` as proxy's weight",
}
}
const KeyProxyEnabled = "proxy-enabled"
func ProxyEnabled() cli.Flag {
return &cli.BoolFlag{
Name: KeyProxyEnabled,
Usage: "Enable or disable proxy",
}
}
2023-04-24 20:52:12 +02:00
func AssertProxyName(ctx *cli.Context) (store.ProxyName, error) {
2023-06-23 19:08:35 +02:00
rawProxyName := ctx.String(KeyProxyName)
2023-04-24 20:52:12 +02:00
name, err := store.ValidateName(rawProxyName)
if err != nil {
return "", errors.WithStack(err)
}
return store.ProxyName(name), nil
}