Skip to content

Commit 2cbbb95

Browse files
committed
main.go: print ExitError before exiting
Fix issue 4167 Signed-off-by: Akihiro Suda <[email protected]>
1 parent cb6b4cd commit 2cbbb95

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

cmd/limactl/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ func main() {
4848
}
4949
}
5050
err := newApp().Execute()
51-
server.StopAllExternalDrivers()
52-
osutil.HandleExitError(err)
5351
if err != nil {
54-
logrus.Fatal(err)
52+
logrus.StandardLogger().Log(logrus.FatalLevel, err)
5553
}
54+
server.StopAllExternalDrivers()
55+
os.Exit(osutil.HandleExitError(err))
5656
}
5757

5858
func processGlobalFlags(rootCmd *cobra.Command) error {

cmd/yq/yq.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
"strings"
1414

1515
command "github.com/mikefarah/yq/v4/cmd"
16+
"github.com/sirupsen/logrus"
17+
18+
"github.com/lima-vm/lima/v2/pkg/osutil"
1619
)
1720

1821
func main() {
@@ -24,11 +27,11 @@ func main() {
2427
newArgs := []string{"eval"}
2528
cmd.SetArgs(append(newArgs, os.Args[1:]...))
2629
}
27-
code := 0
28-
if err := cmd.Execute(); err != nil {
29-
code = 1
30+
err = cmd.Execute()
31+
if err != nil {
32+
logrus.StandardLogger().Log(logrus.FatalLevel, err)
3033
}
31-
os.Exit(code)
34+
os.Exit(osutil.HandleExitError(err))
3235
}
3336

3437
// MaybeRunYQ runs as `yq` if the program name or first argument is `yq`.

pkg/osutil/exit.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ package osutil
55

66
import (
77
"errors"
8-
"os"
98
"os/exec"
109
)
1110

12-
func HandleExitError(err error) {
11+
func HandleExitError(err error) int {
1312
if err == nil {
14-
return
13+
return 0
1514
}
1615

1716
var exitErr *exec.ExitError
1817
if errors.As(err, &exitErr) {
19-
os.Exit(exitErr.ExitCode()) //nolint:revive // it's intentional to call os.Exit in this function
20-
return
18+
return exitErr.ExitCode()
2119
}
20+
return 1
2221
}

pkg/plugins/plugins.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,10 @@ func (plugin *Plugin) Run(ctx context.Context, args []string) {
160160
cmd.Env = os.Environ()
161161

162162
err := cmd.Run()
163-
osutil.HandleExitError(err)
164-
if err == nil {
165-
os.Exit(0) //nolint:revive // it's intentional to call os.Exit in this function
163+
if err != nil {
164+
logrus.StandardLogger().WithError(err).Logf(logrus.FatalLevel, "external command %q failed", plugin.Path)
166165
}
167-
logrus.Fatalf("external command %q failed: %v", plugin.Path, err)
166+
os.Exit(osutil.HandleExitError(err)) //nolint:revive // it's intentional to call os.Exit in this function
168167
}
169168

170169
var descRegex = regexp.MustCompile(`<limactl-desc>(.*?)</limactl-desc>`)

0 commit comments

Comments
 (0)