From 0c72046f44a23bdec119e6315b00a4bdbba82910 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Fri, 10 Sep 2021 23:54:02 +0800 Subject: [PATCH] feature: support saving dependencies' licenses --- README.md | 5 ++++- commands/deps_resolve.go | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f87130a..daeb20b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/commands/deps_resolve.go b/commands/deps_resolve.go index 4a03927..0a43a0a 100644 --- a/commands/deps_resolve.go +++ b/commands/deps_resolve.go @@ -19,17 +19,38 @@ 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{} @@ -37,6 +58,12 @@ var DepsResolveCommand = &cobra.Command{ return err } + if outDir != "" { + for _, result := range report.Resolved { + writeLicense(result) + } + } + fmt.Println(report.String()) if skipped := len(report.Skipped); skipped > 0 { @@ -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 + } +}