-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for dgate cli and dgclient
- Loading branch information
Showing
21 changed files
with
783 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
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"), | ||
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) | ||
}, | ||
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) | ||
} | ||
} |
Oops, something went wrong.