This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch feature/save-licenses
in repository https://gitbox.apache.org/repos/asf/skywalking-eyes.git

commit 41155b34c1b36c2f1daebe8d29a054ec1316cff0
Author: kezhenxu94 <[email protected]>
AuthorDate: Fri Sep 10 23:54:02 2021 +0800

    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..9551fd0 100644
--- a/commands/deps_resolve.go
+++ b/commands/deps_resolve.go
@@ -19,17 +19,38 @@ 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{}
 
@@ -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
+       }
+}

Reply via email to