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

118 lines
2.4 KiB
Go
Raw Normal View History

2023-04-24 20:52:12 +02:00
package flag
import (
"encoding/json"
2023-06-23 19:08:35 +02:00
"fmt"
2023-04-24 20:52:12 +02:00
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
2023-06-23 19:08:35 +02:00
"forge.cadoles.com/cadoles/bouncer/internal/setup"
2023-04-24 20:52:12 +02:00
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
const (
2023-06-23 19:08:35 +02:00
FlagKeyLayerType = "layer-type"
2023-04-24 20:52:12 +02:00
)
func WithLayerFlags(flags ...cli.Flag) []cli.Flag {
baseFlags := proxyFlag.WithProxyFlags(
2023-06-23 19:08:35 +02:00
LayerName(),
2023-04-24 20:52:12 +02:00
)
flags = append(flags, baseFlags...)
return flags
}
func WithLayerCreateFlags(flags ...cli.Flag) []cli.Flag {
return WithLayerFlags(
2023-06-23 19:08:35 +02:00
LayerType(),
LayerOptions(),
2023-04-24 20:52:12 +02:00
)
}
2023-06-23 19:08:35 +02:00
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",
}
}
2023-04-24 20:52:12 +02:00
func AssertLayerName(ctx *cli.Context) (store.LayerName, error) {
2023-06-23 19:08:35 +02:00
rawLayerName := ctx.String(KeyLayerName)
2023-04-24 20:52:12 +02:00
name, err := store.ValidateName(rawLayerName)
if err != nil {
return "", errors.WithStack(err)
}
return store.LayerName(name), nil
}
func AssertLayerType(ctx *cli.Context) (store.LayerType, error) {
2023-06-23 19:08:35 +02:00
rawLayerType := ctx.String(FlagKeyLayerType)
layerType := store.LayerType(rawLayerType)
if !setup.LayerTypeExists(layerType) {
return "", errors.Errorf("unknown layer type '%s'", layerType)
}
2023-04-24 20:52:12 +02:00
2023-06-23 19:08:35 +02:00
return layerType, nil
2023-04-24 20:52:12 +02:00
}
func AssertLayerOptions(ctx *cli.Context) (store.LayerOptions, error) {
2023-06-23 19:08:35 +02:00
rawLayerOptions := ctx.String(KeyLayerOptions)
2023-04-24 20:52:12 +02:00
layerOptions := store.LayerOptions{}
if err := json.Unmarshal([]byte(rawLayerOptions), &layerOptions); err != nil {
return nil, errors.WithStack(err)
}
return layerOptions, nil
}