Skip to content

Commit

Permalink
CLI - add list connections (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
alishakawaguchi committed Dec 26, 2023
1 parent 5860302 commit 2b5c765
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cli/internal/cmds/neosync/connections/connections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package connections_cmd

import (
"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "connections",
Short: "Parent command for connections",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

cmd.AddCommand(newListCmd())
return cmd
}
127 changes: 127 additions & 0 deletions cli/internal/cmds/neosync/connections/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package connections_cmd

import (
"context"
"errors"
"fmt"
"net/http"
"time"

"connectrpc.com/connect"
"github.com/fatih/color"
mgmtv1alpha1 "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1"
"github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1/mgmtv1alpha1connect"
"github.com/nucleuscloud/neosync/cli/internal/auth"
auth_interceptor "github.com/nucleuscloud/neosync/cli/internal/connect/interceptors/auth"
"github.com/nucleuscloud/neosync/cli/internal/serverconfig"
"github.com/nucleuscloud/neosync/cli/internal/userconfig"
"github.com/rodaine/table"
"github.com/spf13/cobra"
)

func newListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "list connections",
RunE: func(cmd *cobra.Command, args []string) error {
apiKey, err := cmd.Flags().GetString("api-key")
if err != nil {
return err
}

accountId, err := cmd.Flags().GetString("account-id")
if err != nil {
return err
}
cmd.SilenceUsage = true
return listConnections(cmd.Context(), &apiKey, &accountId)
},
}
cmd.Flags().String("account-id", "", "Account to list connections for. Defaults to account id in cli context")
return cmd
}

func listConnections(
ctx context.Context,
apiKey, accountIdFlag *string,
) error {
isAuthEnabled, err := auth.IsAuthEnabled(ctx)
if err != nil {
return err
}

var accountId = accountIdFlag
if accountId == nil || *accountId == "" {
aId, err := userconfig.GetAccountId()
if err != nil {
fmt.Println("Unable to retrieve account id. Please use account switch command to set account.") // nolint
return err
}
accountId = &aId
}

if accountId == nil || *accountId == "" {
return errors.New("Account Id not found. Please use account switch command to set account.")
}

connectionclient := mgmtv1alpha1connect.NewConnectionServiceClient(
http.DefaultClient,
serverconfig.GetApiBaseUrl(),
connect.WithInterceptors(
auth_interceptor.NewInterceptor(isAuthEnabled, auth.AuthHeader, auth.GetAuthHeaderTokenFn(apiKey)),
),
)
res, err := connectionclient.GetConnections(ctx, connect.NewRequest[mgmtv1alpha1.GetConnectionsRequest](&mgmtv1alpha1.GetConnectionsRequest{
AccountId: *accountId,
}))
if err != nil {
return err
}

fmt.Println() // nolint
printConnectionsTable(res.Msg.Connections)
fmt.Println() // nolint
return nil
}

func printConnectionsTable(
connections []*mgmtv1alpha1.Connection,
) {
tbl := table.
New("Id", "Name", "Category", "Created At", "Updated At").
WithHeaderFormatter(
color.New(color.FgGreen, color.Underline).SprintfFunc(),
).
WithFirstColumnFormatter(
color.New(color.FgYellow).SprintfFunc(),
)

for idx := range connections {
connection := connections[idx]
tbl.AddRow(
connection.Id,
connection.Name,
getCategory(connection.GetConnectionConfig()),
connection.CreatedAt.AsTime().Local().Format(time.RFC3339),
connection.UpdatedAt.AsTime().Local().Format(time.RFC3339),
)
}
tbl.Print()
}

func getCategory(cc *mgmtv1alpha1.ConnectionConfig) string {
if cc == nil {
return "Unknown"
}
switch cc.Config.(type) {
case *mgmtv1alpha1.ConnectionConfig_PgConfig:
return "Postgres"
case *mgmtv1alpha1.ConnectionConfig_MysqlConfig:
return "Mysql"
case *mgmtv1alpha1.ConnectionConfig_AwsS3Config:
return "AWS S3"
default:
return "Unknown"
}
}
2 changes: 2 additions & 0 deletions cli/internal/cmds/neosync/neosync.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

accounts_cmd "github.com/nucleuscloud/neosync/cli/internal/cmds/neosync/accounts"
connections_cmd "github.com/nucleuscloud/neosync/cli/internal/cmds/neosync/connections"
jobs_cmd "github.com/nucleuscloud/neosync/cli/internal/cmds/neosync/jobs"
login_cmd "github.com/nucleuscloud/neosync/cli/internal/cmds/neosync/login"
sync_cmd "github.com/nucleuscloud/neosync/cli/internal/cmds/neosync/sync"
Expand Down Expand Up @@ -77,6 +78,7 @@ func Execute() {
rootCmd.AddCommand(login_cmd.NewCmd())
rootCmd.AddCommand(sync_cmd.NewCmd())
rootCmd.AddCommand(accounts_cmd.NewCmd())
rootCmd.AddCommand(connections_cmd.NewCmd())

cobra.CheckErr(rootCmd.Execute())
}
Expand Down

1 comment on commit 2b5c765

@vercel
Copy link

@vercel vercel bot commented on 2b5c765 Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.