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

feature: support saving dependencies' licenses #69

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/spf13/cobra"

"github.com/apache/skywalking-eyes/license-eye/internal/logger"
"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 err
}
return nil
},
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
}
}