Skip to content

Commit

Permalink
feat(licensing): ✨ add licensing checks using go-license
Browse files Browse the repository at this point in the history
  • Loading branch information
sheldonhull committed Sep 17, 2021
1 parent 40e7d21 commit 82964d5
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
13 changes: 7 additions & 6 deletions .vscode/settings.json
@@ -1,8 +1,9 @@
{
"conventionalCommits.scopes": [
"gotools",
"ci",
"tooling"
],
"go.testTags": "mage testfiles"
"conventionalCommits.scopes": [
"gotools",
"ci",
"tooling",
"licensing"
],
"go.testTags": "mage testfiles"
}
67 changes: 67 additions & 0 deletions licensing/licensing.go
@@ -0,0 +1,67 @@
package licensing

// This set of tasks helps run Google's licensing tool to check for problem licenses, comply with credit, and also saving source when required.

import (
"github.com/magefile/mage/mg"
"github.com/pterm/pterm"
"github.com/sheldonhull/magetools/tooling"
)

type Licensing mg.Namespace

// licenseDir is the directory where the licenses are stored.
const licenseDir = "./licenses"

// golang tools to ensure are locally vendored.
var toolList = []string{ //nolint:gochecknoglobals // ok to be global for tooling setup
"github.com/google/go-licenses@master",
}

// ⚙️ Init runs all required steps to use this package.
func (Licensing) Init() error {
if err := tooling.InstallTools(toolList); err != nil {
return err
}

return nil
}

// Save checks the licenses of the files in the given repo and saves to a csv.
func (Licensing) Save() error {
pterm.Info.Println("Checks the licenses and persists to local directory")
c := []string{
"save", ".",
"--save_path",
licenseDir,
}

err := tooling.RunTool("go-licenses", c...)
if err != nil {
pterm.Error.Println(err)

return err
}

pterm.Success.Println("Check")

return nil
}

// Check look for forbidden licenses.
func (Licensing) Check() error {
pterm.Info.Println("look for forbidden licenses")
c := []string{
"check", ".",
}

err := tooling.RunTool("go-licenses", c...)
if err != nil {
pterm.Error.Println(err)

return err
}
pterm.Success.Println("Check")

return nil
}
36 changes: 36 additions & 0 deletions licensing/licensing_test.go
@@ -0,0 +1,36 @@
package licensing_test

import (
"os"
"testing"

iz "github.com/matryer/is"
"github.com/pterm/pterm"
"github.com/sheldonhull/magetools/licensing"
)

func TestInitAndSave(t *testing.T) {
pterm.DisableStyling()
is := iz.New(t)
var err error

defer func() {
err = os.RemoveAll("_tools")
is.NoErr(err) // Clean should not fail

err = os.RemoveAll("licenses")
is.NoErr(err) // Clean should not fail
}()

err = licensing.Licensing{}.Init()
is.NoErr(err) // should not error on Init

err = licensing.Licensing{}.Save()
is.NoErr(err) // should not error on Save

err = licensing.Licensing{}.Check()
is.NoErr(err) // should not error on Checking for forbidden licenses

_, err = os.Stat("licenses")
is.NoErr(err) // should not error on finding licenses.csv
}

0 comments on commit 82964d5

Please sign in to comment.