Skip to content

Commit

Permalink
feat: folders without license headers (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo authored Sep 13, 2022
1 parent b40930c commit 02e9417
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
40 changes: 34 additions & 6 deletions cmd/dev/headers/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package headers
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"time"

goGitIgnore "github.com/sabhiram/go-gitignore"
Expand All @@ -23,8 +25,8 @@ const LICENSE_TOKEN = "Copyright ©"
// file types that we don't want to add license headers to
var noLicenseHeadersFor = []comments.FileType{"md", "yml", "yaml"}

// addLicenses adds or updates the Ory license header in all files within the given directory.
func AddLicenses(dir string, year int) error {
// AddLicenses adds or updates the Ory license header in all applicable files within the given directory.
func AddLicenses(dir string, year int, exclude []string) error {
licenseText := fmt.Sprintf(LICENSE_TEMPLATE, year)
gitIgnore, _ := goGitIgnore.CompileIgnoreFile(filepath.Join(dir, ".gitignore"))
return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
Expand All @@ -40,7 +42,10 @@ func AddLicenses(dir string, year int) error {
if !comments.SupportsFile(path) {
return nil
}
if !shouldAddLicense(path) {
if !fileTypeIsLicensed(path) {
return nil
}
if isInExcludedFolder(path, exclude) {
return nil
}
contentNoHeader, err := comments.FileContentWithoutHeader(path, LICENSE_TOKEN)
Expand All @@ -51,21 +56,44 @@ func AddLicenses(dir string, year int) error {
})
}

// isInExcludedFolder indicates whether the given path exists within the given list of folders
func isInExcludedFolder(path string, exclude []string) bool {
for _, e := range exclude {
if strings.HasPrefix(path, e) {
return true
}
}
return false
}

// indicates whether this tool is configured to add a license header to the file with the given path
func shouldAddLicense(path string) bool {
func fileTypeIsLicensed(path string) bool {
return !comments.ContainsFileType(noLicenseHeadersFor, comments.GetFileType(path))
}

var copyright = &cobra.Command{
Use: "license",
Short: "Adds the license header to all known files in the current directory",
Args: cobra.ExactArgs(1),
Long: `Adds the license header to all known files in the current directory.
Does not add the license header to git-ignored files.`,
RunE: func(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot determine the current directory: %w", err)
}
year, _, _ := time.Now().Date()
return AddLicenses(args[0], year)
for e, excluded := range exclude {
exclude[e] = filepath.Join(cwd, excluded)
}
return AddLicenses(cwd, year, exclude)
},
}

func init() {
Main.AddCommand(copyright)
copyright.Flags().StringSliceVarP(&exclude, "exclude", "e", []string{}, "folders to exclude, provide comma-separated values or multiple instances of this flag")
}

// contains the folders to exclude
var exclude []string
19 changes: 17 additions & 2 deletions cmd/dev/headers/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAddLicenses(t *testing.T) {
dir.CreateFile("vue.vue", "<template>\n<Header />")
dir.CreateFile("yaml.yml", "one: two\nalpha: beta")
dir.CreateFile("yaml.yaml", "one: two\nalpha: beta")
err := AddLicenses(dir.Path, 2022)
err := AddLicenses(dir.Path, 2022, []string{})
assert.NoError(t, err)
assert.Equal(t, "// Copyright © 2022 Ory Corp\n\nusing System;\n\nnamespace Foo.Bar {\n", dir.Content("c-sharp.cs"))
assert.Equal(t, "// Copyright © 2022 Ory Corp\n\nint a = 1;\nint b = 2;", dir.Content("dart.dart"))
Expand All @@ -45,6 +45,21 @@ func TestAddLicenses(t *testing.T) {
assert.Equal(t, "one: two\nalpha: beta", dir.Content("yaml.yaml"))
}

func TestIsExcluded(t *testing.T) {
exclude := []string{"internal/httpclient", "generated/"}
tests := map[string]bool{
"foo.md": false,
"foo/bar/baz.md": false,
"internal/httpclient/README.md": true,
"internal/httpclient/foo/bar/README.md": true,
"generated/README.md": true,
"generated/foo/bar/README.md": true,
}
for give, want := range tests {
assert.Equal(t, want, isInExcludedFolder(give, exclude), "%q -> %t", give, want)
}
}

func TestShouldAddLicense(t *testing.T) {
tests := map[string]bool{
"x.cs": true,
Expand All @@ -64,7 +79,7 @@ func TestShouldAddLicense(t *testing.T) {
}
for give, want := range tests {
t.Run(fmt.Sprintf("%s -> %t", give, want), func(t *testing.T) {
assert.Equal(t, want, shouldAddLicense(give))
assert.Equal(t, want, fileTypeIsLicensed(give))
})
}
}

0 comments on commit 02e9417

Please sign in to comment.