feat: initial commit
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
This commit is contained in:
75
internal/command/client/proxy/create.go
Normal file
75
internal/command/client/proxy/create.go
Normal file
@ -0,0 +1,75 @@
|
||||
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
|
||||
},
|
||||
}
|
||||
}
|
56
internal/command/client/proxy/delete.go
Normal file
56
internal/command/client/proxy/delete.go
Normal file
@ -0,0 +1,56 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"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"
|
||||
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/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 DeleteCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "Delete proxy",
|
||||
Flags: proxyFlag.WithProxyFlags(),
|
||||
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)
|
||||
}
|
||||
|
||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
proxyName, err = client.DeleteProxy(ctx.Context, proxyName)
|
||||
if err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
hints := format.Hints{
|
||||
OutputMode: baseFlags.OutputMode,
|
||||
}
|
||||
|
||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, struct {
|
||||
Name store.ProxyName `json:"id"`
|
||||
}{
|
||||
Name: proxyName,
|
||||
}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
34
internal/command/client/proxy/flag/flag.go
Normal file
34
internal/command/client/proxy/flag/flag.go
Normal file
@ -0,0 +1,34 @@
|
||||
package flag
|
||||
|
||||
import (
|
||||
clientFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/flag"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func WithProxyFlags(flags ...cli.Flag) []cli.Flag {
|
||||
baseFlags := clientFlag.ComposeFlags(
|
||||
&cli.StringFlag{
|
||||
Name: "name",
|
||||
Usage: "use `NAME` as targeted proxy",
|
||||
Value: "",
|
||||
Required: true,
|
||||
},
|
||||
)
|
||||
|
||||
flags = append(flags, baseFlags...)
|
||||
|
||||
return flags
|
||||
}
|
||||
|
||||
func AssertProxyName(ctx *cli.Context) (store.ProxyName, error) {
|
||||
rawProxyName := ctx.String("name")
|
||||
|
||||
name, err := store.ValidateName(rawProxyName)
|
||||
if err != nil {
|
||||
return "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
return store.ProxyName(name), nil
|
||||
}
|
49
internal/command/client/proxy/get.go
Normal file
49
internal/command/client/proxy/get.go
Normal file
@ -0,0 +1,49 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"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"
|
||||
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/flag"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/format"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func GetCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "get",
|
||||
Usage: "Get proxy",
|
||||
Flags: proxyFlag.WithProxyFlags(),
|
||||
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)
|
||||
}
|
||||
|
||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
proxy, err := client.GetProxy(ctx.Context, proxyName)
|
||||
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
|
||||
},
|
||||
}
|
||||
}
|
63
internal/command/client/proxy/query.go
Normal file
63
internal/command/client/proxy/query.go
Normal file
@ -0,0 +1,63 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"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 QueryCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "query",
|
||||
Usage: "Query proxies",
|
||||
Flags: clientFlag.ComposeFlags(
|
||||
&cli.Int64SliceFlag{
|
||||
Name: "ids",
|
||||
Usage: "use `IDS` as query filter",
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
baseFlags := clientFlag.GetBaseFlags(ctx)
|
||||
|
||||
token, err := clientFlag.GetToken(baseFlags)
|
||||
if err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
options := make([]client.QueryProxyOptionFunc, 0)
|
||||
|
||||
rawNames := ctx.StringSlice("ids")
|
||||
if rawNames != nil {
|
||||
proxyNames := func(names []string) []store.ProxyName {
|
||||
proxyNames := make([]store.ProxyName, len(names))
|
||||
for i, name := range names {
|
||||
proxyNames[i] = store.ProxyName(name)
|
||||
}
|
||||
return proxyNames
|
||||
}(rawNames)
|
||||
options = append(options, client.WithQueryProxyNames(proxyNames...))
|
||||
}
|
||||
|
||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
proxies, err := client.QueryProxy(ctx.Context, options...)
|
||||
if err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
hints := proxyHeaderHints(baseFlags.OutputMode)
|
||||
|
||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(proxies)...); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
18
internal/command/client/proxy/root.go
Normal file
18
internal/command/client/proxy/root.go
Normal file
@ -0,0 +1,18 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Root() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "proxy",
|
||||
Usage: "Execute actions related to proxies",
|
||||
Subcommands: []*cli.Command{
|
||||
GetCommand(),
|
||||
CreateCommand(),
|
||||
QueryCommand(),
|
||||
DeleteCommand(),
|
||||
},
|
||||
}
|
||||
}
|
72
internal/command/client/proxy/update.go
Normal file
72
internal/command/client/proxy/update.go
Normal file
@ -0,0 +1,72 @@
|
||||
package proxy
|
||||
|
||||
// import (
|
||||
// "os"
|
||||
|
||||
// "forge.cadoles.com/Cadoles/emissary/internal/client"
|
||||
// agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/agent/flag"
|
||||
// "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr"
|
||||
// clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag"
|
||||
// "forge.cadoles.com/Cadoles/emissary/internal/format"
|
||||
// "github.com/pkg/errors"
|
||||
// "github.com/urfave/cli/v2"
|
||||
// )
|
||||
|
||||
// func UpdateCommand() *cli.Command {
|
||||
// return &cli.Command{
|
||||
// Name: "update",
|
||||
// Usage: "Updata agent",
|
||||
// Flags: agentFlag.WithAgentFlags(
|
||||
// &cli.IntFlag{
|
||||
// Name: "status",
|
||||
// Usage: "Set `STATUS` to selected agent",
|
||||
// Value: -1,
|
||||
// },
|
||||
// &cli.StringFlag{
|
||||
// Name: "label",
|
||||
// Usage: "Set `LABEL` to selected agent",
|
||||
// Value: "",
|
||||
// },
|
||||
// ),
|
||||
// Action: func(ctx *cli.Context) error {
|
||||
// baseFlags := clientFlag.GetBaseFlags(ctx)
|
||||
|
||||
// token, err := clientFlag.GetToken(baseFlags)
|
||||
// if err != nil {
|
||||
// return errors.WithStack(apierr.Wrap(err))
|
||||
// }
|
||||
|
||||
// agentID, err := agentFlag.AssertAgentID(ctx)
|
||||
// if err != nil {
|
||||
// return errors.WithStack(err)
|
||||
// }
|
||||
|
||||
// options := make([]client.UpdateAgentOptionFunc, 0)
|
||||
|
||||
// status := ctx.Int("status")
|
||||
// if status != -1 {
|
||||
// options = append(options, client.WithAgentStatus(status))
|
||||
// }
|
||||
|
||||
// label := ctx.String("label")
|
||||
// if label != "" {
|
||||
// options = append(options, client.WithAgentLabel(label))
|
||||
// }
|
||||
|
||||
// client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
// agent, err := client.UpdateAgent(ctx.Context, agentID, options...)
|
||||
// if err != nil {
|
||||
// return errors.WithStack(apierr.Wrap(err))
|
||||
// }
|
||||
|
||||
// hints := agentHints(baseFlags.OutputMode)
|
||||
|
||||
// if err := format.Write(baseFlags.Format, os.Stdout, hints, agent); err != nil {
|
||||
// return errors.WithStack(err)
|
||||
// }
|
||||
|
||||
// return nil
|
||||
// },
|
||||
// }
|
||||
// }
|
31
internal/command/client/proxy/util.go
Normal file
31
internal/command/client/proxy/util.go
Normal file
@ -0,0 +1,31 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/format"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/format/table"
|
||||
)
|
||||
|
||||
func proxyHeaderHints(outputMode format.OutputMode) format.Hints {
|
||||
return format.Hints{
|
||||
OutputMode: outputMode,
|
||||
Props: []format.Prop{
|
||||
format.NewProp("Name", "Name"),
|
||||
format.NewProp("CreatedAt", "CreatedAt", table.WithCompactModeMaxColumnWidth(20)),
|
||||
format.NewProp("UpdatedAt", "UpdatedAt", table.WithCompactModeMaxColumnWidth(20)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func proxyHints(outputMode format.OutputMode) format.Hints {
|
||||
return format.Hints{
|
||||
OutputMode: outputMode,
|
||||
Props: []format.Prop{
|
||||
format.NewProp("Name", "Name"),
|
||||
format.NewProp("From", "From"),
|
||||
format.NewProp("To", "To"),
|
||||
format.NewProp("Weight", "Weight"),
|
||||
format.NewProp("CreatedAt", "CreatedAt", table.WithCompactModeMaxColumnWidth(20)),
|
||||
format.NewProp("UpdatedAt", "UpdatedAt", table.WithCompactModeMaxColumnWidth(20)),
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user