Skip to content

Commit

Permalink
add tests for dgate cli and dgclient
Browse files Browse the repository at this point in the history
  • Loading branch information
bubbajoe committed May 11, 2024
1 parent 109609b commit 57a9c71
Show file tree
Hide file tree
Showing 21 changed files with 783 additions and 202 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
113 changes: 113 additions & 0 deletions cmd/dgate-cli/commands/run_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package commands

import (
"fmt"
"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) {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
bv := buildInfo.Main.Version
if bv != "" && bv != "(devel)" {
version = buildInfo.Main.Version

Check warning on line 19 in cmd/dgate-cli/commands/run_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-cli/commands/run_cmd.go#L19

Added line #L19 was not covered by tests
}
}

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]

Check warning on line 62 in cmd/dgate-cli/commands/run_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-cli/commands/run_cmd.go#L58-L62

Added lines #L58 - L62 were not covered by tests
}
authOption = dgclient.WithBasicAuth(
username, password,
)

Check warning on line 66 in cmd/dgate-cli/commands/run_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-cli/commands/run_cmd.go#L64-L66

Added lines #L64 - L66 were not covered by tests
} else if auth != "" {
fmt.Printf("password for %s:", auth)
password, err := term.ReadPassword(0)
if err != nil {
return err

Check warning on line 71 in cmd/dgate-cli/commands/run_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-cli/commands/run_cmd.go#L68-L71

Added lines #L68 - L71 were not covered by tests
}
fmt.Print("\n")
authOption = dgclient.WithBasicAuth(
auth, string(password),
)

Check warning on line 76 in cmd/dgate-cli/commands/run_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-cli/commands/run_cmd.go#L73-L76

Added lines #L73 - L76 were not covered by tests
}
return client.Init(
ctx.String("admin"),
authOption,
dgclient.WithFollowRedirect(
ctx.Bool("follow"),
),
dgclient.WithUserAgent(
"DGate CLI "+version+
";os="+runtime.GOOS+
";arch="+runtime.GOARCH,
),
dgclient.WithVerboseLogging(
ctx.Bool("verbose"),
),
)
},
Action: func(ctx *cli.Context) error {
return ctx.App.Command("help").Run(ctx)
},

Check warning on line 96 in cmd/dgate-cli/commands/run_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-cli/commands/run_cmd.go#L94-L96

Added lines #L94 - L96 were not covered by tests
Commands: []*cli.Command{
NamespaceCommand(client),
ServiceCommand(client),
ModuleCommand(client),
RouteCommand(client),
DomainCommand(client),
CollectionCommand(client),
DocumentCommand(client),
SecretCommand(client),
},
}

if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)

Check warning on line 111 in cmd/dgate-cli/commands/run_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-cli/commands/run_cmd.go#L110-L111

Added lines #L110 - L111 were not covered by tests
}
}
Loading

0 comments on commit 57a9c71

Please sign in to comment.