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

85 lines
2.0 KiB
Go
Raw Normal View History

2023-04-24 20:52:12 +02:00
package proxy
import (
"net/url"
"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/proxy/flag"
2023-04-24 20:52:12 +02:00
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
"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 proxy",
Flags: proxyFlag.WithProxyFlags(
flag.ProxyTo(false),
2023-06-23 19:08:35 +02:00
flag.ProxyFrom(),
flag.ProxyEnabled(),
flag.ProxyWeight(),
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)
}
opts := &client.UpdateProxyOptions{}
2023-06-23 19:08:35 +02:00
if ctx.IsSet(flag.KeyProxyTo) {
to := ctx.String(flag.KeyProxyTo)
2023-04-24 20:52:12 +02:00
if _, err := url.Parse(to); err != nil {
2023-06-23 19:08:35 +02:00
return errors.Wrapf(err, "'%s' parameter should be a valid url", flag.KeyProxyTo)
2023-04-24 20:52:12 +02:00
}
opts.To = &to
}
2023-06-23 19:08:35 +02:00
if ctx.IsSet(flag.KeyProxyFrom) {
from := ctx.StringSlice(flag.KeyProxyFrom)
if from != nil {
opts.From = from
}
2023-04-24 20:52:12 +02:00
}
2023-06-23 19:08:35 +02:00
if ctx.IsSet(flag.KeyProxyWeight) {
weight := ctx.Int(flag.KeyProxyWeight)
2023-04-24 20:52:12 +02:00
opts.Weight = &weight
}
2023-06-23 19:08:35 +02:00
if ctx.IsSet(flag.KeyProxyEnabled) {
enabled := ctx.Bool(flag.KeyProxyEnabled)
2023-04-24 20:52:12 +02:00
opts.Enabled = &enabled
}
client := client.New(baseFlags.ServerURL, client.WithToken(token))
proxy, err := client.UpdateProxy(ctx.Context, proxyName, opts)
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
hints := proxyHints(baseFlags.OutputMode)
if err := format.Write(baseFlags.Format, os.Stdout, hints, proxy); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}