76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package proxy
|
|
|
|
import (
|
|
"net/url"
|
|
"os"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/client"
|
|
"forge.cadoles.com/cadoles/bouncer/internal/command/client/apierr"
|
|
clientFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/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 CreateCommand() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "create",
|
|
Usage: "Create proxy",
|
|
Flags: clientFlag.ComposeFlags(
|
|
&cli.StringFlag{
|
|
Name: "name",
|
|
Usage: "Set `NAME` as proxy's name",
|
|
Value: "",
|
|
Required: true,
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "to",
|
|
Usage: "Set `TO` as proxy's destination url",
|
|
Value: "",
|
|
Required: true,
|
|
},
|
|
&cli.StringSliceFlag{
|
|
Name: "from",
|
|
Usage: "Set `FROM` as proxy's patterns to match incoming requests",
|
|
Value: cli.NewStringSlice("*"),
|
|
},
|
|
),
|
|
Action: func(ctx *cli.Context) error {
|
|
baseFlags := clientFlag.GetBaseFlags(ctx)
|
|
|
|
token, err := clientFlag.GetToken(baseFlags)
|
|
if err != nil {
|
|
return errors.WithStack(apierr.Wrap(err))
|
|
}
|
|
|
|
name, err := store.ValidateName(ctx.String("name"))
|
|
if err != nil {
|
|
return errors.Wrap(err, "'to' parameter should be a valid url")
|
|
}
|
|
|
|
to, err := url.Parse(ctx.String("to"))
|
|
if err != nil {
|
|
return errors.Wrap(err, "'to' parameter should be a valid url")
|
|
}
|
|
|
|
from := ctx.StringSlice("from")
|
|
|
|
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
|
|
|
proxy, err := client.CreateProxy(ctx.Context, store.ProxyName(name), 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
|
|
},
|
|
}
|
|
}
|