feat: command flags cleanup
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
This commit is contained in:
@ -2,27 +2,22 @@ package flag
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/setup"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
FlagLayerName = "layer-name"
|
||||
FlagLayerType = "layer-type"
|
||||
FlagLayerOptions = "layer-options"
|
||||
FlagKeyLayerType = "layer-type"
|
||||
)
|
||||
|
||||
func WithLayerFlags(flags ...cli.Flag) []cli.Flag {
|
||||
baseFlags := proxyFlag.WithProxyFlags(
|
||||
&cli.StringFlag{
|
||||
Name: FlagLayerName,
|
||||
Usage: "use `LAYER_NAME` as targeted layer",
|
||||
Value: "",
|
||||
Required: true,
|
||||
},
|
||||
LayerName(),
|
||||
)
|
||||
|
||||
flags = append(flags, baseFlags...)
|
||||
@ -32,22 +27,63 @@ func WithLayerFlags(flags ...cli.Flag) []cli.Flag {
|
||||
|
||||
func WithLayerCreateFlags(flags ...cli.Flag) []cli.Flag {
|
||||
return WithLayerFlags(
|
||||
&cli.StringFlag{
|
||||
Name: FlagLayerType,
|
||||
Usage: "Set `LAYER_TYPE` as layer's type",
|
||||
Value: "",
|
||||
Required: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: FlagLayerOptions,
|
||||
Usage: "Set `LAYER_OPTIONS` as layer's options",
|
||||
Value: "{}",
|
||||
},
|
||||
LayerType(),
|
||||
LayerOptions(),
|
||||
)
|
||||
}
|
||||
|
||||
const KeyLayerName = "layer-name"
|
||||
|
||||
func LayerName() cli.Flag {
|
||||
return &cli.StringFlag{
|
||||
Name: KeyLayerName,
|
||||
Usage: "use `LAYER_NAME` as targeted layer",
|
||||
Value: "",
|
||||
Required: true,
|
||||
}
|
||||
}
|
||||
|
||||
const KeyLayerType = "layer-type"
|
||||
|
||||
func LayerType() cli.Flag {
|
||||
return &cli.StringFlag{
|
||||
Name: KeyLayerType,
|
||||
Usage: fmt.Sprintf("Set `LAYER_TYPE` as layer's type (available: %v)", setup.GetLayerTypes()),
|
||||
Value: "",
|
||||
Required: true,
|
||||
}
|
||||
}
|
||||
|
||||
const KeyLayerOptions = "layer-options"
|
||||
|
||||
func LayerOptions() cli.Flag {
|
||||
return &cli.StringFlag{
|
||||
Name: KeyLayerOptions,
|
||||
Usage: "Set `LAYER_OPTIONS` as layer's options",
|
||||
Value: "{}",
|
||||
}
|
||||
}
|
||||
|
||||
const KeyLayerWeight = "layer-weight"
|
||||
|
||||
func LayerWeight() cli.Flag {
|
||||
return &cli.IntFlag{
|
||||
Name: KeyLayerWeight,
|
||||
Usage: "Set `LAYER_WEIGHT` as layer's weight",
|
||||
}
|
||||
}
|
||||
|
||||
const KeyLayerEnabled = "layer-enabled"
|
||||
|
||||
func LayerEnabled() cli.Flag {
|
||||
return &cli.BoolFlag{
|
||||
Name: KeyLayerEnabled,
|
||||
Usage: "Enable or disable layer",
|
||||
}
|
||||
}
|
||||
|
||||
func AssertLayerName(ctx *cli.Context) (store.LayerName, error) {
|
||||
rawLayerName := ctx.String(FlagLayerName)
|
||||
rawLayerName := ctx.String(KeyLayerName)
|
||||
|
||||
name, err := store.ValidateName(rawLayerName)
|
||||
if err != nil {
|
||||
@ -58,13 +94,18 @@ func AssertLayerName(ctx *cli.Context) (store.LayerName, error) {
|
||||
}
|
||||
|
||||
func AssertLayerType(ctx *cli.Context) (store.LayerType, error) {
|
||||
rawLayerType := ctx.String(FlagLayerType)
|
||||
rawLayerType := ctx.String(FlagKeyLayerType)
|
||||
|
||||
return store.LayerType(rawLayerType), nil
|
||||
layerType := store.LayerType(rawLayerType)
|
||||
if !setup.LayerTypeExists(layerType) {
|
||||
return "", errors.Errorf("unknown layer type '%s'", layerType)
|
||||
}
|
||||
|
||||
return layerType, nil
|
||||
}
|
||||
|
||||
func AssertLayerOptions(ctx *cli.Context) (store.LayerOptions, error) {
|
||||
rawLayerOptions := ctx.String(FlagLayerOptions)
|
||||
rawLayerOptions := ctx.String(KeyLayerOptions)
|
||||
|
||||
layerOptions := store.LayerOptions{}
|
||||
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/client"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/apierr"
|
||||
clientFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/flag"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/layer/flag"
|
||||
layerFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/layer/flag"
|
||||
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/format"
|
||||
@ -20,18 +21,9 @@ func UpdateCommand() *cli.Command {
|
||||
Name: "update",
|
||||
Usage: "Update layer",
|
||||
Flags: layerFlag.WithLayerFlags(
|
||||
&cli.BoolFlag{
|
||||
Name: "enabled",
|
||||
Usage: "Enable or disable proxy",
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "weight",
|
||||
Usage: "Set `WEIGHT` as proxy's weight",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "options",
|
||||
Usage: "Set `OPTIONS` as proxy's options",
|
||||
},
|
||||
flag.LayerEnabled(),
|
||||
flag.LayerWeight(),
|
||||
flag.LayerOptions(),
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
baseFlags := clientFlag.GetBaseFlags(ctx)
|
||||
@ -53,22 +45,22 @@ func UpdateCommand() *cli.Command {
|
||||
|
||||
opts := &client.UpdateLayerOptions{}
|
||||
|
||||
if ctx.IsSet("options") {
|
||||
if ctx.IsSet(flag.KeyLayerOptions) {
|
||||
var options store.LayerOptions
|
||||
if err := json.Unmarshal([]byte(ctx.String("options")), &options); err != nil {
|
||||
return errors.Wrap(err, "could not parse options")
|
||||
if err := json.Unmarshal([]byte(ctx.String(flag.KeyLayerOptions)), &options); err != nil {
|
||||
return errors.Wrap(err, "could not parse layer's options")
|
||||
}
|
||||
|
||||
opts.Options = &options
|
||||
}
|
||||
|
||||
if ctx.IsSet("weight") {
|
||||
weight := ctx.Int("weight")
|
||||
if ctx.IsSet(flag.KeyLayerWeight) {
|
||||
weight := ctx.Int(flag.KeyLayerWeight)
|
||||
opts.Weight = &weight
|
||||
}
|
||||
|
||||
if ctx.IsSet("enabled") {
|
||||
enabled := ctx.Bool("enabled")
|
||||
if ctx.IsSet(flag.KeyLayerEnabled) {
|
||||
enabled := ctx.Bool(flag.KeyLayerEnabled)
|
||||
opts.Enabled = &enabled
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user