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

73 lines
1.8 KiB
Go

package layer
import (
"os"
"forge.cadoles.com/cadoles/bouncer/internal/client"
"forge.cadoles.com/cadoles/bouncer/internal/command/client/apierr"
clientFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/flag"
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/flag"
"forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/layer/flag"
layerFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/layer/flag"
"forge.cadoles.com/cadoles/bouncer/internal/format"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
func CreateCommand() *cli.Command {
return &cli.Command{
Name: "create",
Usage: "Create layer",
Flags: layerFlag.WithLayerCreateFlags(),
Action: func(ctx *cli.Context) error {
baseFlags := clientFlag.GetBaseFlags(ctx)
token, err := clientFlag.GetToken(baseFlags)
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
layerName, err := flag.AssertLayerName(ctx)
if err != nil {
return errors.WithStack(err)
}
proxyName, err := proxyFlag.AssertProxyName(ctx)
if err != nil {
return errors.WithStack(err)
}
layerType, err := flag.AssertLayerType(ctx)
if err != nil {
return errors.WithStack(err)
}
layerOptions, err := flag.AssertLayerOptions(ctx)
if err != nil {
return errors.WithStack(err)
}
client := client.New(baseFlags.ServerURL, client.WithToken(token))
layer, err := client.CreateLayer(
ctx.Context,
proxyName,
layerName,
layerType,
layerOptions,
)
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
hints := layerHints(baseFlags.OutputMode)
if err := format.Write(baseFlags.Format, os.Stdout, hints, layer); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}