From 5f1bcaa3a2486266ba9937362277c39ce16dd2b8 Mon Sep 17 00:00:00 2001 From: Toni Schmidbauer Date: Tue, 28 May 2019 18:07:06 +0200 Subject: [PATCH] Add a '--notty' option to the 'run' cmd 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 #179 --- cmd/run.go | 8 ++++++-- doc/toolbox-run.1.md | 5 +++++ toolbox.sh | 13 ++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index bbf6aabaf..d2289d97f 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -34,6 +34,7 @@ var ( runFlags struct { useDefaultContainer bool releaseVersion string + notty bool emitEscapeSequence bool fallbackToBash bool promptForCreate bool @@ -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") @@ -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) diff --git a/doc/toolbox-run.1.md b/doc/toolbox-run.1.md index 4c63d0657..f62dcff45 100644 --- a/doc/toolbox-run.1.md +++ b/doc/toolbox-run.1.md @@ -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 diff --git a/toolbox.sh b/toolbox.sh index 507d283bf..8726f255e 100755 --- a/toolbox.sh +++ b/toolbox.sh @@ -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 @@ -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 \ @@ -2178,7 +2183,6 @@ update_container_and_image_names() return 0 } - arguments=$(save_positional_parameters "$@") host_id=$(get_host_id) @@ -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