You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@skywalking.apache.org by ke...@apache.org on 2020/12/21 07:57:58 UTC
[skywalking-eyes] 24/25: Refactor, set version, module, asf.yaml,
etc.
This is an automated email from the ASF dual-hosted git repository.
kezhenxu94 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-eyes.git
commit 56a6344291bb3b784b016a0b0ec4526c88319a19
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Mon Dec 21 15:33:52 2020 +0800
Refactor, set version, module, asf.yaml, etc.
---
Dockerfile => .asf.yaml | 22 ++---
.github/workflows/build.yaml | 10 ++-
.golangci.yml | 96 ++++++++++++++++++++++
.licenserc.yaml | 52 ++++++------
Dockerfile | 6 +-
Makefile | 51 ++++++------
NOTICE | 5 ++
README.adoc | 35 ++++++--
main.go => cmd/license-eye/main.go | 7 +-
commands/header/check.go | 12 +--
commands/header/fix.go | 19 +++--
{cmd => commands}/root.go | 28 ++++---
.../testcase.go => commands/version.go | 4 +-
go.mod | 2 +-
.../fix/double_slash.go => config/Config.go} | 34 ++++----
pkg/header/check.go | 22 +++--
pkg/header/config.go | 33 ++++++--
pkg/header/fix/angle_bracket.go | 5 +-
pkg/header/fix/double_slash.go | 5 +-
pkg/header/fix/fix.go | 12 +--
pkg/header/fix/hashtag.go | 5 +-
pkg/header/fix/slash_asterisk.go | 5 +-
pkg/header/result.go | 2 +-
test/.licenserc_for_test_check.yaml | 43 +++++-----
test/.licenserc_for_test_fix.yaml | 41 ++++-----
test/include_test/with_license/testcase.go | 2 +-
test/include_test/without_license/testcase.go | 2 +-
27 files changed, 360 insertions(+), 200 deletions(-)
diff --git a/Dockerfile b/.asf.yaml
similarity index 72%
copy from Dockerfile
copy to .asf.yaml
index a466f44..8fe2357 100644
--- a/Dockerfile
+++ b/.asf.yaml
@@ -15,18 +15,12 @@
# specific language governing permissions and limitations
# under the License.
#
-FROM golang:1.14.3-alpine AS build
-WORKDIR /src
-
-COPY . .
-
-RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /bin/license-checker
-
-FROM alpine:3 AS bin
-
-COPY --from=build /bin/license-checker /bin/license-checker
-
-WORKDIR /github/workspace/
-
-ENTRYPOINT /bin/license-checker header check -v debug
+github:
+ description: Apache SkyWalking Eyes
+ homepage: https://skywalking.apache.org/
+ labels:
+ - cli
+ - tools
+ - license
+ - licensing
diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml
index 1fa632a..68fcaf2 100644
--- a/.github/workflows/build.yaml
+++ b/.github/workflows/build.yaml
@@ -14,7 +14,8 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-#
+#
+
name: Build
on:
@@ -28,13 +29,16 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
+ - uses: actions/checkout@v2
+
- uses: actions/setup-go@v2
with:
go-version: 1.14
- - uses: actions/checkout@v2
+ - name: Lint Codes
+ run: make lint
- - name: Selfcheck License
+ - name: License Check
run: make license
- name: Build
diff --git a/.golangci.yml b/.golangci.yml
new file mode 100644
index 0000000..ca0227f
--- /dev/null
+++ b/.golangci.yml
@@ -0,0 +1,96 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you under
+# the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+linters-settings:
+ govet:
+ check-shadowing: true
+ golint:
+ min-confidence: 0
+ gocyclo:
+ min-complexity: 15
+ maligned:
+ suggest-new: true
+ dupl:
+ threshold: 200
+ goconst:
+ min-len: 2
+ min-occurrences: 2
+ depguard:
+ list-type: blacklist
+ include-go-root: true
+ packages-with-error-messages:
+ fmt: "logging is allowed only by logutils.Log"
+ misspell:
+ locale: US
+ lll:
+ line-length: 150
+ goimports:
+ local-prefixes: github.com/apache/skywalking-cli
+ gocritic:
+ enabled-tags:
+ - diagnostic
+ - experimental
+ - opinionated
+ - performance
+ - style
+ disabled-checks:
+ - ifElseChain
+ funlen:
+ lines: 100
+ statements: 50
+ whitespace:
+ multi-if: false
+ multi-func: false
+
+linters:
+ enable:
+ - bodyclose
+ - deadcode
+ - depguard
+ - dogsled
+ - dupl
+ - errcheck
+ - funlen
+ - goconst
+ - gocritic
+ - gocyclo
+ - gofmt
+ - goimports
+ - golint
+ - gosec
+ - gosimple
+ - govet
+ - ineffassign
+ - interfacer
+ - lll
+ - misspell
+ - nakedret
+ - staticcheck
+ - structcheck
+ - stylecheck
+ - typecheck
+ - unconvert
+ - unparam
+ - unused
+ - varcheck
+ - whitespace
+
+service:
+ golangci-lint-version: 1.20.x
+ prepare:
+ - echo "here I can run custom commands, but no preparation needed for this repo"
diff --git a/.licenserc.yaml b/.licenserc.yaml
index b8366ec..6aa224f 100644
--- a/.licenserc.yaml
+++ b/.licenserc.yaml
@@ -1,28 +1,30 @@
-license: |
- Licensed to Apache Software Foundation (ASF) under one or more contributor
- license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright
- ownership. Apache Software Foundation (ASF) licenses this file to you under
- the Apache License, Version 2.0 (the "License"); you may
- not use this file except in compliance with the License.
- You may obtain a copy of the License at
+header:
+ license: |
+ Licensed to Apache Software Foundation (ASF) under one or more contributor
+ license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright
+ ownership. Apache Software Foundation (ASF) licenses this file to you under
+ the Apache License, Version 2.0 (the "License"); you may
+ not use this file except in compliance with the License.
+ You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
+ http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
-paths-ignore:
- - '.git/**'
- - '.idea/**'
- - 'bin/**'
- - '**/*.md'
- - '**/.DS_Store'
- - 'test/**'
- - 'go.mod'
- - 'go.sum'
- - 'LICENSE'
+ paths-ignore:
+ - '.git/**'
+ - '.idea/**'
+ - 'bin/**'
+ - '**/*.md'
+ - '**/.DS_Store'
+ - 'test/**'
+ - 'go.mod'
+ - 'go.sum'
+ - 'LICENSE'
+ - 'NOTICE'
diff --git a/Dockerfile b/Dockerfile
index a466f44..62c66ee 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -21,12 +21,12 @@ WORKDIR /src
COPY . .
-RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /bin/license-checker
+RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /bin/license-eye
FROM alpine:3 AS bin
-COPY --from=build /bin/license-checker /bin/license-checker
+COPY --from=build /bin/license-eye /bin/license-eye
WORKDIR /github/workspace/
-ENTRYPOINT /bin/license-checker header check -v debug
+ENTRYPOINT /bin/license-eye header check -v debug
diff --git a/Makefile b/Makefile
index e2a933a..3d1612e 100644
--- a/Makefile
+++ b/Makefile
@@ -14,13 +14,16 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-#
-PROJECT = license-checker
+#
+
+PROJECT = license-eye
VERSION ?= latest
OUT_DIR = bin
CURDIR := $(shell pwd)
FILES := $$(find .$$($(PACKAGE_DIRECTORIES)) -name "*.go")
FAIL_ON_STDOUT := awk '{ print } END { if (NR > 0) { exit 1 } }'
+ARCH := $(shell uname)
+OSNAME := $(if $(findstring Darwin,$(ARCH)),darwin,linux)
GO := GO111MODULE=on go
GO_PATH = $(shell $(GO) env GOPATH)
@@ -28,38 +31,40 @@ GO_BUILD = $(GO) build
GO_GET = $(GO) get
GO_TEST = $(GO) test
GO_LINT = $(GO_PATH)/bin/golangci-lint
+GO_BUILD_LDFLAGS = -X github.com/apache/skywalking-eyes/license-eye/commands.version=$(VERSION)
-all: clean deps lint test build
+PLATFORMS := windows linux darwin
+os = $(word 1, $@)
+ARCH = amd64
-tools:
- mkdir -p $(GO_PATH)/bin
-
-deps: tools
- $(GO_GET) -v -t -d ./...
+all: clean lint license test build
.PHONY: lint
-lint: tools
- $(GO_LINT) version || curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GO_PATH)/bin v1.21.0
- @gofmt -s -l -w $(FILES) 2>&1 | $(FAIL_ON_STDOUT)
+lint:
+ $(GO_LINT) version || curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GO_PATH)/bin
$(GO_LINT) run -v ./...
-.PHONE: test
+.PHONY: fix-lint
+fix-lint:
+ $(GO_LINT) run -v --fix ./...
+
+.PHONY: license
+license: clean
+ $(GO) run cmd/license-eye/main.go header check
+
+.PHONY: test
test: clean lint
$(GO_TEST) ./...
@>&2 echo "Great, all tests passed."
-.PHONY: build
-build: deps
- $(GO_BUILD) -o $(OUT_DIR)/$(PROJECT)
+.PHONY: $(PLATFORMS)
+$(PLATFORMS):
+ mkdir -p $(OUT_DIR)
+ GOOS=$(os) GOARCH=$(ARCH) $(GO_BUILD) $(GO_BUILD_FLAGS) -ldflags "$(GO_BUILD_LDFLAGS)" -o $(OUT_DIR)/$(os)/$(PROJECT) cmd/license-eye/main.go
-.PHONY: license
-license: clean
- $(GO) run main.go header check
-
-.PHONY: fix
-fix: tools
- $(GO_LINT) run -v --fix ./...
+.PHONY: build
+build: windows linux darwin
.PHONY: clean
-clean: tools
+clean:
-rm -rf bin
diff --git a/NOTICE b/NOTICE
new file mode 100644
index 0000000..e646f51
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,5 @@
+Apache SkyWalking
+Copyright 2017-2020 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
diff --git a/README.adoc b/README.adoc
index 3174252..17501c0 100644
--- a/README.adoc
+++ b/README.adoc
@@ -1,5 +1,22 @@
-= license-checker
-:repo: https://github.com/fgksgf/license-checker
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+= license-eye
+:repo: https://github.com/apache/skywalking-eyes
A full-featured license guard to check and fix license headers and dependencies' licenses.
@@ -8,7 +25,7 @@ A full-featured license guard to check and fix license headers and dependencies'
[subs="attributes+",source,bash]
----
git clone {repo}
-cd license-checker
+cd license-eye
make
----
@@ -16,22 +33,22 @@ make
[source]
----
-$ license-checker
+$ license-eye
A full-featured license guard to check and fix license headers and dependencies' licenses.
Usage:
- license-checker [command]
+ license-eye [command]
Available Commands:
header License header related commands; e.g. check, fix, etc.
help Help about any command
Flags:
- -h, --help help for license-checker
+ -h, --help help for license-eye
-v, --verbosity string log level (debug, info, warn, error, fatal, panic (default "info")
-Use "license-checker [command] --help" for more information about a command.
+Use "license-eye [command] --help" for more information about a command.
----
== Configuration
@@ -46,7 +63,7 @@ include::test/.licenserc_for_test_check.yaml[]
[source]
----
-bin/license-checker -c test/.licenserc_for_test_fix.yaml header check
+bin/license-eye -c test/.licenserc_for_test_fix.yaml header check
INFO Loading configuration from file: test/.licenserc_for_test.yaml serc_for_test.yaml
INFO Totally checked 23 files, valid: 8, invalid: 8, ignored: 7, fixed: 0
@@ -66,7 +83,7 @@ exit status 1
[source]
----
-bin/license-checker -c test/.licenserc_for_test_fix.yaml header fix
+bin/license-eye -c test/.licenserc_for_test_fix.yaml header fix
INFO Loading configuration from file: test/.licenserc_for_test_fix.yaml
INFO Totally checked 16 files, valid: 7, invalid: 8, ignored: 1, fixed: 8
diff --git a/main.go b/cmd/license-eye/main.go
similarity index 84%
rename from main.go
rename to cmd/license-eye/main.go
index b2f1453..4059df5 100644
--- a/main.go
+++ b/cmd/license-eye/main.go
@@ -18,13 +18,14 @@
package main
import (
- "license-checker/cmd"
- "license-checker/internal/logger"
"os"
+
+ "github.com/apache/skywalking-eyes/license-eye/commands"
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
)
func main() {
- if err := cmd.Execute(); err != nil {
+ if err := commands.Execute(); err != nil {
logger.Log.Errorln(err)
os.Exit(1)
}
diff --git a/commands/header/check.go b/commands/header/check.go
index b20642f..1de9115 100644
--- a/commands/header/check.go
+++ b/commands/header/check.go
@@ -18,24 +18,26 @@
package header
import (
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
+ "github.com/apache/skywalking-eyes/license-eye/pkg/config"
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header"
+
"github.com/spf13/cobra"
- "license-checker/internal/logger"
- "license-checker/pkg/header"
)
var CheckCommand = &cobra.Command{
Use: "check",
Aliases: []string{"c"},
- Long: "`check` command walks the specified paths recursively and checks if the specified files have the license header in the config file.",
+ Long: "check command walks the specified paths recursively and checks if the specified files have the license header in the config file.",
RunE: func(cmd *cobra.Command, args []string) error {
- var config header.Config
+ var config config.Config
var result header.Result
if err := config.Parse(cfgFile); err != nil {
return err
}
- if err := header.Check(&config, &result); err != nil {
+ if err := header.Check(&config.Header, &result); err != nil {
return err
}
diff --git a/commands/header/fix.go b/commands/header/fix.go
index 624a1f0..0bc6047 100644
--- a/commands/header/fix.go
+++ b/commands/header/fix.go
@@ -19,32 +19,35 @@ package header
import (
"fmt"
- "github.com/spf13/cobra"
- "license-checker/internal/logger"
- "license-checker/pkg/header"
- "license-checker/pkg/header/fix"
"strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
+ "github.com/apache/skywalking-eyes/license-eye/pkg/config"
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header"
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header/fix"
+
+ "github.com/spf13/cobra"
)
var FixCommand = &cobra.Command{
Use: "fix",
Aliases: []string{"f"},
- Long: "`fix` command walks the specified paths recursively and fix the license header if the specified files don't have the license header in the config file.",
+ Long: "fix command walks the specified paths recursively and fix the license header if the specified files don't have the license header.",
RunE: func(cmd *cobra.Command, args []string) error {
- var config header.Config
+ var config config.Config
var result header.Result
if err := config.Parse(cfgFile); err != nil {
return err
}
- if err := header.Check(&config, &result); err != nil {
+ if err := header.Check(&config.Header, &result); err != nil {
return err
}
var errors []string
for _, file := range result.Failure {
- if err := fix.Fix(file, &config, &result); err != nil {
+ if err := fix.Fix(file, &config.Header, &result); err != nil {
errors = append(errors, err.Error())
}
}
diff --git a/cmd/root.go b/commands/root.go
similarity index 67%
rename from cmd/root.go
rename to commands/root.go
index 91cb6b6..ab16bf8 100644
--- a/cmd/root.go
+++ b/commands/root.go
@@ -15,41 +15,43 @@
// specific language governing permissions and limitations
// under the License.
//
-package cmd
+package commands
import (
+ headercommand "github.com/apache/skywalking-eyes/license-eye/commands/header"
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
+
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
- headercommand "license-checker/commands/header"
- "license-checker/internal/logger"
)
var (
verbosity string
)
-// rootCmd represents the base command when called without any subcommands
-var rootCmd = &cobra.Command{
- Use: "license-checker command [flags]",
+// Root represents the base command when called without any subcommands
+var Root = &cobra.Command{
+ Use: "license-eye command [flags]",
Long: "A full-featured license guard to check and fix license headers and dependencies' licenses",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
- if level, err := logrus.ParseLevel(verbosity); err != nil {
+ level, err := logrus.ParseLevel(verbosity)
+ if err != nil {
return err
- } else {
- logger.Log.SetLevel(level)
}
+ logger.Log.SetLevel(level)
return nil
},
+ Version: version,
}
// Execute sets flags to the root command appropriately.
-// This is called by main.main(). It only needs to happen once to the rootCmd.
+// This is called by main.main(). It only needs to happen once to the Root.
func Execute() error {
- rootCmd.PersistentFlags().StringVarP(&verbosity, "verbosity", "v", logrus.InfoLevel.String(), "log level (debug, info, warn, error, fatal, panic")
+ Root.PersistentFlags().StringVarP(&verbosity, "verbosity", "v", logrus.InfoLevel.String(), "log level (debug, info, warn, error, fatal, panic")
- rootCmd.AddCommand(headercommand.Header)
+ Root.AddCommand(headercommand.Header)
- return rootCmd.Execute()
+ return Root.Execute()
}
diff --git a/test/include_test/with_license/testcase.go b/commands/version.go
similarity index 95%
copy from test/include_test/with_license/testcase.go
copy to commands/version.go
index 0f54b96..0b07316 100644
--- a/test/include_test/with_license/testcase.go
+++ b/commands/version.go
@@ -15,4 +15,6 @@
// specific language governing permissions and limitations
// under the License.
//
-package with_license
+package commands
+
+var version string
diff --git a/go.mod b/go.mod
index 1ac37cf..bba41da 100644
--- a/go.mod
+++ b/go.mod
@@ -1,4 +1,4 @@
-module license-checker
+module github.com/apache/skywalking-eyes/license-eye
go 1.13
diff --git a/pkg/header/fix/double_slash.go b/pkg/config/Config.go
similarity index 61%
copy from pkg/header/fix/double_slash.go
copy to pkg/config/Config.go
index ab2819a..3b7c417 100644
--- a/pkg/header/fix/double_slash.go
+++ b/pkg/config/Config.go
@@ -15,34 +15,34 @@
// specific language governing permissions and limitations
// under the License.
//
-package fix
+package config
import (
"io/ioutil"
- "license-checker/pkg/header"
- "os"
- "strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header"
+
+ "gopkg.in/yaml.v3"
)
-// DoubleSlash adds the configured license header to files whose comment starts with //.
-func DoubleSlash(file string, config *header.Config, result *header.Result) error {
- stat, err := os.Stat(file)
- if err != nil {
- return err
- }
+type Config struct {
+ Header header.ConfigHeader `yaml:"header"`
+}
- content, err := ioutil.ReadFile(file)
- if err != nil {
+// Parse reads and parses the header check configurations in config file.
+func (config *Config) Parse(file string) error {
+ logger.Log.Infoln("Loading configuration from file:", file)
+
+ if bytes, err := ioutil.ReadFile(file); err != nil {
+ return err
+ } else if err := yaml.Unmarshal(bytes, config); err != nil {
return err
}
- lines := "// " + strings.Join(strings.Split(config.License, "\n"), "\n// ") + "\n"
-
- if err := ioutil.WriteFile(file, append([]byte(lines), content...), stat.Mode()); err != nil {
+ if err := config.Header.Finalize(); err != nil {
return err
}
- result.Fix(file)
-
return nil
}
diff --git a/pkg/header/check.go b/pkg/header/check.go
index 31eb6c7..e851b59 100644
--- a/pkg/header/check.go
+++ b/pkg/header/check.go
@@ -19,17 +19,20 @@ package header
import (
"bufio"
- "github.com/bmatcuk/doublestar/v2"
- "license-checker/internal/logger"
"os"
"path/filepath"
+ "regexp"
"strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
+
+ "github.com/bmatcuk/doublestar/v2"
)
-const CommentChars = "/*#- !~"
+const CommentChars = "/*#- !~'\""
// Check checks the license headers of the specified paths/globs.
-func Check(config *Config, result *Result) error {
+func Check(config *ConfigHeader, result *Result) error {
for _, pattern := range config.Paths {
if err := checkPattern(pattern, result, config); err != nil {
return err
@@ -41,7 +44,7 @@ func Check(config *Config, result *Result) error {
var seen = make(map[string]bool)
-func checkPattern(pattern string, result *Result, config *Config) error {
+func checkPattern(pattern string, result *Result, config *ConfigHeader) error {
paths, err := doublestar.Glob(pattern)
if err != nil {
@@ -53,7 +56,7 @@ func checkPattern(pattern string, result *Result, config *Config) error {
result.Ignore(path)
continue
}
- if err = checkPath(path, result, config); err != nil {
+ if err := checkPath(path, result, config); err != nil {
return err
}
seen[path] = true
@@ -62,7 +65,7 @@ func checkPattern(pattern string, result *Result, config *Config) error {
return nil
}
-func checkPath(path string, result *Result, config *Config) error {
+func checkPath(path string, result *Result, config *ConfigHeader) error {
defer func() { seen[path] = true }()
if yes, err := config.ShouldIgnore(path); yes || seen[path] || err != nil {
@@ -95,7 +98,7 @@ func checkPath(path string, result *Result, config *Config) error {
}
// CheckFile checks whether or not the file contains the configured license header.
-func CheckFile(file string, config *Config, result *Result) error {
+func CheckFile(file string, config *ConfigHeader, result *Result) error {
if yes, err := config.ShouldIgnore(file); yes || err != nil {
if !seen[file] {
result.Ignore(file)
@@ -115,7 +118,8 @@ func CheckFile(file string, config *Config, result *Result) error {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
- line := strings.Trim(scanner.Text(), CommentChars)
+ line := strings.ToLower(strings.Trim(scanner.Text(), CommentChars))
+ line = regexp.MustCompile(" +").ReplaceAllString(line, " ")
if len(line) > 0 {
lines = append(lines, line)
}
diff --git a/pkg/header/config.go b/pkg/header/config.go
index f3629d1..6714fca 100644
--- a/pkg/header/config.go
+++ b/pkg/header/config.go
@@ -18,14 +18,17 @@
package header
import (
- "github.com/bmatcuk/doublestar/v2"
- "gopkg.in/yaml.v3"
"io/ioutil"
- "license-checker/internal/logger"
+ "regexp"
"strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
+
+ "github.com/bmatcuk/doublestar/v2"
+ "gopkg.in/yaml.v3"
)
-type Config struct {
+type ConfigHeader struct {
License string `yaml:"license"`
Paths []string `yaml:"paths"`
PathsIgnore []string `yaml:"paths-ignore"`
@@ -33,23 +36,25 @@ type Config struct {
// NormalizedLicense returns the normalized string of the license content,
// "normalized" means the linebreaks and CommentChars are all trimmed.
-func (config *Config) NormalizedLicense() string {
+func (config *ConfigHeader) NormalizedLicense() string {
var lines []string
for _, line := range strings.Split(config.License, "\n") {
if len(line) > 0 {
- lines = append(lines, strings.Trim(line, CommentChars))
+ line = strings.ToLower(strings.Trim(line, CommentChars))
+ line = regexp.MustCompile(" +").ReplaceAllString(line, " ")
+ lines = append(lines, line)
}
}
return strings.Join(lines, " ")
}
// Parse reads and parses the header check configurations in config file.
-func (config *Config) Parse(file string) error {
+func (config *ConfigHeader) Parse(file string) error {
logger.Log.Infoln("Loading configuration from file:", file)
if bytes, err := ioutil.ReadFile(file); err != nil {
return err
- } else if err = yaml.Unmarshal(bytes, config); err != nil {
+ } else if err := yaml.Unmarshal(bytes, config); err != nil {
return err
}
@@ -62,7 +67,7 @@ func (config *Config) Parse(file string) error {
return nil
}
-func (config *Config) ShouldIgnore(path string) (bool, error) {
+func (config *ConfigHeader) ShouldIgnore(path string) (bool, error) {
for _, ignorePattern := range config.PathsIgnore {
if matched, err := doublestar.Match(ignorePattern, path); matched || err != nil {
return matched, err
@@ -70,3 +75,13 @@ func (config *Config) ShouldIgnore(path string) (bool, error) {
}
return false, nil
}
+
+func (config *ConfigHeader) Finalize() error {
+ logger.Log.Debugln("License header is:", config.NormalizedLicense())
+
+ if len(config.Paths) == 0 {
+ config.Paths = []string{"**"}
+ }
+
+ return nil
+}
diff --git a/pkg/header/fix/angle_bracket.go b/pkg/header/fix/angle_bracket.go
index ac31302..1b11bb1 100644
--- a/pkg/header/fix/angle_bracket.go
+++ b/pkg/header/fix/angle_bracket.go
@@ -20,14 +20,15 @@ package fix
import (
"fmt"
"io/ioutil"
- "license-checker/pkg/header"
"os"
"reflect"
"strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header"
)
// AngleBracket adds the configured license header to files whose comment starts with <!--.
-func AngleBracket(file string, config *header.Config, result *header.Result) error {
+func AngleBracket(file string, config *header.ConfigHeader, result *header.Result) error {
stat, err := os.Stat(file)
if err != nil {
return err
diff --git a/pkg/header/fix/double_slash.go b/pkg/header/fix/double_slash.go
index ab2819a..2c7d634 100644
--- a/pkg/header/fix/double_slash.go
+++ b/pkg/header/fix/double_slash.go
@@ -19,13 +19,14 @@ package fix
import (
"io/ioutil"
- "license-checker/pkg/header"
"os"
"strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header"
)
// DoubleSlash adds the configured license header to files whose comment starts with //.
-func DoubleSlash(file string, config *header.Config, result *header.Result) error {
+func DoubleSlash(file string, config *header.ConfigHeader, result *header.Result) error {
stat, err := os.Stat(file)
if err != nil {
return err
diff --git a/pkg/header/fix/fix.go b/pkg/header/fix/fix.go
index bba4df0..579969b 100644
--- a/pkg/header/fix/fix.go
+++ b/pkg/header/fix/fix.go
@@ -19,13 +19,15 @@ package fix
import (
"fmt"
- "license-checker/internal/logger"
- "license-checker/pkg/header"
"strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header"
)
-var suffixToFunc = map[string]func(string, *header.Config, *header.Result) error{
- ".go": DoubleSlash,
+var suffixToFunc = map[string]func(string, *header.ConfigHeader, *header.Result) error{
+ ".go": DoubleSlash,
+ ".adoc": DoubleSlash,
".py": Hashtag, // TODO: tackle shebang
".sh": Hashtag, // TODO: tackle shebang
@@ -42,7 +44,7 @@ var suffixToFunc = map[string]func(string, *header.Config, *header.Result) error
}
// Fix adds the configured license header to the given file.
-func Fix(file string, config *header.Config, result *header.Result) error {
+func Fix(file string, config *header.ConfigHeader, result *header.Result) error {
var r header.Result
if err := header.CheckFile(file, config, &r); err != nil || !r.HasFailure() {
logger.Log.Warnln("Try to fix a valid file, returning:", file)
diff --git a/pkg/header/fix/hashtag.go b/pkg/header/fix/hashtag.go
index 601da4b..f467b18 100644
--- a/pkg/header/fix/hashtag.go
+++ b/pkg/header/fix/hashtag.go
@@ -19,14 +19,15 @@ package fix
import (
"io/ioutil"
- "license-checker/pkg/header"
"os"
"reflect"
"strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header"
)
// Hashtag adds the configured license header to the files whose comment starts with #.
-func Hashtag(file string, config *header.Config, result *header.Result) error {
+func Hashtag(file string, config *header.ConfigHeader, result *header.Result) error {
stat, err := os.Stat(file)
if err != nil {
return err
diff --git a/pkg/header/fix/slash_asterisk.go b/pkg/header/fix/slash_asterisk.go
index adbf4e8..5a92209 100644
--- a/pkg/header/fix/slash_asterisk.go
+++ b/pkg/header/fix/slash_asterisk.go
@@ -19,13 +19,14 @@ package fix
import (
"io/ioutil"
- "license-checker/pkg/header"
"os"
"strings"
+
+ "github.com/apache/skywalking-eyes/license-eye/pkg/header"
)
// SlashAsterisk adds the configured license header to files whose comment starts with /**.
-func SlashAsterisk(file string, config *header.Config, result *header.Result) error {
+func SlashAsterisk(file string, config *header.ConfigHeader, result *header.Result) error {
stat, err := os.Stat(file)
if err != nil {
return err
diff --git a/pkg/header/result.go b/pkg/header/result.go
index ea17fb6..068fe75 100644
--- a/pkg/header/result.go
+++ b/pkg/header/result.go
@@ -51,7 +51,7 @@ func (result *Result) HasFailure() bool {
func (result *Result) Error() error {
return fmt.Errorf(
- "The following files don't have a valid license header: \n%v",
+ "the following files don't have a valid license header: \n%v",
strings.Join(result.Failure, "\n"),
)
}
diff --git a/test/.licenserc_for_test_check.yaml b/test/.licenserc_for_test_check.yaml
index b4c1eb8..cad5ecb 100644
--- a/test/.licenserc_for_test_check.yaml
+++ b/test/.licenserc_for_test_check.yaml
@@ -1,25 +1,26 @@
-license: |
- Licensed to Apache Software Foundation (ASF) under one or more contributor
- license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright
- ownership. Apache Software Foundation (ASF) licenses this file to you under
- the Apache License, Version 2.0 (the "License"); you may
- not use this file except in compliance with the License.
- You may obtain a copy of the License at
+header:
+ license: |
+ Licensed to Apache Software Foundation (ASF) under one or more contributor
+ license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright
+ ownership. Apache Software Foundation (ASF) licenses this file to you under
+ the Apache License, Version 2.0 (the "License"); you may
+ not use this file except in compliance with the License.
+ You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
+ http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
-paths:
- - 'test/**'
+ paths:
+ - 'test/**'
-paths-ignore:
- - '**/.DS_Store'
- - '**/.json'
- - '**/exclude_test/**'
+ paths-ignore:
+ - '**/.DS_Store'
+ - '**/.json'
+ - '**/exclude_test/**'
diff --git a/test/.licenserc_for_test_fix.yaml b/test/.licenserc_for_test_fix.yaml
index 27d3087..19a864b 100644
--- a/test/.licenserc_for_test_fix.yaml
+++ b/test/.licenserc_for_test_fix.yaml
@@ -1,24 +1,25 @@
-license: |
- Licensed to Apache Software Foundation (ASF) under one or more contributor
- license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright
- ownership. Apache Software Foundation (ASF) licenses this file to you under
- the Apache License, Version 2.0 (the "License"); you may
- not use this file except in compliance with the License.
- You may obtain a copy of the License at
+header:
+ license: |
+ Licensed to Apache Software Foundation (ASF) under one or more contributor
+ license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright
+ ownership. Apache Software Foundation (ASF) licenses this file to you under
+ the Apache License, Version 2.0 (the "License"); you may
+ not use this file except in compliance with the License.
+ You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
+ http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
-paths:
- - 'test/include_test/**'
+ paths:
+ - 'test/include_test/**'
-paths-ignore:
- - '**/.DS_Store'
- - '**/.json'
+ paths-ignore:
+ - '**/.DS_Store'
+ - '**/.json'
diff --git a/test/include_test/with_license/testcase.go b/test/include_test/with_license/testcase.go
index 0f54b96..c382b60 100644
--- a/test/include_test/with_license/testcase.go
+++ b/test/include_test/with_license/testcase.go
@@ -15,4 +15,4 @@
// specific language governing permissions and limitations
// under the License.
//
-package with_license
+package withlicense
diff --git a/test/include_test/without_license/testcase.go b/test/include_test/without_license/testcase.go
index fab4aaa..6ee0c67 100644
--- a/test/include_test/without_license/testcase.go
+++ b/test/include_test/without_license/testcase.go
@@ -16,4 +16,4 @@
// consectetur mollit voluptate magna ut ullamco pariatur proident esse commodo consectetur minim in do eu
// consequat ea eiusmod proident incididunt ut qui sunt consequat officia amet amet amet dolore fugiat ex.
-package with_license
+package withlicense