Skip to content

Commit

Permalink
add run cmd for osx
Browse files Browse the repository at this point in the history
  • Loading branch information
trigged committed Oct 10, 2017
1 parent 4e6aa19 commit cd3d07f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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"}},
}
}

0 comments on commit cd3d07f

Please # to comment.