gomacro icon indicating copy to clipboard operation
gomacro copied to clipboard

feature request: shell commands

Open j0hnsmith opened this issue 7 years ago • 5 comments

Would be great to run commands prefixed with ! in a shell like in ipython (and others?), eg

gomacro> !ls -la
drwxr-xr-x    6 peter  staff   204 12 Jul 22:49 .
drwxr-xr-x    3 peter  staff   102 12 Apr  2014 ..
drwxr-xr-x  140 peter  staff  4760 19 Mar  2016 5072f70927f95a13e54e775676c51ee144a14dd7
drwxr-xr-x  264 peter  staff  8976 13 Aug  2017 5a774f194ac60210f83ba3cfa6c7a769f5600dee

More examples https://jakevdp.github.io/PythonDataScienceHandbook/01.05-ipython-and-shell-commands.html#Shell-Commands-in-IPython

j0hnsmith avatar Jul 13 '18 07:07 j0hnsmith

It would be useful - I am just wondering whether it's slightly out of scope or not...

If implemented, we will have to choose a different special char for shell commands: ! is negation in go, and can already appear at line start - for example !foo() && !bar().

Maybe $ ?

cosmos72 avatar Jul 15 '18 21:07 cosmos72

: is used as a prefix to indicate interpreter commands, so how about :!ls -la (! is used for shell in many programs)?

As for being out of scope, I think it's a pretty common REPL feature.

j0hnsmith avatar Jul 16 '18 07:07 j0hnsmith

Mysql repl uses mysql> \! ls -la, psql uses the same syntax.

j0hnsmith avatar Jul 16 '18 08:07 j0hnsmith

Both :!ls -la and \!ls -la are feasible, but I think we can do better:

Special commands take a whole line, and can only used at top level, so exchanging data between a special command and Go must be implemented manually by the special command.

If instead I implement an expression that invokes the shell and returns its output (and error), it can be used anywhere - inside functions, as for condition, etc.

What do you think about something like:

// plain shell syntax, prefix is $
$ls -al

to run a shell command and collect its output as []string, surround it with $()

for i, name := range $(ls -al) {
    println(name)
}

to pass Go expressions (variables, function/method calls...) to shell command, surround them with {} like ipython does:

dir := "/usr/bin"
for i, name := range $(ls -al {dir}) {
    println(name)
}

standard error would be available as second return value:

out, err := $(some_command)

cosmos72 avatar Jul 16 '18 20:07 cosmos72

Using ,? If the first char of an input is , the line is treated as shell.

I believe , cannot be a valid char to start an expression. Also , (and .) is right below finger tip and there is no need for shift key. Dot (.) would be nice but it can be the first char of a valid expression (.6 as floating point).

vitesse-ftian avatar Jul 24 '18 00:07 vitesse-ftian