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

85 lines
2.0 KiB
Go

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"
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
"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 proxy",
Flags: proxyFlag.WithProxyFlags(
flag.ProxyTo(),
flag.ProxyFrom(),
flag.ProxyEnabled(),
flag.ProxyWeight(),
),
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{}
if ctx.IsSet(flag.KeyProxyTo) {
to := ctx.String(flag.KeyProxyTo)
if _, err := url.Parse(to); err != nil {
return errors.Wrapf(err, "'%s' parameter should be a valid url", flag.KeyProxyTo)
}
opts.To = &to
}
if ctx.IsSet(flag.KeyProxyFrom) {
from := ctx.StringSlice(flag.KeyProxyFrom)
if from != nil {
opts.From = from
}
}
if ctx.IsSet(flag.KeyProxyWeight) {
weight := ctx.Int(flag.KeyProxyWeight)
opts.Weight = &weight
}
if ctx.IsSet(flag.KeyProxyEnabled) {
enabled := ctx.Bool(flag.KeyProxyEnabled)
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
},
}
}