Skip to content

Commit

Permalink
cmd/create: Require valid terminal when prompting to pull an image
Browse files Browse the repository at this point in the history
To limit the possible misbehaviour of Toolbx when ued in scripting,
require the presence of valid terminal on stdin when an image needs to
be pulled to create a new toolbx.
  • Loading branch information
HarryMichal committed Dec 19, 2023
1 parent ccc3eee commit 213897d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ var (
{"/etc/profile.d/toolbox.sh", "/etc/profile.d/toolbox.sh"},
{"/etc/profile.d/toolbox.sh", "/usr/share/profile.d/toolbox.sh"},
}

errorNotTerminal = errors.New("terminal is not connected")
)

var createCmd = &cobra.Command{
Expand Down Expand Up @@ -219,6 +221,15 @@ func createContainer(container, image, release, authFile string, showCommandToEn

pulled, err := pullImage(image, release, authFile)
if err != nil {
if errors.Is(err, errorNotTerminal) {
var builder strings.Builder
fmt.Fprintf(&builder, "User interaction required to pull a new image. Stopping now.\n")
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)

errMsg := builder.String()
return errors.New(errMsg)
}

return err
}
if !pulled {
Expand Down Expand Up @@ -722,7 +733,11 @@ func pullImage(image, release, authFile string) (bool, error) {
}

promptForDownload := true
var shouldPullImage bool
shouldPullImage := false

if !term.IsTerminal(os.Stdin) {
return false, errorNotTerminal
}

if rootFlags.assumeYes || domain == "localhost" {
promptForDownload = false
Expand Down

0 comments on commit 213897d

Please sign in to comment.