83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
|
package main
|
||
|
|
||
|
// multiple cases of URIs, and we are not dealing with query parameters.
|
||
|
// No shell execution.
|
||
|
// - terminal:///home/user
|
||
|
// - terminal://host:/home/user
|
||
|
// - terminal://host/home/user
|
||
|
// - terminal://user@host:/home/user
|
||
|
// - terminal://user@host:port/home/user
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"net/url"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
var defaultTerminal string = "x-terminal-emulator"
|
||
|
|
||
|
func main() {
|
||
|
flag.Parse()
|
||
|
if flag.NArg() == 0 {
|
||
|
logrus.Fatal("no URI provided!")
|
||
|
}
|
||
|
|
||
|
u, err := url.Parse(flag.Args()[0])
|
||
|
if err != nil {
|
||
|
logrus.Fatalf("failed to parse %q: %s", flag.Args()[0], err)
|
||
|
}
|
||
|
logrus.Infof("%#v", u)
|
||
|
|
||
|
if u.Host != "" {
|
||
|
logrus.Infof("need to use ssh to connect to %q", u.Host)
|
||
|
var user string
|
||
|
if u.User != nil && u.User.Username() != "" {
|
||
|
user = u.User.Username()
|
||
|
} else {
|
||
|
var ok bool
|
||
|
user, ok = os.LookupEnv("USER")
|
||
|
if !ok {
|
||
|
logrus.Fatal("could not determine username to ssh as")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
host := u.Host
|
||
|
port := "22"
|
||
|
if strings.Contains(host, ":") {
|
||
|
args := strings.Split(host, ":")
|
||
|
host = args[0]
|
||
|
if args[1] != "" {
|
||
|
port = args[1]
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cmd := exec.Command(defaultTerminal,
|
||
|
"-e",
|
||
|
fmt.Sprintf(`sh -c 'ssh -p %s -t %s@%s -- "cd %s && $SHELL -l"'`, port, user, host, u.Path),
|
||
|
)
|
||
|
logrus.Infof("local path shell command(simple): %s", cmd.String())
|
||
|
logrus.Infof("local path shell command(struct): %#v", cmd)
|
||
|
if err := cmd.Run(); err != nil {
|
||
|
logrus.Fatalf("failed to run the terminal in a local path: %s", err)
|
||
|
}
|
||
|
} else {
|
||
|
cmd := exec.Command(defaultTerminal,
|
||
|
"-e",
|
||
|
fmt.Sprintf(`sh -c 'if [ -d %q ] ; then cd %q && $SHELL ; else echo "WARNING: %q not found" && $SHELL ; fi'`, u.Path, u.Path, u.Path),
|
||
|
)
|
||
|
logrus.Infof("local path shell command(simple): %s", cmd.String())
|
||
|
logrus.Infof("local path shell command(struct): %#v", cmd)
|
||
|
if err := cmd.Run(); err != nil {
|
||
|
logrus.Fatalf("failed to run the terminal in a local path: %s", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// TODO maybe not _all_ terminals accept the same flags as gnome and xfce terminal. May need a case statement for generic situations.
|