Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ssh tunnel connection handling and closing #249

Merged
merged 1 commit into from
Jan 7, 2025
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
22 changes: 13 additions & 9 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (

// App Struct.
type App struct {
t *tui.Tui
c *client.Client
t *tui.Tui
c *client.Client
sc *sshdb.SSHConfig
}

// New bootstrap a new application.
func New(opts command.Options) (*App, error) {
var sshConfig *sshdb.SSHConfig
var sc *sshdb.SSHConfig

if opts.SSHHost != "" {
sshConfig = sshdb.New(
sc = sshdb.New(
sshdb.WithDBDriver(opts.Driver),
sshdb.WithSShHost(opts.SSHHost),
sshdb.WithSShPort(opts.SSHPort),
Expand All @@ -29,11 +30,9 @@ func New(opts command.Options) (*App, error) {
sshdb.WithDBDURL(opts.URL),
)

if err := sshConfig.SSHTunnel(); err != nil {
if err := sc.SSHTunnel(); err != nil {
return nil, err
}

defer sshConfig.Close()
}

c, err := client.New(opts)
Expand All @@ -47,8 +46,9 @@ func New(opts command.Options) (*App, error) {
}

app := App{
t: t,
c: c,
c: c,
t: t,
sc: sc,
}

return &app, nil
Expand All @@ -57,6 +57,10 @@ func New(opts command.Options) (*App, error) {
// Run runs the application.
func (a *App) Run() error {
defer func() {
if a.sc != nil {
_ = a.sc.Close()
}

_ = a.c.DB().Close()
}()

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (c *Client) ShowTablesPerDB(database string) ([]string, error) {
}

switch c.driver {
case drivers.PostgreSQL, drivers.Postgres, drivers.MySQL:
case drivers.PostgreSQL, drivers.Postgres, drivers.PostgresSSH, drivers.MySQL:
db, ok = c.dbs[database]
if !ok {
return nil, fmt.Errorf("connection with %s database not found", database)
Expand Down
Loading