94 lines
2.1 KiB
Go
94 lines
2.1 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"
|
|
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
|
|
"forge.cadoles.com/cadoles/bouncer/internal/format"
|
|
"github.com/pkg/errors"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func UpdateCommand() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "update",
|
|
Usage: "Update proxy",
|
|
Flags: proxyFlag.WithProxyFlags(
|
|
&cli.StringFlag{
|
|
Name: "to",
|
|
Usage: "Set `TO` as proxy's destination url",
|
|
},
|
|
&cli.StringSliceFlag{
|
|
Name: "from",
|
|
Usage: "Set `FROM` as proxy's patterns to match incoming requests",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "enabled",
|
|
Usage: "Enable or disable proxy",
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "weight",
|
|
Usage: "Set `WEIGHT` as proxy's weight",
|
|
},
|
|
),
|
|
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("to") {
|
|
to := ctx.String("to")
|
|
if _, err := url.Parse(to); err != nil {
|
|
return errors.Wrap(err, "'to' parameter should be a valid url")
|
|
}
|
|
|
|
opts.To = &to
|
|
}
|
|
|
|
from := ctx.StringSlice("from")
|
|
if from != nil {
|
|
opts.From = from
|
|
}
|
|
|
|
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.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
|
|
},
|
|
}
|
|
}
|