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:
72
internal/command/client/proxy/layer/create.go
Normal file
72
internal/command/client/proxy/layer/create.go
Normal file
@ -0,0 +1,72 @@
|
||||
package layer
|
||||
|
||||
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/command/client/proxy/layer/flag"
|
||||
layerFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/layer/flag"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/format"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func CreateCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "create",
|
||||
Usage: "Create layer",
|
||||
Flags: layerFlag.WithLayerCreateFlags(),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
baseFlags := clientFlag.GetBaseFlags(ctx)
|
||||
|
||||
token, err := clientFlag.GetToken(baseFlags)
|
||||
if err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
layerName, err := flag.AssertLayerName(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
proxyName, err := proxyFlag.AssertProxyName(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
layerType, err := flag.AssertLayerType(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
layerOptions, err := flag.AssertLayerOptions(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
layer, err := client.CreateLayer(
|
||||
ctx.Context,
|
||||
proxyName,
|
||||
layerName,
|
||||
layerType,
|
||||
layerOptions,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
hints := layerHints(baseFlags.OutputMode)
|
||||
|
||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, layer); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
62
internal/command/client/proxy/layer/delete.go
Normal file
62
internal/command/client/proxy/layer/delete.go
Normal file
@ -0,0 +1,62 @@
|
||||
package layer
|
||||
|
||||
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"
|
||||
layerFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/layer/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 layer",
|
||||
Flags: layerFlag.WithLayerFlags(),
|
||||
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)
|
||||
}
|
||||
|
||||
layerName, err := layerFlag.AssertLayerName(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
layerName, err = client.DeleteLayer(ctx.Context, proxyName, layerName)
|
||||
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.LayerName `json:"id"`
|
||||
}{
|
||||
Name: layerName,
|
||||
}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
76
internal/command/client/proxy/layer/flag/flag.go
Normal file
76
internal/command/client/proxy/layer/flag/flag.go
Normal file
@ -0,0 +1,76 @@
|
||||
package flag
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/flag"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
FlagLayerName = "layer-name"
|
||||
FlagLayerType = "layer-type"
|
||||
FlagLayerOptions = "layer-options"
|
||||
)
|
||||
|
||||
func WithLayerFlags(flags ...cli.Flag) []cli.Flag {
|
||||
baseFlags := proxyFlag.WithProxyFlags(
|
||||
&cli.StringFlag{
|
||||
Name: FlagLayerName,
|
||||
Usage: "use `LAYER_NAME` as targeted layer",
|
||||
Value: "",
|
||||
Required: true,
|
||||
},
|
||||
)
|
||||
|
||||
flags = append(flags, baseFlags...)
|
||||
|
||||
return flags
|
||||
}
|
||||
|
||||
func WithLayerCreateFlags(flags ...cli.Flag) []cli.Flag {
|
||||
return WithLayerFlags(
|
||||
&cli.StringFlag{
|
||||
Name: FlagLayerType,
|
||||
Usage: "Set `LAYER_TYPE` as layer's type",
|
||||
Value: "",
|
||||
Required: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: FlagLayerOptions,
|
||||
Usage: "Set `LAYER_OPTIONS` as layer's options",
|
||||
Value: "{}",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func AssertLayerName(ctx *cli.Context) (store.LayerName, error) {
|
||||
rawLayerName := ctx.String(FlagLayerName)
|
||||
|
||||
name, err := store.ValidateName(rawLayerName)
|
||||
if err != nil {
|
||||
return "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
return store.LayerName(name), nil
|
||||
}
|
||||
|
||||
func AssertLayerType(ctx *cli.Context) (store.LayerType, error) {
|
||||
rawLayerType := ctx.String(FlagLayerType)
|
||||
|
||||
return store.LayerType(rawLayerType), nil
|
||||
}
|
||||
|
||||
func AssertLayerOptions(ctx *cli.Context) (store.LayerOptions, error) {
|
||||
rawLayerOptions := ctx.String(FlagLayerOptions)
|
||||
|
||||
layerOptions := store.LayerOptions{}
|
||||
|
||||
if err := json.Unmarshal([]byte(rawLayerOptions), &layerOptions); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return layerOptions, nil
|
||||
}
|
55
internal/command/client/proxy/layer/get.go
Normal file
55
internal/command/client/proxy/layer/get.go
Normal file
@ -0,0 +1,55 @@
|
||||
package layer
|
||||
|
||||
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"
|
||||
layerFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/proxy/layer/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 layer",
|
||||
Flags: layerFlag.WithLayerFlags(),
|
||||
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)
|
||||
}
|
||||
|
||||
layerName, err := layerFlag.AssertLayerName(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
layer, err := client.GetLayer(ctx.Context, proxyName, layerName)
|
||||
if err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
hints := layerHints(baseFlags.OutputMode)
|
||||
|
||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, layer); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
69
internal/command/client/proxy/layer/query.go
Normal file
69
internal/command/client/proxy/layer/query.go
Normal file
@ -0,0 +1,69 @@
|
||||
package layer
|
||||
|
||||
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 QueryCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "query",
|
||||
Usage: "Query layers",
|
||||
Flags: proxyFlag.WithProxyFlags(
|
||||
&cli.StringSliceFlag{
|
||||
Name: "name",
|
||||
Usage: "use `NAME` 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))
|
||||
}
|
||||
|
||||
proxyName, err := proxyFlag.AssertProxyName(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
options := make([]client.QueryLayerOptionFunc, 0)
|
||||
|
||||
rawNames := ctx.StringSlice("name")
|
||||
if rawNames != nil {
|
||||
layerNames := func(names []string) []store.LayerName {
|
||||
layerNames := make([]store.LayerName, len(names))
|
||||
for i, name := range names {
|
||||
layerNames[i] = store.LayerName(name)
|
||||
}
|
||||
return layerNames
|
||||
}(rawNames)
|
||||
options = append(options, client.WithQueryLayerNames(layerNames...))
|
||||
}
|
||||
|
||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
proxies, err := client.QueryLayer(ctx.Context, proxyName, options...)
|
||||
if err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
hints := layerHeaderHints(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/layer/root.go
Normal file
18
internal/command/client/proxy/layer/root.go
Normal file
@ -0,0 +1,18 @@
|
||||
package layer
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Root() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "layer",
|
||||
Usage: "Execute actions related to layers",
|
||||
Subcommands: []*cli.Command{
|
||||
GetCommand(),
|
||||
CreateCommand(),
|
||||
QueryCommand(),
|
||||
DeleteCommand(),
|
||||
},
|
||||
}
|
||||
}
|
72
internal/command/client/proxy/layer/update.go
Normal file
72
internal/command/client/proxy/layer/update.go
Normal file
@ -0,0 +1,72 @@
|
||||
package layer
|
||||
|
||||
// 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
|
||||
// },
|
||||
// }
|
||||
// }
|
33
internal/command/client/proxy/layer/util.go
Normal file
33
internal/command/client/proxy/layer/util.go
Normal file
@ -0,0 +1,33 @@
|
||||
package layer
|
||||
|
||||
import (
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/format"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/format/table"
|
||||
)
|
||||
|
||||
func layerHeaderHints(outputMode format.OutputMode) format.Hints {
|
||||
return format.Hints{
|
||||
OutputMode: outputMode,
|
||||
Props: []format.Prop{
|
||||
format.NewProp("Name", "Name"),
|
||||
format.NewProp("Type", "Type"),
|
||||
format.NewProp("Enabled", "Enabled"),
|
||||
format.NewProp("Weight", "Weight"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func layerHints(outputMode format.OutputMode) format.Hints {
|
||||
return format.Hints{
|
||||
OutputMode: outputMode,
|
||||
Props: []format.Prop{
|
||||
format.NewProp("Name", "Name"),
|
||||
format.NewProp("Type", "Type"),
|
||||
format.NewProp("Enabled", "Enabled"),
|
||||
format.NewProp("Weight", "Weight"),
|
||||
format.NewProp("Options", "Options"),
|
||||
format.NewProp("CreatedAt", "CreatedAt", table.WithCompactModeMaxColumnWidth(20)),
|
||||
format.NewProp("UpdatedAt", "UpdatedAt", table.WithCompactModeMaxColumnWidth(20)),
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user