Skip to content

Commit

Permalink
support take user param for run command
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Oct 14, 2017
1 parent 058d8e8 commit 2702819
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 19 deletions.
15 changes: 13 additions & 2 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -136,11 +138,20 @@ func runAction(c *cli.Context) error {
alias := c.Args().Get(0)
command := c.Args().Get(1)

exists, user, hostname, port := getHostConnect(alias)
userAlias := strings.Split(alias, "@")
tmpUser, tmpAlias := "", alias
if len(userAlias) == 2 {
tmpUser, tmpAlias = userAlias[0], userAlias[1]
}

exists, user, hostname, port, identityfile := getHostConnect(tmpAlias)
if !exists {
user, hostname, port = parseConnct(alias)
} else if tmpUser != "" {
user = tmpUser
}
session, err := createSession(c.Bool("password"), user, hostname, port)
log.Println(user, hostname, port)
session, err := createSession(c.Bool("password"), user, hostname, port, identityfile)
if err != nil {
printErrorFlag()
return cli.NewExitError(err, 1)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {

func flags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{Name: "file, f", Value: fmt.Sprintf("%s/.ssh/config", os.Getenv("HOME")), Destination: &path},
cli.StringFlag{Name: "file, f", Value: fmt.Sprintf("%s/.ssh/config", getHomeDir()), Destination: &path},
}
}

Expand Down
14 changes: 9 additions & 5 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (
"os"

"github.com/howeyc/gopass"
homedir "github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/terminal"
)

var keyPath = fmt.Sprintf("%s/.ssh/id_rsa", os.Getenv("HOME"))

func getAuthMethod(passwordAuth bool) (ssh.AuthMethod, error) {
func getAuthMethod(passwordAuth bool, keyPath string) (ssh.AuthMethod, error) {
var auth ssh.AuthMethod
var err error
if passwordAuth {
Expand Down Expand Up @@ -82,8 +81,13 @@ func readPrivateKey(file string) ([]byte, error) {
return privateKey, nil
}

func createSession(passwordAuth bool, user, hostname, port string) (*ssh.Session, error) {
auth, err := getAuthMethod(passwordAuth)
func createSession(passwordAuth bool, user, hostname, port, identityfile string) (*ssh.Session, error) {
if identityfile == "" {
identityfile = fmt.Sprintf("%s/.ssh/id_rsa", getHomeDir())
} else {
identityfile, _ = homedir.Expand(identityfile)
}
auth, err := getAuthMethod(passwordAuth, identityfile)
if err != nil {
return nil, err
}
Expand Down
26 changes: 15 additions & 11 deletions sshconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ type hostConfig struct {
}

const (
USER = "user"
HOSTNAME = "hostname"
PORT = "port"
USER = "user"
HOSTNAME = "hostname"
PORT = "port"
IDENTITY_FILE = "identityfile"
)

func checkAlias(aliasMap map[string]*ssh_config.Host, expectExist bool, aliases ...string) error {
Expand Down Expand Up @@ -48,22 +49,23 @@ func parseConfig() (*ssh_config.Config, map[string]*ssh_config.Host) {
return cfg, aliasMap
}

func getHostConnect(alias string) (bool, string, string, string) {
func getHostConnect(alias string) (bool, string, string, string, string) {
_, aliasMap := parseConfig()
host := aliasMap[alias]
if host == nil {
return false, "", "", ""
return false, "", "", "", ""
}
connectMap := map[string]string{USER: "", HOSTNAME: "", PORT: ""}
connectMap := map[string]string{USER: "", HOSTNAME: "", PORT: "", IDENTITY_FILE: ""}
for _, node := range host.Nodes {
switch t := node.(type) {
case *ssh_config.KV:
if _, ok := connectMap[t.Key]; ok {
connectMap[t.Key] = t.Value
lk := strings.ToLower(t.Key)
if _, ok := connectMap[lk]; ok {
connectMap[lk] = t.Value
}
}
}
return true, connectMap[USER], connectMap[HOSTNAME], connectMap[PORT]
return true, connectMap[USER], connectMap[HOSTNAME], connectMap[PORT], connectMap[IDENTITY_FILE]
}

func listHost(keywords ...string) ([]*hostConfig, map[string]string) {
Expand Down Expand Up @@ -140,9 +142,11 @@ func addHost(host *hostConfig) error {
checkKeyRepeat := map[string]bool{USER: true, HOSTNAME: true, PORT: true}
deleteKeys := []string{}
for k, v := range host.config {
if !checkKeyRepeat[strings.ToLower(k)] {
node := &ssh_config.KV{Key: k, Value: v}
lk := strings.ToLower(k)
if !checkKeyRepeat[lk] {
node := &ssh_config.KV{Key: lk, Value: v}
nodes = append(nodes, node.SetLeadingSpace(4))
checkKeyRepeat[lk] = true
} else {
deleteKeys = append(deleteKeys, k)
}
Expand Down
9 changes: 9 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"os"
"os/user"
"strconv"
"strings"
Expand Down Expand Up @@ -120,3 +121,11 @@ func contains(values []string, key string) bool {
}
return false
}

func getHomeDir() string {
user, err := user.Current()
if nil == err && user.HomeDir != "" {
return user.HomeDir
}
return os.Getenv("HOME")
}
21 changes: 21 additions & 0 deletions vendor/github.com/mitchellh/go-homedir/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/mitchellh/go-homedir/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 137 additions & 0 deletions vendor/github.com/mitchellh/go-homedir/homedir.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
"revision": "501286685510343458a6e698a18174f5242749f3",
"revisionTime": "2017-06-14T17:46:21Z"
},
{
"checksumSHA1": "V/quM7+em2ByJbWBLOsEwnY3j/Q=",
"path": "github.com/mitchellh/go-homedir",
"revision": "b8bc1bf767474819792c23f32d8286a45736f1c6",
"revisionTime": "2016-12-03T19:45:07Z"
},
{
"checksumSHA1": "F1IYMLBLAZaTOWnmXsgaxTGvrWI=",
"path": "github.com/pelletier/go-buffruneio",
Expand Down

0 comments on commit 2702819

Please # to comment.