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

84 lines
2.2 KiB
Go
Raw Permalink Normal View History

2023-04-24 20:52:12 +02:00
package layer
import (
"encoding/json"
"os"
"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"
2023-06-23 19:08:35 +02:00
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/layer/flag"
2023-04-24 20:52:12 +02:00
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/store"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
2024-05-17 15:44:28 +02:00
"gitlab.com/wpetit/goweb/cli/format"
2023-04-24 20:52:12 +02:00
)
func UpdateCommand() *cli.Command {
return &cli.Command{
Name: "update",
Usage: "Update layer",
Flags: layerFlag.WithLayerFlags(
2023-06-23 19:08:35 +02:00
flag.LayerEnabled(),
flag.LayerWeight(),
flag.LayerOptions(),
2023-04-24 20:52:12 +02:00
),
Action: func(ctx *cli.Context) error {
baseFlags := clientFlag.GetBaseFlags(ctx)
token, err := clientFlag.GetToken(baseFlags)
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
proxyName, err := proxyFlag.AssertProxyName(ctx)
if err != nil {
return errors.WithStack(err)
}
layerName, err := layerFlag.AssertLayerName(ctx)
if err != nil {
return errors.WithStack(err)
}
opts := &client.UpdateLayerOptions{}
2023-06-23 19:08:35 +02:00
if ctx.IsSet(flag.KeyLayerOptions) {
2023-04-24 20:52:12 +02:00
var options store.LayerOptions
2023-06-23 19:08:35 +02:00
if err := json.Unmarshal([]byte(ctx.String(flag.KeyLayerOptions)), &options); err != nil {
return errors.Wrap(err, "could not parse layer's options")
2023-04-24 20:52:12 +02:00
}
opts.Options = &options
}
2023-06-23 19:08:35 +02:00
if ctx.IsSet(flag.KeyLayerWeight) {
weight := ctx.Int(flag.KeyLayerWeight)
2023-04-24 20:52:12 +02:00
opts.Weight = &weight
}
2023-06-23 19:08:35 +02:00
if ctx.IsSet(flag.KeyLayerEnabled) {
enabled := ctx.Bool(flag.KeyLayerEnabled)
2023-04-24 20:52:12 +02:00
opts.Enabled = &enabled
}
client := client.New(baseFlags.ServerURL, client.WithToken(token))
proxy, err := client.UpdateLayer(ctx.Context, proxyName, layerName, opts)
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
hints := layerHints(baseFlags.OutputMode)
if err := format.Write(baseFlags.Format, os.Stdout, hints, proxy); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}