Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 74 additions & 18 deletions cmd/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/fatih/color"
"github.com/rodaine/table"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/bcgov/gwa-cli/pkg"
)

func NewGatewayCmd(ctx *pkg.AppContext) *cobra.Command {
func NewGatewayCmd(ctx *pkg.AppContext, buf *bytes.Buffer) *cobra.Command {
gatewayCmd := &cobra.Command{
Use: "gateway",
Short: "Manage your gateways",
Long: `Gateways are used to organize your services.`,
}
gatewayCmd.AddCommand(GatewayListCmd(ctx))
gatewayCmd.AddCommand(GatewayListCmd(ctx, buf))
gatewayCmd.AddCommand(GatewayCreateCmd(ctx))
gatewayCmd.AddCommand(GatewayDestroyCmd(ctx))
gatewayCmd.AddCommand(GatewayCurrentCmd(ctx))
gatewayCmd.AddCommand(GatewayCurrentCmd(ctx, buf))
return gatewayCmd
}

Expand All @@ -38,14 +40,14 @@ func (n *GatewayFormData) IsEmpty() bool {
return n.DisplayName == "" && n.GatewayId == ""
}

func GatewayListCmd(ctx *pkg.AppContext) *cobra.Command {
func GatewayListCmd(ctx *pkg.AppContext, buf *bytes.Buffer) *cobra.Command {
listCommand := &cobra.Command{
Use: "list",
Short: "List all your managed gateways",
RunE: pkg.WrapError(ctx, func(_ *cobra.Command, _ []string) error {
path := fmt.Sprintf("/ds/api/%s/gateways", ctx.ApiVersion)
URL, _ := ctx.CreateUrl(path, nil)
r, err := pkg.NewApiGet[[]string](ctx, URL)
r, err := pkg.NewApiGet[[]GatewayFormData](ctx, URL)
if err != nil {
return err
}
Expand All @@ -54,15 +56,13 @@ func GatewayListCmd(ctx *pkg.AppContext) *cobra.Command {
response, err := r.Do()
if err != nil {
loader.Stop()
if response.StatusCode == http.StatusUnauthorized {
fmt.Println()
fmt.Println(
heredoc.Doc(`
Next Steps:
Run gwa login to obtain another auth token
`),
)
}
fmt.Println()
fmt.Println(
heredoc.Doc(`
Next Steps:
Run gwa login to obtain another auth token
`),
)
return err
}
loader.Stop()
Expand All @@ -71,8 +71,22 @@ func GatewayListCmd(ctx *pkg.AppContext) *cobra.Command {
fmt.Println("You have no gateways")
}

for _, n := range response.Data {
fmt.Println(n)
if len(response.Data) > 0 {
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("Display Name", "Gateway ID")

if buf != nil {
tbl.WithWriter(buf)
}

tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

for _, n := range response.Data {
tbl.AddRow(n.DisplayName, n.GatewayId)
}

tbl.Print()
}

return nil
Expand Down Expand Up @@ -218,7 +232,7 @@ func createGateway(ctx *pkg.AppContext, data *GatewayFormData) (string, error) {
return response.Data.GatewayId, nil
}

func GatewayCurrentCmd(ctx *pkg.AppContext) *cobra.Command {
func GatewayCurrentCmd(ctx *pkg.AppContext, buf *bytes.Buffer) *cobra.Command {
currentCmd := &cobra.Command{
Use: "current",
Short: "Display the current gateway",
Expand All @@ -231,7 +245,49 @@ You can create a gateway by running:
return fmt.Errorf("no gateway has been defined")
}

fmt.Println(ctx.Gateway)
path := fmt.Sprintf("/ds/api/%s/gateways/%s", ctx.ApiVersion, ctx.Gateway)
URL, _ := ctx.CreateUrl(path, nil)

r, err := pkg.NewApiGet[GatewayFormData](ctx, URL)
if err != nil {
return err
}
loader := pkg.NewSpinner()
loader.Start()
response, err := r.Do()
if err != nil {
loader.Stop()
if response.StatusCode == http.StatusUnauthorized {
fmt.Println()
fmt.Printf(`Next Steps:
1. Run gwa gateway list
2. Check if %s is in the list
3. If not, run gwa config set gateway <Gateway ID> with a valid Gateway ID
`, ctx.Gateway)
fmt.Println()
} else {
fmt.Println()
fmt.Println(
heredoc.Doc(`
Next Steps:
Run gwa login to obtain another auth token
`),
)
}
return err
}
loader.Stop()

headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("Display Name", "Gateway ID")
if buf != nil {
tbl.WithWriter(buf)
}
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
tbl.AddRow(response.Data.DisplayName, ctx.Gateway)
tbl.Print()

return nil
},
}
Expand Down
51 changes: 38 additions & 13 deletions cmd/gateway_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -41,13 +42,20 @@ func TestGatewayCommands(t *testing.T) {
{
name: "list gateways",
args: []string{"list"},
expect: `ns-123
ns-456`,
expect: `Display Name Gateway ID
janis's Gateway gw-1
janis's Gateway gw-2 `,
method: "GET",
response: func(r *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, []string{
"ns-123",
"ns-456",
return httpmock.NewJsonResponse(200, []GatewayFormData{
{
GatewayId: "gw-1",
DisplayName: "janis's Gateway",
},
{
GatewayId: "gw-2",
DisplayName: "janis's Gateway",
},
})
},
},
Expand Down Expand Up @@ -137,37 +145,54 @@ ns-456`,
{
name: "show current gateway",
args: []string{"current"},
expect: "ns-sampler",
expect: `Display Name Gateway ID
janis's Gateway ns-sampler
`,
method: "GET",
gateway: "/ns-sampler",
response: func(r *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, map[string]interface{}{
"displayName": "janis's Gateway",
})
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
dir := t.TempDir()
setup(dir)
if tt.response != nil {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
URL := fmt.Sprintf("https://api.gov.ca/ds/api/v2/gateways%s", tt.gateway)
URL := fmt.Sprintf("https://api.gov.ca/ds/api/v3/gateways%s", tt.gateway)
httpmock.RegisterResponder(tt.method, URL, tt.response)
}
ctx := &pkg.AppContext{
ApiHost: "api.gov.ca",
ApiVersion: "v2",
ApiVersion: "v3",
Gateway: "ns-sampler",
}
args := append([]string{"gateway"}, tt.args...)
mainCmd := &cobra.Command{
Use: "gwa",
SilenceUsage: true,
}
mainCmd.AddCommand(NewGatewayCmd(ctx))
mainCmd.AddCommand(NewGatewayCmd(ctx, buf))
mainCmd.SetArgs(args)
out := capturer.CaptureOutput(func() {
mainCmd.Execute()
})

assert.Contains(t, out, tt.expect, "Expect: %v\nActual: %v\n", tt.expect, out)
// Use buffer to capture table output
if (tt.name == "list gateways" || tt.name == "show current gateway") {
mainCmd.Execute()
out := buf.String()
assert.Contains(t, out, tt.expect, "Expect: %v\nActual: %v\n", tt.expect, out)
} else {
out := capturer.CaptureOutput(func() {
mainCmd.Execute()
})
assert.Contains(t, out, tt.expect, "Expect: %v\nActual: %v\n", tt.expect, out)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/generateConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (o *GenerateConfigOptions) ValidateTemplate() error {
}

func (o *GenerateConfigOptions) ValidateService(ctx *pkg.AppContext, service string) error {
path := fmt.Sprintf("/ds/api/v3/routes/availability?gatewayId=%s&serviceName=%s", ctx.Gateway, service)
path := fmt.Sprintf("/ds/api/%s/routes/availability?gatewayId=%s&serviceName=%s", ctx.ApiVersion, ctx.Gateway, service)
URL, _ := ctx.CreateUrl(path, nil)
decodedURL, err := url.QueryUnescape(URL)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func NewRootCommand(ctx *pkg.AppContext) *cobra.Command {
rootCmd.AddCommand(NewApplyCmd(ctx))
rootCmd.AddCommand(NewGenerateConfigCmd(ctx))
rootCmd.AddCommand(NewLoginCmd(ctx))
rootCmd.AddCommand(NewGatewayCmd(ctx))
rootCmd.AddCommand(NewGatewayCmd(ctx, nil))
rootCmd.AddCommand(NewStatusCmd(ctx, nil))
// Disable these for now since they don't do anything
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gwa-confg.yaml)")
// rootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "only print results, ideal for CI/CD")
// rootCmd.PersistentFlags().StringVar(&ctx.ApiVersion, "api-version", ctx.ApiVersion, "Set the global API version")
rootCmd.PersistentFlags().BoolVarP(&ctx.Debug, "debug", "D", false, "Print debug information to stdout when the command has exited")
rootCmd.PersistentFlags().StringVar(&ctx.ApiVersion, "api-version", ctx.ApiVersion, "Set the global API version")
rootCmd.PersistentFlags().StringVar(&ctx.ApiHost, "host", ctx.ApiHost, "Set the default host to use for the API")
rootCmd.PersistentFlags().StringVar(&ctx.Scheme, "scheme", "", "Use to override default https")
rootCmd.PersistentFlags().StringVar(&ctx.Gateway, "gateway", "", "Assign the gateway you would like to use")
Expand Down