Skip to content

Commit

Permalink
Add sshql.Dialer.Close() method
Browse files Browse the repository at this point in the history
  • Loading branch information
spiegel-im-spiegel committed Sep 11, 2022
1 parent d8f39e7 commit b27ff66
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ jobs:

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
- name: testing sshql
run: go test -shuffle on ./...
- name: testing pgdrv
run: go test -shuffle on ./pgdrv/...
- name: testing mysqldrv
run: go test -shuffle on ./mysqldrv/...
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# vendor/

# Other files and directories
go.work
# go.work
go.work.sum
*.bak
work/
Expand Down
7 changes: 7 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
go 1.19

use (
.
mysqldrv
pgdrv
)
21 changes: 21 additions & 0 deletions sshql.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sshql

import (
"errors"
"fmt"
"net"
"os"
Expand All @@ -12,6 +13,8 @@ import (
"golang.org/x/crypto/ssh/knownhosts"
)

var ErrNoConnection = errors.New("no SSH connection exists")

// Dialer is authentication provider information.
type Dialer struct {
Hostname string `json:"hostname"`
Expand All @@ -25,6 +28,13 @@ type Dialer struct {

// Connect starts a client connection to the given SSH server.
func (d *Dialer) Connect() error {
if d == nil {
return errs.Wrap(ErrNoConnection)
}
if d.client != nil {
d.Close()
d.client = nil
}
sshConfig := &ssh.ClientConfig{
User: d.Username,
Auth: []ssh.AuthMethod{},
Expand Down Expand Up @@ -68,9 +78,20 @@ func (d *Dialer) Connect() error {

// Dial makes socket connection via SSH tunnel.
func (d *Dialer) Dial(network, address string) (net.Conn, error) {
if d == nil || d.client == nil {
return nil, errs.Wrap(ErrNoConnection)
}
return d.client.Dial(network, address)
}

// Close closes SSH connection.
func (d *Dialer) Close() error {
if d == nil || d.client == nil {
return nil
}
return d.client.Close()
}

func getSigners(keyfile string, password string) ([]ssh.Signer, error) {
buf, err := os.ReadFile(keyfile)
if err != nil {
Expand Down
61 changes: 61 additions & 0 deletions sshql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package sshql

import (
"errors"
"testing"
)

func TestNil(t *testing.T) {
var d *Dialer // initialize by nil
err := d.Connect()
if err == nil || !errors.Is(err, ErrNoConnection) {
t.Errorf("<nil>.Connect() = '%v', want '%v'.", err, ErrNoConnection)
}
_, err = d.Dial("tcp", "")
if err == nil || !errors.Is(err, ErrNoConnection) {
t.Errorf("<nil>.Connect() = '%v', want '%v'.", err, ErrNoConnection)
}
err = d.Close()
if err != nil {
t.Errorf("<nil>.Close() = '%v', want <nil>.", err)
}
}

func TestEmpty(t *testing.T) {
d := &Dialer{}
err := d.Connect()
if err == nil {
t.Errorf("<nil>.Connect() = '%v', do not want <nil>.", err)
}
_, err = d.Dial("tcp", "")
if err == nil || !errors.Is(err, ErrNoConnection) {
t.Errorf("<nil>.Connect() = '%v', want '%v'.", err, ErrNoConnection)
}
err = d.Close()
if err != nil {
t.Errorf("<nil>.Close() = '%v', want <nil>.", err)
}
}

/* MIT License
*
* Copyright 2022 Spiegel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

0 comments on commit b27ff66

Please sign in to comment.