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

I would like a legacy.Execute() without the os.Exit(1) #1435

Closed
harve106 opened this issue Aug 15, 2023 · 5 comments
Closed

I would like a legacy.Execute() without the os.Exit(1) #1435

harve106 opened this issue Aug 15, 2023 · 5 comments
Labels
changelog-ignore Don't include this issue in the release changelog enhancement New feature or request

Comments

@harve106
Copy link

What would you like to be added:
These lines of code in github.com\anchore\grype@v0.65.1\cmd\grype\cli\legacy\cmd.go

func ExecuteNoExit() {
	if err := rootCmd.Execute(); err != nil {
		_ = stderrPrintLnf(err.Error())
	}
}

Why is this needed:
I need it in order to use this function as I would the command line app because it's way easier to screen scrape than to go through APIs for simple usage

@kzantow
Copy link
Contributor

kzantow commented Sep 8, 2023

Hi @harve106 -- there's an upcoming change to the way the CLI is built; it's this the PR. As part of that, there is a cli package that has a Command function, which allows you to get the root command.

In order to use this, you would just have to write something like:

package grype

import (
  "github.com/anchore/clio"
  "github.com/anchore/grype/cmd/cli"
)

func ExecuteGrype() {
  cmd := cli.Command(clio.Identification{
    Name: "my-app-name",
    Version: "my-app-version",
  })
  cmd.Execute()
}

In the case there is an error, Grype would still call os.Exit(exitCode), but it does not call os.Exit without errors.

Would this solve your issue?

@harve106
Copy link
Author

harve106 commented Sep 8, 2023

Not quite, a lot of errors are not fatal and should not trigger an exit of the calling program. Go does not provide a good way for us to allow an OS.exit in a child process without exiting the parent. For certain applications output scraping is desirable, but calling something through a shell scraper is not for numerous reasons. That is the main reason I would like that functionality, to be able to scrape output even if there is an error without killing the parent process.

@harve106
Copy link
Author

harve106 commented Sep 8, 2023

I have a PR that implements the functionality I would like with your old method of calling Execute(), #1436

@kzantow
Copy link
Contributor

kzantow commented Sep 13, 2023

Hi @harve106, the aforementioned PR has been merged and you can run grype CLI using this code:

package main

import (
	"fmt"
	"github.com/anchore/clio"
	"github.com/anchore/grype/cmd/grype/cli"
)

func executeGrype() {
	cmd := cli.Command(clio.Identification{
		Name:    "my-app-name",
		Version: "[not provided]",
	})
	_ = cmd.Execute()
}

func main() {
	executeGrype()
	fmt.Println("post-execute")
}

It does not call os.Exit.

However, I think it would be better to use Grype as a library. I took a stab at this and ended up running into a few issues, but would suggest we look at how to improve the experience of using Grype as a library. Could you expand on your use case at all?

@tgerla tgerla moved this to Awaiting Response in OSS Oct 5, 2023
@tgerla tgerla added the changelog-ignore Don't include this issue in the release changelog label Oct 19, 2023
@tgerla
Copy link
Contributor

tgerla commented Nov 9, 2023

Hey @harve106, we'll go ahead and close this issue out but please feel free to let us know if you need any more help here. Thanks!

@tgerla tgerla closed this as completed Nov 9, 2023
@github-project-automation github-project-automation bot moved this from Awaiting Response to Done in OSS Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-ignore Don't include this issue in the release changelog enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

3 participants