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

62 lines
1.6 KiB
Go
Raw Permalink 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 CreateCommand() *cli.Command {
return &cli.Command{
Name: "create",
Usage: "Create proxy",
Flags: proxyFlag.WithProxyFlags(
2024-06-26 16:22:30 +02:00
flag.ProxyTo(),
2023-06-23 19:08:35 +02:00
flag.ProxyFrom(),
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.Wrap(err, "'to' parameter should be a valid url")
}
2023-06-23 19:08:35 +02:00
to, err := url.Parse(ctx.String(flag.KeyProxyTo))
2023-04-24 20:52:12 +02:00
if err != nil {
return errors.Wrap(err, "'to' parameter should be a valid url")
}
2023-06-23 19:08:35 +02:00
from := ctx.StringSlice(flag.KeyProxyFrom)
2023-04-24 20:52:12 +02:00
client := client.New(baseFlags.ServerURL, client.WithToken(token))
proxy, err := client.CreateProxy(ctx.Context, proxyName, to, from)
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
},
}
}