Skip to content

Commit 82964d5

Browse files
committed
feat(licensing): ✨ add licensing checks using go-license
1 parent 40e7d21 commit 82964d5

File tree

3 files changed

+110
-6
lines changed

3 files changed

+110
-6
lines changed

.vscode/settings.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"conventionalCommits.scopes": [
3-
"gotools",
4-
"ci",
5-
"tooling"
6-
],
7-
"go.testTags": "mage testfiles"
2+
"conventionalCommits.scopes": [
3+
"gotools",
4+
"ci",
5+
"tooling",
6+
"licensing"
7+
],
8+
"go.testTags": "mage testfiles"
89
}

licensing/licensing.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package licensing
2+
3+
// This set of tasks helps run Google's licensing tool to check for problem licenses, comply with credit, and also saving source when required.
4+
5+
import (
6+
"github.com/magefile/mage/mg"
7+
"github.com/pterm/pterm"
8+
"github.com/sheldonhull/magetools/tooling"
9+
)
10+
11+
type Licensing mg.Namespace
12+
13+
// licenseDir is the directory where the licenses are stored.
14+
const licenseDir = "./licenses"
15+
16+
// golang tools to ensure are locally vendored.
17+
var toolList = []string{ //nolint:gochecknoglobals // ok to be global for tooling setup
18+
"github.com/google/go-licenses@master",
19+
}
20+
21+
// ⚙️ Init runs all required steps to use this package.
22+
func (Licensing) Init() error {
23+
if err := tooling.InstallTools(toolList); err != nil {
24+
return err
25+
}
26+
27+
return nil
28+
}
29+
30+
// Save checks the licenses of the files in the given repo and saves to a csv.
31+
func (Licensing) Save() error {
32+
pterm.Info.Println("Checks the licenses and persists to local directory")
33+
c := []string{
34+
"save", ".",
35+
"--save_path",
36+
licenseDir,
37+
}
38+
39+
err := tooling.RunTool("go-licenses", c...)
40+
if err != nil {
41+
pterm.Error.Println(err)
42+
43+
return err
44+
}
45+
46+
pterm.Success.Println("Check")
47+
48+
return nil
49+
}
50+
51+
// Check look for forbidden licenses.
52+
func (Licensing) Check() error {
53+
pterm.Info.Println("look for forbidden licenses")
54+
c := []string{
55+
"check", ".",
56+
}
57+
58+
err := tooling.RunTool("go-licenses", c...)
59+
if err != nil {
60+
pterm.Error.Println(err)
61+
62+
return err
63+
}
64+
pterm.Success.Println("Check")
65+
66+
return nil
67+
}

licensing/licensing_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package licensing_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
iz "github.com/matryer/is"
8+
"github.com/pterm/pterm"
9+
"github.com/sheldonhull/magetools/licensing"
10+
)
11+
12+
func TestInitAndSave(t *testing.T) {
13+
pterm.DisableStyling()
14+
is := iz.New(t)
15+
var err error
16+
17+
defer func() {
18+
err = os.RemoveAll("_tools")
19+
is.NoErr(err) // Clean should not fail
20+
21+
err = os.RemoveAll("licenses")
22+
is.NoErr(err) // Clean should not fail
23+
}()
24+
25+
err = licensing.Licensing{}.Init()
26+
is.NoErr(err) // should not error on Init
27+
28+
err = licensing.Licensing{}.Save()
29+
is.NoErr(err) // should not error on Save
30+
31+
err = licensing.Licensing{}.Check()
32+
is.NoErr(err) // should not error on Checking for forbidden licenses
33+
34+
_, err = os.Stat("licenses")
35+
is.NoErr(err) // should not error on finding licenses.csv
36+
}

0 commit comments

Comments
 (0)