Skip to content
New issue

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

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

Already on GitHub? # to your account

add run command for osx #1

Merged
merged 1 commit into from
Oct 10, 2017
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
30 changes: 30 additions & 0 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"

"github.com/mikkeloscar/sshconfig"
Expand All @@ -11,6 +13,7 @@ import (

var (
path string
CMD = "tell application \"Terminal\" to do script \"%s\" in selected tab of the front window"
)

func list(c *cli.Context) error {
Expand All @@ -35,6 +38,7 @@ func list(c *cli.Context) error {
}

func add(c *cli.Context) error {
fmt.Println("add ", c.Args())
if err := argumentsCheck(c, 2, 2); err != nil {
return err
}
Expand Down Expand Up @@ -179,3 +183,29 @@ func backup(c *cli.Context) error {
whiteBoldColor.Printf("backup ssh config to ('%s') successfully.", backupPath)
return nil
}

func run(c *cli.Context) error {
if err := argumentsCheck(c, 1, 2); err != nil {
return err
}

alias := c.Args().Get(0)
hosts, _ := sshconfig.ParseSSHConfig(path)
hostMap := getHostsMap(hosts)
host, ok := hostMap[alias]
if !ok {
printErrorFlag()
return cli.NewExitError(fmt.Sprintf("ssh alias('%s') not found.", alias), 1)
}
ssh := fmt.Sprintf("ssh %s@%s -p %d", host.User, host.HostName, host.Port)
sshCMD := exec.Command("/usr/bin/osascript", "-e", "tell application \"Terminal\" to activate", "-e", "tell application \"System Events\" to tell process \"Terminal\" to keystroke \"t\" using command down", "-e", fmt.Sprintf(CMD, ssh))
sshCMD.Env = os.Environ()
output, err := sshCMD.CombinedOutput()
fmt.Println("out: ", string(output))

if err != nil {
fmt.Println("run cmd err:", err)
}
return err

}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ func commands() []cli.Command {
},
{Name: "delete", Usage: "delete existing ssh alias record", Action: delete, Aliases: []string{"d"}},
{Name: "backup", Usage: "backup ssh alias config records", Action: backup, Aliases: []string{"b"}},
{Name: "run", Usage: "run ssh alias only for osx", Action: run, Aliases: []string{"r"}},
}
}