Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech Debt Request: Avoid calling os.Exit() in exported functions #3526

Open
YrrepNoj opened this issue Feb 20, 2025 · 1 comment
Open

Tech Debt Request: Avoid calling os.Exit() in exported functions #3526

YrrepNoj opened this issue Feb 20, 2025 · 1 comment

Comments

@YrrepNoj
Copy link
Contributor

YrrepNoj commented Feb 20, 2025

Zarf has a publicly accessible function src/cmd/root.go -> Execute() that calls os.Exit().

It is fairly easy for an external project to call this function and have unexpected results in their program because they were not anticipating a library they were using to call os.Exit().

The only place within the Zarf project calling Execute() is within main.go.

I would like to propose having Execute() return an int or an err and let the calling main() perform the os.Exit().

package main

import (
        .....
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	signalCh := make(chan os.Signal, 1)
	signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)

        ........
        ........
        ........

-       cmd.Execute(ctx)
+	os.Exit(cmd.Execute(ctx))

}

After writing this out, I realize I am asking for a breaking change, since this will change the function signature of Execute, so I won't be sad at all if the team decides to just disregard this request.

@YrrepNoj YrrepNoj changed the title Tech Debt Request: Avoid calling os.Exit() is exported functions Tech Debt Request: Avoid calling os.Exit() in exported functions Feb 20, 2025
@AustinAbro321
Copy link
Contributor

@YrrepNoj I would agree that it makes sense to call os.Exit in main rather than in a package. However, if we introduced a breaking change here, I'd rather make cmd.Execute private, and instead encourage users to call the function NewZarfCommand() to either embed Zarf directly with cmd.AddCommand(cmds ...*Command) or by calling NewZarfCommand().ExecuteContextC(ctx) when not using Cobra. All cmd.Execute does is log the error and exit.

If NewZarfCommand works for you, I can spin up a PR with those changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants