Skip to content

Commit

Permalink
Add a '--notty' option to the 'run' cmd
Browse files Browse the repository at this point in the history
This disables usage of options '--tty' and '--interactive' in the 'run'
cmd (passed to (podman exec').

This is useful when executing apps from within container using dmenu,
rofi and similar tools.

Based on containers#179
  • Loading branch information
tosmi authored and HarryMichal committed Mar 13, 2020
1 parent ecdfc21 commit 5f1bcaa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
runFlags struct {
useDefaultContainer bool
releaseVersion string
notty bool
emitEscapeSequence bool
fallbackToBash bool
promptForCreate bool
Expand All @@ -58,6 +59,7 @@ func init() {

flags.BoolVarP(&runFlags.useDefaultContainer, "default", "d", false, "Run command inside the system default container (eg. on Fedora 31 -> fedora-toolbox-31)")
flags.StringVarP(&runFlags.releaseVersion, "release", "r", "", "Run command inside a toolbox container with the release version")
flags.BoolVarP(&runFlags.notty, "notty", "n", false, "Run command inside a toolbox container but do not allocate a tty")
flags.BoolVar(&runFlags.emitEscapeSequence, "escape-sequence", false, "Emit an escape sequence for terminals")
flags.BoolVar(&runFlags.fallbackToBash, "fallback-to-bash", false, "If requested program does not exist, fallback to bash")
flags.BoolVar(&runFlags.promptForCreate, "prompt-for-create", true, "Offer to create a container if it does not exist")
Expand Down Expand Up @@ -238,11 +240,13 @@ func run(args []string) error {
logrus.Infof("Running in container '%s': %v", containerName, commands)

args = []string{"exec",
"--interactive",
"--tty",
"--user", viper.GetString("USER"),
"--workdir", viper.GetString("PWD")}

if !runFlags.notty {
args = append(args, "--interactive", "--tty")
}

// Add the environment variables that hold a value
for _, env := range preservedEnvVars {
value := viper.GetString(env)
Expand Down
5 changes: 5 additions & 0 deletions doc/toolbox-run.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ host OS.
Run command inside a toolbox container for a different operating system
RELEASE than the host.

**--notty** **-n**

Run command inside a toolbox container but do not allocate a tty. This
is mostly useful for running commands from dmenu, rofi and so on.

## EXAMPLES

### Run ls inside a toolbox container using the default image matching the host OS
Expand Down
13 changes: 10 additions & 3 deletions toolbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ toolbox_image=""
toolbox_runtime_directory="$XDG_RUNTIME_DIR"/toolbox
user_id_real=$(id -ru 2>&3)
verbose=false
notty=false


LGC='\033[1;32m' # Light Green Color
Expand Down Expand Up @@ -1588,12 +1589,16 @@ run()

$emit_escape_sequence && printf "\033]777;container;push;%s;toolbox\033\\" "$toolbox_container"

tty_arguments="--interactive --tty"
if $notty; then
tty_arguments=""
fi

# shellcheck disable=SC2016
# for the command passed to capsh
# shellcheck disable=SC2086
$podman_command exec \
--interactive \
--tty \
$tty_arguments \
--user "$USER" \
--workdir "$PWD" \
$set_environment \
Expand Down Expand Up @@ -2178,7 +2183,6 @@ update_container_and_image_names()
return 0
}


arguments=$(save_positional_parameters "$@")

host_id=$(get_host_id)
Expand Down Expand Up @@ -2607,6 +2611,9 @@ case $op in
exit_if_non_positive_argument --release "$arg"
release=$arg
;;
-n | --notty )
notty=true
;;
* )
exit_if_unrecognized_option "$1"
esac
Expand Down

0 comments on commit 5f1bcaa

Please sign in to comment.