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 ( FlagKeyLayerType = "layer-type" ) func WithLayerFlags(flags ...cli.Flag) []cli.Flag { baseFlags := proxyFlag.WithProxyFlags( LayerName(), ) flags = append(flags, baseFlags...) return flags } func WithLayerCreateFlags(flags ...cli.Flag) []cli.Flag { return WithLayerFlags( 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(KeyLayerName) 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) { rawLayerType := ctx.String(FlagKeyLayerType) 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(KeyLayerOptions) layerOptions := store.LayerOptions{} if err := json.Unmarshal([]byte(rawLayerOptions), &layerOptions); err != nil { return nil, errors.WithStack(err) } return layerOptions, nil }