InteractiveCmd (interactive_cmd v0.1.3)

Copy Markdown View Source

Run interactive shell commands from mostly pure Elixir

Summary

Types

Options for cmd/3

Functions

Check if cmd/3 should work

Executes the given command with args interactively

Executes the given command interactively

Types

options()

@type options() :: [env: Enumerable.t(), cd: String.t(), log_path: String.t()]

Options for cmd/3

Functions

check_requirements()

@spec check_requirements() :: :ok | {:error, String.t()}

Check if cmd/3 should work

Returns :ok if it looks like the requirements are met. An error tuple is returned if not. The caller can either implement a fallback or show the user the error message.

cmd(command, args, options \\ [])

@spec cmd(binary(), [binary()], options()) ::
  {binary(), exit_status :: non_neg_integer()}

Executes the given command with args interactively

This shell will take over the terminal so that it's possible for the user to interact with whatever program is run. All input is sent to the command including CTRL+C. This allows you to invoke interactive commands like those that request passwords, prompt for input, or show an interactive text-based UI.

This uses :user_drv internally so it only works at the startup console or other consoles that use it.

Options:

  • :env - a map of string key/value pairs to be put into the environment. See System.put_env/1.
  • :cd - the directory to run the command in. See System.cmd/3.
  • :log_path - if specified, all output is logged to this file. Defaults to /dev/null

Returns {"", exit_status} where the first element is always an empty string and the second is the exit status of the command. This return value is intentionally similar to System.cmd/3 to allow InteractiveCmd,cnd/3 to be swapped in quickly when needed.

shell(command, options \\ [])

@spec shell(binary(), options()) :: {binary(), exit_status :: non_neg_integer()}

Executes the given command interactively

It uses sh to evaluate the command.

Watch out

Use this function with care. In particular, never pass untrusted user input to this function, as the user would be able to perform "command injection attacks" by executing any code directly on the machine. Generally speaking, prefer to use cmd/3 over this function.

See cmd/3 for more information and options.