Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 16 additions & 16 deletions cmd/dev/headers/license.go → cmd/dev/headers/copyright.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Tool for adding a license header to all supported files.
// Tool for adding copyright headers to files.

package headers

Expand All @@ -21,18 +21,18 @@ import (
// HEADER_TEMPLATE defines the full header text.
const HEADER_TEMPLATE = "Copyright © %d Ory Corp\nSPDX-License-Identifier: Apache-2.0"

// HEADER_TOKEN defines the token that identifies comments containing the license.
// HEADER_TOKEN defines a text snippet to recognize an existing copyright header in a file.
const HEADER_TOKEN = "Copyright ©"

// file types that we don't want to add license headers to
// file types that we don't want to add copyright headers to
var noHeadersFor = []comments.FileType{"md", "yml", "yaml"}

// folders that are excluded by default
var defaultExcludedFolders = []string{"dist", "node_modules", "vendor"}

// AddHeaders adds or updates the Ory license header in all applicable files within the given directory.
// AddHeaders adds or updates the Ory copyright header in all applicable files within the given directory.
func AddHeaders(dir string, year int, exclude []string) error {
licenseText := fmt.Sprintf(HEADER_TEMPLATE, year)
headerText := fmt.Sprintf(HEADER_TEMPLATE, year)
gitIgnore, _ := ignore.CompileIgnoreFile(filepath.Join(dir, ".gitignore"))
prettierIgnore, _ := ignore.CompileIgnoreFile(filepath.Join(dir, ".prettierignore"))
return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
Expand All @@ -51,7 +51,7 @@ func AddHeaders(dir string, year int, exclude []string) error {
if !comments.SupportsFile(path) {
return nil
}
if !fileTypeIsLicensed(path) {
if !fileTypeNeedsCopyrightHeader(path) {
return nil
}
if isInFolders(path, defaultExcludedFolders) {
Expand All @@ -64,7 +64,7 @@ func AddHeaders(dir string, year int, exclude []string) error {
if err != nil {
return err
}
return comments.WriteFileWithHeader(path, licenseText, contentNoHeader)
return comments.WriteFileWithHeader(path, headerText, contentNoHeader)
})
}

Expand All @@ -78,26 +78,26 @@ func isInFolders(path string, exclude []string) bool {
return false
}

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

var license = &cobra.Command{
Use: "license",
Short: "Adds the license header to all known files in the current directory",
Long: `Adds the license header to all files that need one in the current directory.
var copyright = &cobra.Command{
Use: "copyright",
Short: "Adds the copyright header to all files in the current directory",
Long: `Adds the copyright header to all files that need one in the current directory.

Does not add the license header to files listed in .gitignore and .prettierignore.`,
Does not add the header to files listed in .gitignore and .prettierignore.`,
RunE: func(cmd *cobra.Command, args []string) error {
year, _, _ := time.Now().Date()
return AddHeaders(".", year, exclude)
},
}

func init() {
Main.AddCommand(license)
license.Flags().StringSliceVarP(&exclude, "exclude", "e", []string{}, "folders to exclude, provide comma-separated values or multiple instances of this flag")
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestAddLicenses(t *testing.T) {
func TestAddHeaders(t *testing.T) {
t.Parallel()
dir := CreateTmpDir()
dir.CreateFile(".gitignore", "git-ignored.go")
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestAddLicenses(t *testing.T) {
assert.Equal(t, "one: two\nalpha: beta", dir.Content("yaml.yaml"))
}

func TestIsExcluded(t *testing.T) {
func TestIsInFolders(t *testing.T) {
exclude := []string{"internal/httpclient", "generated/"}
tests := map[string]bool{
"foo.md": false,
Expand All @@ -63,7 +63,7 @@ func TestIsExcluded(t *testing.T) {
}
}

func TestShouldAddLicense(t *testing.T) {
func TestFileTypeNeedsCopyrightHeader(t *testing.T) {
tests := map[string]bool{
"x.cs": true,
"x.dart": true,
Expand All @@ -82,7 +82,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, fileTypeIsLicensed(give))
assert.Equal(t, want, fileTypeNeedsCopyrightHeader(give))
})
}
}
File renamed without changes.
File renamed without changes.