Skip to content

Commit

Permalink
feature: support saving dependencies' licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
kezhenxu94 committed Sep 10, 2021
1 parent 785bb7f commit 41155b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ INFO Totally checked 20 files, valid: 10, invalid: 10, ignored: 0, fixed: 10

This command serves as assistance for human beings to audit the dependencies license, it's exit code is always 0.
You can also use the `--output` or `-o` to save the dependencies' `LICENSE` files to a specified directory so that
you can put them in distribution package if needed.

```bash
bin/darwin/license-eye -c test/testdata/.licenserc_for_test_check.yaml dep resolve
bin/darwin/license-eye -c test/testdata/.licenserc_for_test_check.yaml dep resolve -o ./dependencies/licenses
INFO GITHUB_TOKEN is not set, license-eye won't comment on the pull request
INFO Loading configuration from file: test/testdata/.licenserc_for_test_check.yaml
WARNING Failed to resolve the license of dependency: gopkg.in/yaml.v3 cannot identify license content
Expand Down
43 changes: 43 additions & 0 deletions commands/deps_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,51 @@ package commands

import (
"fmt"
"os"
"regexp"
"strings"

"github.com/apache/skywalking-eyes/license-eye/internal/logger"
"github.com/spf13/cobra"

"github.com/apache/skywalking-eyes/license-eye/pkg/deps"
)

var outDir string

func init() {
DepsResolveCommand.PersistentFlags().StringVarP(&outDir, "output", "o", "", "the directory to output the resolved dependencies' licenses, if not set the dependencies' licenses won't be saved")
}

var fileNamePattern = regexp.MustCompile(`[^a-zA-Z0-9\\.\-]`)

var DepsResolveCommand = &cobra.Command{
Use: "resolve",
Aliases: []string{"r"},
Long: "resolves all dependencies of a module and their transitive dependencies",
PreRunE: func(cmd *cobra.Command, args []string) error {
if outDir == "" {
return nil
}
if err := os.MkdirAll(outDir, 0700); err == nil || os.IsExist(err) {
return nil
} else {
return err
}
},
RunE: func(cmd *cobra.Command, args []string) error {
report := deps.Report{}

if err := deps.Resolve(&Config.Deps, &report); err != nil {
return err
}

if outDir != "" {
for _, result := range report.Resolved {
writeLicense(result)
}
}

fmt.Println(report.String())

if skipped := len(report.Skipped); skipped > 0 {
Expand All @@ -53,3 +80,19 @@ var DepsResolveCommand = &cobra.Command{
return nil
},
}

func writeLicense(result *deps.Result) {
filename := string(fileNamePattern.ReplaceAll([]byte(result.Dependency), []byte("-")))
filename = strings.TrimRight(outDir, "/") + "/license-" + filename + ".txt"
file, err := os.Create(filename)
if err != nil {
logger.Log.Errorf("failed to create license file %v: %v", filename, err)
return
}
defer func(file *os.File) { _ = file.Close() }(file)
_, err = file.WriteString(result.LicenseContent)
if err != nil {
logger.Log.Errorf("failed to write license file, %v: %v", filename, err)
return
}
}

0 comments on commit 41155b3

Please sign in to comment.