Skip to content

Commit

Permalink
Merge pull request #94 from ricardoseriani/avoid-ssh-file-not-found-e…
Browse files Browse the repository at this point in the history
…rror

Avoid ssh config file not found error
  • Loading branch information
davrodpin committed Oct 3, 2019
2 parents fbb118b + 74167bf commit 6b3f8cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ For more information about installation, usage, examples and specific use cases,

### Build and Install from Source

* [Go 1.12.5+](https://golang.org/dl/) is required
* [Go 1.13+](https://golang.org/dl/) is required
* Mole uses [Go Modules](https://blog.golang.org/using-go-modules) to manage its dependencies, so remember to clone the project outside `GOPATH` and unset it.

```sh
Expand Down
5 changes: 5 additions & 0 deletions tunnel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func NewSSHConfigFile() (*SSHConfigFile, error) {
return &SSHConfigFile{sshConfig: cfg}, nil
}

func NewEmptySSHConfigStruct() *SSHConfigFile {
log.Debugf("generating an empty config struct")
return &SSHConfigFile{sshConfig: &ssh_config.Config{}}
}

// Get consults a ssh config file to extract some ssh server attributes
// from it, returning a SSHHost. Any attribute which its value is an empty
// string is an attribute that could not be found in the ssh config file.
Expand Down
11 changes: 10 additions & 1 deletion tunnel/tunnel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tunnel

import (
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -49,7 +50,15 @@ func NewServer(user, address, key string) (*Server, error) {

c, err := NewSSHConfigFile()
if err != nil {
return nil, fmt.Errorf("error accessing %s: %v", host, err)
// Ignore file doesnt exists
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("error accessing %s: %v", host, err)
}
}

// // If ssh config file doesnt exists, create an empty ssh config struct to avoid nil pointer deference
if errors.Is(err, os.ErrNotExist) {
c = NewEmptySSHConfigStruct()
}

h := c.Get(host)
Expand Down

0 comments on commit 6b3f8cb

Please sign in to comment.