Skip to content

Commit

Permalink
Merge 'dshemin-remove-unnecessary-panic'
Browse files Browse the repository at this point in the history
  • Loading branch information
melbahja committed Nov 10, 2020
2 parents 3176130 + eff6058 commit 0a8e7c4
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 43 deletions.
42 changes: 28 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<div align="center">
<h1>Golang SSH Client.</h1>
<a href="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/melbahja/goph">
<img src="https://github.com/melbahja/goph/raw/master/.github/goph.png" width="200">
</a>
<h4 align="center">
Fast and easy golang ssh client module.
<a href="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/melbahja/goph">
<img src="https://github.com/melbahja/goph/raw/master/.github/goph.png" width="200">
</a>
<h4 align="center">
Fast and easy golang ssh client module.
</h4>
<p>Goph is a lightweight Go SSH client focusing on simplicity!</p>
<p>Goph is a lightweight Go SSH client focusing on simplicity!</p>
</div>

<p align="center">
<a href="#installation">Installation</a> ❘
<a href="#features">Features</a> ❘
<a href="#usage">Usage</a> ❘
<a href="#examples">Examples</a> ❘
<a href="#license">License</a>
<a href="#installation">Installation</a> ❘
<a href="#features">Features</a> ❘
<a href="#usage">Usage</a> ❘
<a href="#examples">Examples</a> ❘
<a href="#license">License</a>
</p>


Expand Down Expand Up @@ -54,8 +54,12 @@ import (
func main() {

// Start new ssh connection with private key.
client, err := goph.New("root", "192.1.1.3", goph.Key("/home/mohamed/.ssh/id_rsa", ""))
auth, err := goph.Key("/home/mohamed/.ssh/id_rsa", "")
if err != nil {
log.Fatal(err)
}

client, err := goph.New("root", "192.1.1.3", auth)
if err != nil {
log.Fatal(err)
}
Expand All @@ -77,7 +81,12 @@ func main() {

#### 🔐 Start Connection With Protected Private Key:
```go
client, err := goph.New("root", "192.1.1.3", goph.Key("/home/mohamed/.ssh/id_rsa", "you_passphrase_here"))
auth, err := goph.Key("/home/mohamed/.ssh/id_rsa", "you_passphrase_here")
if err != nil {
// handle error
}

client, err := goph.New("root", "192.1.1.3", auth)
```

#### 🔑 Start Connection With Password:
Expand All @@ -87,7 +96,12 @@ client, err := goph.New("root", "192.1.1.3", goph.Password("you_password_here"))

#### ☛ Start Connection With SSH Agent (Unix systems only):
```go
client, err := goph.New("root", "192.1.1.3", goph.UseAgent())
auth, err := goph.UseAgent()
if err != nil {
// handle error
}

client, err := goph.New("root", "192.1.1.3", auth)
```

#### ⤴️ Upload Local File to Remote:
Expand Down
12 changes: 6 additions & 6 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ func Password(pass string) Auth {
}

// Key returns auth method from private key with or without passphrase.
func Key(prvFile string, passphrase string) Auth {
func Key(prvFile string, passphrase string) (Auth, error) {

signer, err := GetSigner(prvFile, passphrase)

if err != nil {
panic(err)
return nil, err
}

return Auth{
ssh.PublicKeys(signer),
}
}, nil
}

// HasAgent checks if ssh agent exists.
Expand All @@ -43,14 +43,14 @@ func HasAgent() bool {
}

// UseAgent auth via ssh agent, (Unix systems only)
func UseAgent() Auth {
func UseAgent() (Auth, error) {
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
panic(fmt.Errorf("could not find ssh agent: %w", err))
return nil, fmt.Errorf("could not find ssh agent: %w", err)
}
return Auth{
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
}
}, nil
}

// GetSigner returns ssh signer from private key file.
Expand Down
23 changes: 13 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,21 @@ func (c Client) Close() error {
func (c Client) Upload(localPath string, remotePath string) (err error) {

local, err := os.Open(localPath)

if err != nil {
return
}

defer local.Close()

remote, err := c.ftp().Create(remotePath)

ftp, err := c.ftp()
if err != nil {
return
}

remote, err := ftp.Create(remotePath)
if err != nil {
return
}
defer remote.Close()

_, err = io.Copy(remote, local)
Expand All @@ -168,19 +170,20 @@ func (c Client) Upload(localPath string, remotePath string) (err error) {
func (c Client) Download(remotePath string, localPath string) (err error) {

local, err := os.Create(localPath)

if err != nil {
return
}

defer local.Close()

remote, err := c.ftp().Open(remotePath)

ftp, err := c.ftp()
if err != nil {
return
}

remote, err := ftp.Open(remotePath)
if err != nil {
return
}
defer remote.Close()

if _, err = io.Copy(local, remote); err != nil {
Expand All @@ -191,15 +194,15 @@ func (c Client) Download(remotePath string, localPath string) (err error) {
}

// get sftp client if not set.
func (c *Client) ftp() *sftp.Client {
func (c *Client) ftp() (*sftp.Client, error) {

if c.sftp == nil {
sftp, err := c.NewSftp()
if err != nil {
panic(err)
return nil, err
}
c.sftp = sftp
}

return c.sftp
return c.sftp, nil
}
29 changes: 22 additions & 7 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"strings"

"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
)

Expand All @@ -28,22 +29,34 @@ type Cmd struct {

// CombinedOutput runs cmd on the remote host and returns its combined stdout and stderr.
func (c *Cmd) CombinedOutput() ([]byte, error) {
return c.init().Session.CombinedOutput(c.String())
if err := c.init(); err != nil {
return nil, errors.Wrap(err, "cmd init")
}
return c.Session.CombinedOutput(c.String())
}

// Output runs cmd on the remote host and returns its stdout.
func (c *Cmd) Output() ([]byte, error) {
return c.init().Session.Output(c.String())
if err := c.init(); err != nil {
return nil, errors.Wrap(err, "cmd init")
}
return c.Session.Output(c.String())
}

// Run runs cmd on the remote host.
func (c *Cmd) Run() error {
return c.init().Session.Run(c.String())
if err := c.init(); err != nil {
return errors.Wrap(err, "cmd init")
}
return c.Session.Run(c.String())
}

// Start runs the command on the remote host.
func (c *Cmd) Start() error {
return c.init().Session.Start(c.String())
if err := c.init(); err != nil {
return errors.Wrap(err, "cmd init")
}
return c.Session.Start(c.String())
}

// String return the command line string.
Expand All @@ -52,14 +65,16 @@ func (c *Cmd) String() string {
}

// Init inits and sets session env vars.
func (c *Cmd) init() *Cmd {
func (c *Cmd) init() (err error) {

// Set session env vars
var env []string
for _, value := range c.Env {
env = strings.Split(value, "=")
c.Setenv(env[0], strings.Join(env[1:], "="))
if err = c.Setenv(env[0], strings.Join(env[1:], "=")); err != nil {
return
}
}

return c
return nil
}
16 changes: 13 additions & 3 deletions examples/goph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,23 @@ func main() {

flag.Parse()

var err error

if agent || goph.HasAgent() {

auth = goph.UseAgent()
auth, err = goph.UseAgent()

} else if pass {

auth = goph.Password(askPass("Enter SSH Password: "))

} else {

auth = goph.Key(key, getPassphrase(passphrase))
auth, err = goph.Key(key, getPassphrase(passphrase))
}

if err != nil {
panic(err)
}

client, err = goph.NewConn(&goph.Config{
Expand Down Expand Up @@ -273,7 +279,11 @@ loop:

default:

out, err = client.Run(cmd)
command, err := client.Command(parts[0], parts[1:]...)
if err != nil {
panic(err)
}
out, err = command.CombinedOutput()
fmt.Println(string(out), err)
}

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/melbahja/goph
go 1.13

require (
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.12.0
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13 // indirect
golang.org/x/sys v0.0.0-20201110211018-35f3e6cf4a65 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13 h1:5jaG59Zhd+8ZXe8C+lgiAGqkOaZBruqrWclLkgAww34=
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201110211018-35f3e6cf4a65 h1:Qo9oJ566/Sq7N4hrGftVXs8GI2CXBCuOd4S2wHE/e0M=
golang.org/x/sys v0.0.0-20201110211018-35f3e6cf4a65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit 0a8e7c4

Please sign in to comment.