bouncer/internal/command/client/proxy/layer/flag/flag.go

77 lines
1.7 KiB
Go
Raw Normal View History

2023-04-24 20:52:12 +02:00
package flag
import (
"encoding/json"
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/flag"
"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"
)
func WithLayerFlags(flags ...cli.Flag) []cli.Flag {
baseFlags := proxyFlag.WithProxyFlags(
&cli.StringFlag{
Name: FlagLayerName,
Usage: "use `LAYER_NAME` as targeted layer",
Value: "",
Required: true,
},
)
flags = append(flags, baseFlags...)
return flags
}
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: "{}",
},
)
}
func AssertLayerName(ctx *cli.Context) (store.LayerName, error) {
rawLayerName := ctx.String(FlagLayerName)
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(FlagLayerType)
return store.LayerType(rawLayerType), nil
}
func AssertLayerOptions(ctx *cli.Context) (store.LayerOptions, error) {
rawLayerOptions := ctx.String(FlagLayerOptions)
layerOptions := store.LayerOptions{}
if err := json.Unmarshal([]byte(rawLayerOptions), &layerOptions); err != nil {
return nil, errors.WithStack(err)
}
return layerOptions, nil
}