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

92 lines
2.2 KiB
Go
Raw 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"
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/format"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
func UpdateCommand() *cli.Command {
return &cli.Command{
Name: "update",
Usage: "Update layer",
Flags: layerFlag.WithLayerFlags(
&cli.BoolFlag{
Name: "enabled",
Usage: "Enable or disable proxy",
},
&cli.IntFlag{
Name: "weight",
Usage: "Set `WEIGHT` as proxy's weight",
},
&cli.StringFlag{
Name: "options",
Usage: "Set `OPTIONS` as proxy's options",
},
),
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("options") {
var options store.LayerOptions
if err := json.Unmarshal([]byte(ctx.String("options")), &options); err != nil {
return errors.Wrap(err, "could not parse options")
}
opts.Options = &options
}
if ctx.IsSet("weight") {
weight := ctx.Int("weight")
opts.Weight = &weight
}
if ctx.IsSet("enabled") {
enabled := ctx.Bool("enabled")
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
},
}
}