charmbracelet / gum

A tool for glamorous shell scripts 🎀
MIT License
17.94k stars 339 forks source link

`gum log -l fatal` no longer returns error status code #574

Closed alexschcom closed 2 months ago

alexschcom commented 4 months ago

Describe the bug Calling gum log -l fatal <...> used to return an error status code which would stop execution of further commands.

To Reproduce Steps to reproduce the behavior:

  1. Execute gum log -l fatal hello; echo world
  2. gum logs fatal msg but continues to execute echo.

Expected behavior the log library calls os.Exit(1) on log.Fatal(err). Correspondingly, using gum log -l fatal should also stop execution of the script.

Desktop (please complete the following information):

MikaelFangel commented 4 months ago

I checked and gum log -l fatal hello indeed returns an error status code. More precisely it returns 1. (See the image)

Screenshot 2024-05-13 at 13 46 07

You should use && instead of ; between your commands. The reason is that && ensures the second command only runs if the first command succeeds. This can help prevent potential issues if the first command fails for some reason.

alexschcom commented 4 months ago

Hm, you're right. I'm still confused though since I use gum log -l fatal in one of my scripts and it did exit the entire script before. Now it keeps executing (unless chained with &&, like you said).

Wouldn't it make sense to exit when encountering a fatal issue? As in:

if [[ -z $1 ]]; then gum log -l fatal "Not enough arguments"; fi
MikaelFangel commented 4 months ago

I have tried to checkout the tags v0.13.0 and v0.12.0 and I can't reproduce the behaviour you're talking about. Can you please provide some more details like a version number and a reproducible example of the behaviour you think have changed.

Also you can stop the script by checking if the error code is 1.

alexschcom commented 3 months ago

I am using version 0.14.1. I remember before the 0.14.x update, calling gum log -l fatal would stop execution of following commands in a script. For example:

gum log -l fatal "Some error occurred"
echo hello

Here, the echo command would not execute, since gum log -l fatal would automatically exit the script, as it does in Go files (see here). This makes sense, considering we're dealing with a fatal error. Now, however, it does execute the following commands...

piero-vic commented 3 months ago

@alexschcom You can add set -e at the begining of your script to exit immediately if a command fails.

#!/usr/bin/env bash

set -e

gum log -l fatal "Some error occurred"
echo hello
caarlos0 commented 2 months ago

seems to be working on main.

alexschcom commented 2 months ago

What seems to be working?

Screenshot 2024-07-26 at 3 29 11 PM
caarlos0 commented 2 months ago

on main, not released yet.