Skip to content

Commit

Permalink
fix: Test Improvements (#7)
Browse files Browse the repository at this point in the history
* fix: test improvements + add tests for dgate cli and dgclient

* refactor dgate cli run function, add more test to increase coverage for dgate-cli

* simplify code and add more test for better coverage

* fix nil ref and remove WithHttpClient hook for dgclient

* small refactor on dgclient for better support for changing the client

* fix redirect bug
  • Loading branch information
bubbajoe authored May 12, 2024
1 parent 60da400 commit 5834288
Show file tree
Hide file tree
Showing 46 changed files with 1,973 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/urfave/cli/v2"
)

func CollectionCommand(client *dgclient.DGateClient) *cli.Command {
func CollectionCommand(client dgclient.DGateClient) *cli.Command {
return &cli.Command{
Name: "collection",
Aliases: []string{"col"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/urfave/cli/v2"
)

func DocumentCommand(client *dgclient.DGateClient) *cli.Command {
func DocumentCommand(client dgclient.DGateClient) *cli.Command {
return &cli.Command{
Name: "document",
Aliases: []string{"doc"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/urfave/cli/v2"
)

func DomainCommand(client *dgclient.DGateClient) *cli.Command {
func DomainCommand(client dgclient.DGateClient) *cli.Command {
return &cli.Command{
Name: "domain",
Aliases: []string{"dom"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/urfave/cli/v2"
)

func ModuleCommand(client *dgclient.DGateClient) *cli.Command {
func ModuleCommand(client dgclient.DGateClient) *cli.Command {
return &cli.Command{
Name: "module",
Aliases: []string{"mod"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/urfave/cli/v2"
)

func NamespaceCommand(client *dgclient.DGateClient) *cli.Command {
func NamespaceCommand(client dgclient.DGateClient) *cli.Command {
return &cli.Command{
Name: "namespace",
Aliases: []string{"ns"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/urfave/cli/v2"
)

func RouteCommand(client *dgclient.DGateClient) *cli.Command {
func RouteCommand(client dgclient.DGateClient) *cli.Command {
return &cli.Command{
Name: "route",
Aliases: []string{"rt"},
Expand Down
111 changes: 111 additions & 0 deletions cmd/dgate-cli/commands/run_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package commands

import (
"fmt"
"net/http"
"os"
"runtime"
"runtime/debug"
"strings"

"github.com/dgate-io/dgate/pkg/dgclient"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)

func Run(client dgclient.DGateClient, version string) error {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
bv := buildInfo.Main.Version
if bv != "" && bv != "(devel)" {
version = buildInfo.Main.Version
}
}

app := &cli.App{
Name: "dgate-cli",
Usage: "a command line interface for DGate (API Gateway) Admin API",
Version: version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "admin",
Value: "http://localhost:9080",
EnvVars: []string{"DGATE_ADMIN_API"},
Usage: "the url for the file client",
},
// only basic auth support for now
&cli.StringFlag{
Name: "auth",
Aliases: []string{"a"},
EnvVars: []string{"DGATE_ADMIN_AUTH"},
Usage: "basic auth username:password; or just username for password prompt",
},
&cli.BoolFlag{
Name: "follow",
DefaultText: "false",
Aliases: []string{"f"},
EnvVars: []string{"DGATE_FOLLOW_REDIRECTS"},
Usage: "follows redirects, useful for raft leader changes",
},
&cli.BoolFlag{
Name: "verbose",
DefaultText: "false",
Aliases: []string{"V"},
Usage: "enable verbose logging",
},
},
Before: func(ctx *cli.Context) (err error) {
var authOption dgclient.Options = func(dc dgclient.DGateClient) {}
if auth := ctx.String("auth"); strings.Contains(auth, ":") {
pair := strings.SplitN(ctx.String("auth"), ":", 2)
username := pair[0]
password := ""
if len(pair) > 1 {
password = pair[1]
}
authOption = dgclient.WithBasicAuth(
username, password,
)
} else if auth != "" {
fmt.Printf("password for %s:", auth)
password, err := term.ReadPassword(0)
if err != nil {
return err
}
fmt.Print("\n")
authOption = dgclient.WithBasicAuth(
auth, string(password),
)
}
return client.Init(ctx.String("admin"),
http.DefaultClient,
dgclient.WithFollowRedirect(
ctx.Bool("follow"),
),
dgclient.WithUserAgent(
"DGate CLI "+version+
";os="+runtime.GOOS+
";arch="+runtime.GOARCH,
),
dgclient.WithVerboseLogging(
ctx.Bool("verbose"),
),
authOption,
)
},
Action: func(ctx *cli.Context) error {
return ctx.App.Command("help").Run(ctx)
},
Commands: []*cli.Command{
NamespaceCommand(client),
ServiceCommand(client),
ModuleCommand(client),
RouteCommand(client),
DomainCommand(client),
CollectionCommand(client),
DocumentCommand(client),
SecretCommand(client),
},
}

return app.Run(os.Args)
}
Loading

0 comments on commit 5834288

Please sign in to comment.