Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,19 +486,15 @@ func createContainer(container, image, release, authFile string, showCommandToEn
logrus.Debugf("%s", arg)
}

s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s.Prefix = fmt.Sprintf("Creating container %s: ", container)
s.Start()
defer s.Stop()
}
s := startSpinner(fmt.Sprintf("Creating container %s: ", container))
defer stopSpinner(s)

if err := shell.Run("podman", nil, nil, nil, createArgs...); err != nil {
return fmt.Errorf("failed to create container %s", container)
}

// The spinner must be stopped before showing the 'enter' hint below.
s.Stop()
stopSpinner(s)

if showCommandToEnter {
fmt.Printf("Created container: %s\n", container)
Expand Down Expand Up @@ -735,12 +731,8 @@ func pullImage(image, release, authFile string) (bool, error) {

logrus.Debugf("Pulling image %s", imageFull)

if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
s.Prefix = fmt.Sprintf("Pulling %s: ", imageFull)
s.Start()
defer s.Stop()
}
s := startSpinner(fmt.Sprintf("Pulling %s: ", imageFull))
defer stopSpinner(s)

if err := podman.Pull(imageFull, authFile); err != nil {
var builder strings.Builder
Expand Down Expand Up @@ -963,6 +955,22 @@ func showPromptForDownload(imageFull string) bool {
return shouldPullImage
}

func startSpinner(message string) *spinner.Spinner {
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
s.Prefix = message
s.Start()
return s
}
return nil
}
Comment on lines +958 to +966

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The current implementation of startSpinner returns nil when the log level is Debug or higher. However, there is a direct call to s.Stop() at line 497 (visible in the full file but outside this diff hunk) that does not check for nil. This will cause a panic when running with verbose logging (e.g., toolbox create --verbose).

To fix this, you should either use the stopSpinner(s) helper at line 497 or refactor startSpinner to always return a spinner instance that is only started conditionally, as suggested below.

func startSpinner(message string) *spinner.Spinner {
	s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
	if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
		s.Prefix = message
		s.Start()
	}
	return s
}


func stopSpinner(s *spinner.Spinner) {
if s != nil {
s.Stop()
}
}

// systemdNeedsEscape checks whether a byte in a potential dbus ObjectPath needs to be escaped
func systemdNeedsEscape(i int, b byte) bool {
// Escape everything that is not a-z-A-Z-0-9
Expand Down
41 changes: 41 additions & 0 deletions src/pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,49 @@ func RunContextWithExitCode(ctx context.Context,
return 0, nil
}

func RunContextWithExitCode2(ctx context.Context,
name string,
stdin io.Reader,
stdout, stderr io.Writer,
arg ...string) (int, error) {

logLevel := logrus.GetLevel()
if stderr == nil && logLevel >= logrus.DebugLevel {
stderr = os.Stderr
}

cmd := exec.CommandContext(ctx, name, arg...)
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr

if err := cmd.Run(); err != nil {
exitCode := 1

if ctxErr := ctx.Err(); ctxErr != nil {
return 1, ctxErr
}

var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
exitCode = exitErr.ExitCode()
return exitCode, err
}

return exitCode, err
}

return 0, nil
}

func RunWithExitCode(name string, stdin io.Reader, stdout, stderr io.Writer, arg ...string) (int, error) {
ctx := context.Background()
exitCode, err := RunContextWithExitCode(ctx, name, stdin, stdout, stderr, arg...)
return exitCode, err
}

func RunWithExitCode2(name string, stdin io.Reader, stdout, stderr io.Writer, arg ...string) (int, error) {
ctx := context.Background()
exitCode, err := RunContextWithExitCode2(ctx, name, stdin, stdout, stderr, arg...)
return exitCode, err
}
14 changes: 14 additions & 0 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,20 @@ func IsP11KitClientPresent() (bool, error) {
return false, err
}

func IsSupportedDistroImage(image string) bool {
basename := ImageReferenceGetBasename(image)
if basename == "" {
return false
}

for _, distroObj := range supportedDistros {
if distroObj.ImageBasename == basename {
return true
}
}
return false
}

func SetUpConfiguration() error {
logrus.Debug("Setting up configuration")

Expand Down
Loading