Shell
Docs: https://docs.api.kunkun.sh/types/ui.IShell
Shell API provides a way to execute shell commands/scripts and spawn processes.
Commands executed runs at once and are not interactive. stdout
and stderr
are captured and returned as a result.
If you are running a long running process and need to capture the stdout
/stderr
stream, or interact with stdin
, you should use the spawn
API.
More examples will be provided below.
All shell permissions are scoped, read the documentation carefully before using them. Without proper permissions, KK’s API will deny the shell command.
API and Permissions
Here is a list of APIs and permissions required to use them.
-
execute
: [shell:all
,shell:execute
] -
kill
: [shell:all
,shell:kill
] -
stdinWrite
: [shell:all
,shell:stdin-write
,shell:execute
] -
open
: [shell:all
,shell:open
] -
rawSpawn
: [shell:all
,shell:spawn
] -
executeBashScript
: [shell:all
,shell:execute
] -
executePowershellScript
: [shell:all
,shell:execute
] -
executeAppleScript
: [shell:all
,shell:execute
] -
executePythonScript
: [shell:all
,shell:execute
] -
executeZshScript
: [shell:all
,shell:execute
] -
executeNodeScript
: [shell:all
,shell:execute
] -
hasCommand
: [ ] -
likelyOnWindows
: [shell:all
,shell:execute
]
Command
Execute Command
To execute a command, you need to add scoped permission for the command you are executing.
This is used to prevent extensions from being hacked and running injected malicious code.
So always make the permission as specific as possible.
Here is an example permission for the sample code above.
Each item in the args
array is a regex to match the argument.
You can add a permission like this to allow dynamic arguments.
Spawn Command
Executed command runs at once and are not interactive. stdout
and stderr
are captured and returned as a result.
If you are running a long running process and need to capture the stdout
/stderr
stream or event interact with stdin
, you should use the spawn
API.
For example, this could be useful if you are converting a video file with ffmpeg
, and need to read stdout to get the progress throughout the conversion process.
Here is a simplified example of how to use the spawn
API.
Spawn also requires its own scoped permission (shell:spawn
).
Execute Scripts Directly
Sometimes adding args one by one is inconvenient, especially for extensions that need to run a lot of shell scripts (like an emulated terminal).
KK provides some convenient methods for executing scripts directly.
This API allows extension to run any shell script, which could be potentially unsafe.
executeBashScript
(bash -c
)shellExecutePowershellScript
(powershell -Command
)executeAppleScript
(osascript -e
)executePythonScript
(python -c
)executeZshScript
(zsh -c
)executeNodeScript
(node -e
)
Permissions
Direct shell script execution use the entire script as one argument. Thus the scoped permission will look like this.
program
is the command to execute, first arg can be -c
, -e
etc. depending on which shell you are using. Check the list above.
The second argument is a regex to match the script. In this example .+
is used to match any script, you should replace it with a more specific regex.
Make Command Script
The previous section provides APIs to execute scripts directly. You can’t interact with the script.
In this section, we will create a command script object and execute/spawn it.
And of course, you need to add permission for running bash scripts.
More Example Code
Promisify spawn
You can turn the spawned command into a promise, so you can easily work with it.
Here we define a custom execute
function that returns a promise.
You can write custom logic to handle the stdout
and stderr
stream.
The final output is returned as a promise.