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

84 lines
2.2 KiB
Go

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"
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/layer/flag"
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"
"gitlab.com/wpetit/goweb/cli/format"
)
func UpdateCommand() *cli.Command {
return &cli.Command{
Name: "update",
Usage: "Update layer",
Flags: layerFlag.WithLayerFlags(
flag.LayerEnabled(),
flag.LayerWeight(),
flag.LayerOptions(),
),
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{}
if ctx.IsSet(flag.KeyLayerOptions) {
var options store.LayerOptions
if err := json.Unmarshal([]byte(ctx.String(flag.KeyLayerOptions)), &options); err != nil {
return errors.Wrap(err, "could not parse layer's options")
}
opts.Options = &options
}
if ctx.IsSet(flag.KeyLayerWeight) {
weight := ctx.Int(flag.KeyLayerWeight)
opts.Weight = &weight
}
if ctx.IsSet(flag.KeyLayerEnabled) {
enabled := ctx.Bool(flag.KeyLayerEnabled)
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
},
}
}