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 b8babd1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions 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,9 +733,13 @@ 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" {
if rootFlags.assumeYes || domain == "localhost" {
promptForDownload = false
shouldPullImage = true
}
Expand Down

0 comments on commit b8babd1

Please sign in to comment.