Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Fixes #12 -- support running over multiple binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Gaynor committed Sep 6, 2019
1 parent bc97f64 commit 9a2abe2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ since it uses metadata from the Go compiler to determine the _exact_ set of
dependencies embedded in a compiled Go binary. This excludes dependencies that
are not used in the final binary. For example, if a library depends on "foo"
in function "F" but "F" is never called, then the dependency "foo" will not
be present in the final binary.
be present in the final binary.

golicense is not meant to be a complete replacement for open source compliance
companies such as [FOSSA](https://fossa.io/) or
Expand Down Expand Up @@ -58,6 +58,8 @@ $ golicense [flags] [BINARY]
$ golicense [flags] [CONFIG] [BINARY]
```

You may also pass mutliple binaries (but only if you are providing a CONFIG).

### Configuration File

The configuration file can specify allow/deny lists of licenses for reports,
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ require (
gopkg.in/src-d/go-siva.v1 v1.3.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

go 1.13
68 changes: 38 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,13 @@ func realMain() int {
"❗️ Path to file to analyze expected.\n\n"))
printHelp(flags)
return 1
} else if len(args) > 2 {
fmt.Fprintf(os.Stderr, color.RedString(
"❗️ Exactly one or two arguments is allowed.\n\n"))
printHelp(flags)
return 1
}

// Determine the exe path and parse the configuration if given.
var cfg config.Config
exePath := args[0]
var exePaths []string
if len(args) > 1 {
exePath = args[1]
exePaths = args[1:]

c, err := config.ParseFile(args[0])
if err != nil {
Expand All @@ -74,34 +69,47 @@ func realMain() int {

// Store the config and set it on the output
cfg = *c
} else {
exePaths = args[:1]
}

// Read the dependencies from the binary itself
vsn, err := version.ReadExe(exePath)
if err != nil {
fmt.Fprintf(os.Stderr, color.RedString(fmt.Sprintf(
"❗️ Error reading %q: %s\n", args[0], err)))
return 1
}
allMods := map[module.Module]struct{}{}
for _, exePath := range exePaths {
// Read the dependencies from the binary itself
vsn, err := version.ReadExe(exePath)
if err != nil {
fmt.Fprintf(os.Stderr, color.RedString(fmt.Sprintf(
"❗️ Error reading %q: %s\n", args[0], err)))
return 1
}

if vsn.ModuleInfo == "" {
// ModuleInfo empty means that the binary didn't use Go modules
// or it could mean that a binary has no dependencies. Either way
// we error since we can't be sure.
fmt.Fprintf(os.Stderr, color.YellowString(fmt.Sprintf(
"⚠️ %q ⚠️\n\n"+
"This executable was compiled without using Go modules or has \n"+
"zero dependencies. golicense considers this an error (exit code 1).\n", exePath)))
return 1
if vsn.ModuleInfo == "" {
// ModuleInfo empty means that the binary didn't use Go modules
// or it could mean that a binary has no dependencies. Either way
// we error since we can't be sure.
fmt.Fprintf(os.Stderr, color.YellowString(fmt.Sprintf(
"⚠️ %q ⚠️\n\n"+
"This executable was compiled without using Go modules or has \n"+
"zero dependencies. golicense considers this an error (exit code 1).\n", exePath)))
return 1
}

// From the raw module string from the binary, we need to parse this
// into structured data with the module information.
mods, err := module.ParseExeData(vsn.ModuleInfo)
if err != nil {
fmt.Fprintf(os.Stderr, color.RedString(fmt.Sprintf(
"❗️ Error parsing dependencies: %s\n", err)))
return 1
}
for _, mod := range mods {
allMods[mod] = struct{}{}
}
}

// From the raw module string from the binary, we need to parse this
// into structured data with the module information.
mods, err := module.ParseExeData(vsn.ModuleInfo)
if err != nil {
fmt.Fprintf(os.Stderr, color.RedString(fmt.Sprintf(
"❗️ Error parsing dependencies: %s\n", err)))
return 1
mods := make([]module.Module, 0, len(allMods))
for mod := range allMods {
mods = append(mods, mod)
}

// Complete terminal output setup
Expand Down

0 comments on commit 9a2abe2

Please sign in to comment.