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

Fix invalid flag processing when no flags passed on command line #26

Merged
merged 1 commit into from
Jun 10, 2024

Conversation

mrutkows
Copy link
Contributor

@mrutkows mrutkows commented May 29, 2024

After first building the license-scanner I decided to install/run it to see the help and what flags were available/documented. However, to my surprise, the executable terminated (exited with no error) and displayed no help.

Debugging this I found that the flag "configurer.UpdateAllFlag" was being retrieved as a string (getString()) despite it being a boolean flag. Cobra returned no error on the existing code, but returns an empty string ("") which was being checked for and caused the "return importer.Update(cfg)" method to be called which has nothing to do so it exits (with no help, message or error return code).

This PR correctly reads the flag as a Bool and actually causes the desired behavior which displays:

$ license-scanner
Error: you must provide a file path
Usage:
  license-scanner [flags]

Flags:
  -g, --acceptable          Flag acceptable
      --addAll string       Add licenses from this dir to spdx, spdxPath, custom or customPath dir
      --configName string   Base name for config file (default "config")
      --configPath string   Path to any config files
  -c, --copyrights          Flag copyrights
      --custom string       Custom templates to use (default "default")
      --customPath string   Path to external custom templates to use
  -d, --debug               Enable debug logging
      --dir string          A directory in which to identify licenses
  -f, --file string         A file in which to identify licenses
  -x, --hash                Output file hash
  -h, --help                help for license-scanner
  -k, --keywords            Flag keywords
  -l, --license string      Display match debugging for the given license
      --list                List the license templates to be used
  -n, --normalized          Flag normalized
  -q, --quiet               Set logging to quiet
      --spdx string         Set of embedded SPDX templates to use (default "default")
      --spdxPath string     Path to external SPDX templates to use
      --updateAll           Update existing licenses
  -v, --version             version for license-scanner

from the root command's RunE() method logic here:

			if f != "" {
				return findLicensesInFile(cfg, f)
			} else if cfg.GetString(configurer.DirFlag) != "" {
				return findLicensesInDirectory(cfg)
			} else if cfg.GetBool(configurer.ListFlag) {
				return listLicenses(cfg)
			} else if cfg.GetString(configurer.AddAllFlag) != "" {
				return importer.Import(cfg)
			} else if cfg.GetBool(configurer.UpdateAllFlag) {
				return importer.Update(cfg)
			} else {
				// Otherwise, terminate with an error.
				return errors.New("you must provide a file path")
			}

Fixes: #28

@mrutkows mrutkows requested a review from markstur May 29, 2024 19:30
@mrutkows mrutkows self-assigned this Jun 7, 2024
@markstur markstur merged commit 7240bb4 into CycloneDX:main Jun 10, 2024
4 checks passed
@mrutkows mrutkows deleted the spdx-id branch June 11, 2024 14:01
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

Successfully merging this pull request may close these issues.

license-scanner CLI run with no args provides no help
2 participants