diff --git a/core/core_unix.go b/core/core_unix.go new file mode 100644 index 00000000..4eb09092 --- /dev/null +++ b/core/core_unix.go @@ -0,0 +1,5 @@ +package core + +func Shell(cmd string) (string, error) { + return Exec("/bin/sh", []string{"-c", cmd}) +} diff --git a/core/core_windows.go b/core/core_windows.go new file mode 100644 index 00000000..4773e290 --- /dev/null +++ b/core/core_windows.go @@ -0,0 +1,5 @@ +package core + +func Shell(cmd string) (string, error) { + return Exec("cmd.exe", []string{"/c", cmd}) +} diff --git a/session/session_core_handlers.go b/session/session_core_handlers.go index f2be1e09..ae1baa0c 100644 --- a/session/session_core_handlers.go +++ b/session/session_core_handlers.go @@ -165,6 +165,14 @@ func (s *Session) includeHandler(args []string, sess *Session) error { return s.RunCaplet(args[0]) } +func (s *Session) shHandler(args []string, sess *Session) error { + out, err := core.Shell(args[0]) + if err == nil { + fmt.Printf("%s\n", out) + } + return err +} + func (s *Session) addHandler(h CommandHandler, c *readline.PrefixCompleter) { h.Completer = c s.CoreHandlers = append(s.CoreHandlers, h) @@ -254,4 +262,10 @@ func (s *Session) registerCoreHandlers() { files, _ = filepath.Glob(prefix + "*") return files }))) + + s.addHandler(NewCommandHandler("! COMMAND", + "^!\\s*(.+)$", + "Execute a shell command and print its output.", + s.shHandler), + readline.PcItem("!")) }