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

dependency resolve with default template and specified output of license #163

Merged
merged 6 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ To check dependencies license in GitHub Actions, add a step in your GitHub workf
# log: debug # optional: set the log level. The default value is `info`.
# config: .licenserc.yaml # optional: set the config file. The default value is `.licenserc.yaml`.
# mode: # optional: Which mode License-Eye should be run in. Choices are `check` or `resolve`. The default value is `check`.
# flags: # optional: Extra flags appended to the command, for example, `--summary=path/to/template.tmpl`
```

### Docker Image
Expand Down Expand Up @@ -189,10 +190,11 @@ This command assists human audits of the dependencies licenses. It's exit code i

It supports two flags:

|Flag name|Short name|Description|
|---------|----------|-----------|
|`--output`|`-o`|Save the dependencies' `LICENSE` files to a specified directory so that you can put them in distribution package if needed.|
|`--summary`|`-s`|Based on the template, aggregate all dependency information and generate a `LICENSE` file.|
| Flag name | Short name | Description |
|-------------|------------|----------------------------------------------------------------------------------------------------------------------------------------|
| `--output` | `-o` | Save the dependencies' `LICENSE` files to a specified directory so that you can put them in distribution package if needed. |
| `--summary` | `-s` | Based on the template, aggregate all dependency information and generate a `LICENSE` file. |
| `--license` | `-l` | The output path to the LICENSE file to be generated. The default summary format will be used if summary template file is not specified |

```bash
license-eye -c test/testdata/.licenserc_for_test_check.yaml dep resolve -o ./dependencies/licenses -s LICENSE.tpl
Expand Down
4 changes: 4 additions & 0 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
//go:embed *
var assets embed.FS

func FS() fs.FS {
return assets
}

func Asset(file string) ([]byte, error) {
return assets.ReadFile(filepath.ToSlash(file))
}
Expand Down
9 changes: 9 additions & 0 deletions assets/default-license.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{.LicenseContent }}
{{ range .Groups }}
========================================================================
{{.LicenseID}} licenses
========================================================================
{{range .Deps}}
{{.Name}} {{.Version}} {{.LicenseID}}
{{- end }}
{{ end }}
34 changes: 30 additions & 4 deletions commands/deps_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package commands

import (
"fmt"
"github.com/apache/skywalking-eyes/assets"
"os"
"path/filepath"
"regexp"
Expand All @@ -32,6 +33,7 @@ import (
)

var outDir string
var licensePath string
var summaryTplPath string
var summaryTpl *template.Template

Expand All @@ -41,6 +43,8 @@ func init() {
DepsResolveCommand.PersistentFlags().StringVarP(&summaryTplPath, "summary", "s", "",
"the template file to write the summary of dependencies' licenses, a new file named \"LICENSE\" will be "+
"created in the same directory as the template file, to save the final summary.")
DepsResolveCommand.PersistentFlags().StringVarP(&licensePath, "license", "l", "",
"the path to the LICENSE file to be generated. The default summary format will be used if summary template file is not specified")
}

var fileNamePattern = regexp.MustCompile(`[^a-zA-Z0-9\\.\-]`)
Expand All @@ -66,12 +70,30 @@ var DepsResolveCommand = &cobra.Command{
return err
}
summaryTplPath = absPath
tpl, err := deps.ParseTemplate(summaryTplPath)
tpl, err := deps.ParseTemplate(os.DirFS(filepath.Dir(absPath)), filepath.Base(absPath))
if err != nil {
return err
}
summaryTpl = tpl
}
if licensePath != "" {
absPath, err := filepath.Abs(licensePath)
if err != nil {
return err
}
licensePath = absPath
if err := os.MkdirAll(filepath.Dir(outDir), 0o700); err != nil && !os.IsExist(err) {
return err
}

if summaryTpl == nil {
tpl, err := deps.ParseTemplate(assets.FS(), "default-license.tpl")
if err != nil {
return err
}
summaryTpl = tpl
}
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -83,7 +105,7 @@ var DepsResolveCommand = &cobra.Command{
}

if summaryTpl != nil {
if err := writeSummary(&report); err != nil {
if err := writeSummary(&report, licensePath); err != nil {
return err
}
}
Expand Down Expand Up @@ -131,8 +153,12 @@ func writeLicense(result *deps.Result) {
}
}

func writeSummary(rep *deps.Report) error {
file, err := os.Create(filepath.Join(filepath.Dir(summaryTplPath), "LICENSE"))
func writeSummary(rep *deps.Report, path string) error {

if path == "" {
path = filepath.Join(filepath.Dir(summaryTplPath), "LICENSE")
}
file, err := os.Create(path)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion dependency/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ inputs:
default value is `check`.
required: false
default: check
flags:
description: |
Extra flags appended to the command, for example, --summary=path/to/template.tmpl
required: false
runs:
using: "composite"
steps:
Expand All @@ -48,4 +52,4 @@ runs:
- shell: bash
env:
GITHUB_TOKEN: ${{ inputs.token }}
run: license-eye -v ${{ inputs.log }} -c ${{ inputs.config }} dependency ${{ inputs.mode }}
run: license-eye -v ${{ inputs.log }} -c ${{ inputs.config }} dependency ${{ inputs.mode }} ${{ inputs.flags }}
6 changes: 3 additions & 3 deletions pkg/deps/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package deps

import (
"bytes"
"os"
"io/fs"
"sort"
"text/template"

Expand All @@ -45,8 +45,8 @@ type SummaryRenderLicense struct {
LicenseID string // License ID
}

func ParseTemplate(path string) (*template.Template, error) {
tpl, err := os.ReadFile(path)
func ParseTemplate(f fs.FS, path string) (*template.Template, error) {
tpl, err := fs.ReadFile(f, path)
if err != nil {
return nil, err
}
Expand Down