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:53 UTC

[skywalking-eyes] 19/25: Refactor the config, result, and check logic

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 9176312e255c1d9083b1cf90fb09d9bc49fa4e47
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Sun Dec 20 22:43:58 2020 +0800

    Refactor the config, result, and check logic
---
 commands/header/check.go           | 49 +++++++--------------------
 pkg/header/check.go                | 52 +++++++++++-----------------
 pkg/header/config.go               | 69 ++++++++++++++++++++++++++++++++++++++
 pkg/header/{model.go => result.go} | 37 ++++++++++++++++----
 4 files changed, 132 insertions(+), 75 deletions(-)

diff --git a/commands/header/check.go b/commands/header/check.go
index b020882..9668894 100644
--- a/commands/header/check.go
+++ b/commands/header/check.go
@@ -16,11 +16,7 @@ package header
 
 import (
 	"github.com/spf13/cobra"
-	"gopkg.in/yaml.v3"
-	"io/ioutil"
-	"license-checker/internal/logger"
 	"license-checker/pkg/header"
-	"strings"
 )
 
 var (
@@ -34,43 +30,24 @@ var CheckCommand = &cobra.Command{
 	Aliases: []string{"c"},
 	RunE: func(cmd *cobra.Command, args []string) error {
 		var config header.Config
-		if err := loadConfig(&config); err != nil {
+		var result header.Result
+
+		if err := config.Parse(cfgFile); err != nil {
 			return err
 		}
-		return header.Check(&config)
-	},
-}
-
-func init() {
-	CheckCommand.Flags().StringVarP(&cfgFile, "config", "c", ".licenserc.yaml", "the config file")
-}
-
-// loadConfig reads and parses the header check configurations in config file.
-func loadConfig(config *header.Config) error {
-	logger.Log.Infoln("Loading configuration from file:", cfgFile)
 
-	bytes, err := ioutil.ReadFile(cfgFile)
-	if err != nil {
-		return err
-	}
-
-	if err = yaml.Unmarshal(bytes, config); err != nil {
-		return err
-	}
-
-	var lines []string
-	for _, line := range strings.Split(config.License, "\n") {
-		if len(line) > 0 {
-			lines = append(lines, strings.Trim(line, header.CommentChars))
+		if err := header.Check(&config, &result); err != nil {
+			return err
 		}
-	}
-	config.License = strings.Join(lines, " ")
 
-	logger.Log.Infoln("License header is:", config.License)
+		if result.HasFailure() {
+			return result.Error()
+		}
 
-	if len(config.Paths) == 0 {
-		config.Paths = []string{"**"}
-	}
+		return nil
+	},
+}
 
-	return nil
+func init() {
+	CheckCommand.Flags().StringVarP(&cfgFile, "config", "c", ".licenserc.yaml", "the config file")
 }
diff --git a/pkg/header/check.go b/pkg/header/check.go
index 0defbe4..9f1cf62 100644
--- a/pkg/header/check.go
+++ b/pkg/header/check.go
@@ -16,7 +16,6 @@ package header
 
 import (
 	"bufio"
-	"fmt"
 	"github.com/bmatcuk/doublestar/v2"
 	"license-checker/internal/logger"
 	"os"
@@ -24,25 +23,16 @@ import (
 	"strings"
 )
 
-const CommentChars = "/*#- "
+const CommentChars = "/*#- !"
 
 // Check checks the license headers of the specified paths/globs.
-func Check(config *Config) error {
-	var result Result
-
+func Check(config *Config, result *Result) error {
 	for _, pattern := range config.Paths {
-		if err := checkPattern(pattern, &result, config); err != nil {
+		if err := checkPattern(pattern, result, config); err != nil {
 			return err
 		}
 	}
 
-	if len(result.Failure) > 0 {
-		return fmt.Errorf(
-			"The following files don't have a valid license header: \n%v",
-			strings.Join(result.Failure, "\n"),
-		)
-	}
-
 	return nil
 }
 
@@ -53,11 +43,10 @@ func checkPattern(pattern string, result *Result, config *Config) error {
 		return err
 	}
 
-	logger.Log.Infoln("Checking matched paths:", paths)
-
 	for _, path := range paths {
-		logger.Log.Debugln("Checking path:", path)
-
+		if yes, err := config.ShouldIgnore(path); yes || err != nil {
+			continue
+		}
 		if err = checkPath(path, result, config); err != nil {
 			return err
 		}
@@ -67,6 +56,10 @@ func checkPattern(pattern string, result *Result, config *Config) error {
 }
 
 func checkPath(path string, result *Result, config *Config) error {
+	if yes, err := config.ShouldIgnore(path); yes || err != nil {
+		return err
+	}
+
 	pathInfo, err := os.Stat(path)
 
 	if err != nil {
@@ -92,21 +85,16 @@ func checkPath(path string, result *Result, config *Config) error {
 	return nil
 }
 
+var seen = make(map[string]bool)
+
 func checkFile(file string, result *Result, config *Config) error {
-	skip := false
-	for _, ignorePattern := range config.PathsIgnore {
-		logger.Log.Debugln("Checking ignore pattern:", ignorePattern)
-
-		if ignored, err := doublestar.Match(ignorePattern, file); ignored || err != nil {
-			logger.Log.Infoln("Ignoring path:", file)
-			skip = ignored
-			break
-		}
-	}
-	if skip {
-		return nil
+	if yes, err := config.ShouldIgnore(file); yes || seen[file] || err != nil {
+		seen[file] = true
+		return err
 	}
 
+	seen[file] = true
+
 	logger.Log.Debugln("Checking file:", file)
 
 	reader, err := os.Open(file)
@@ -125,13 +113,13 @@ func checkFile(file string, result *Result, config *Config) error {
 		}
 	}
 
-	if content := strings.Join(lines, " "); !strings.Contains(content, config.License) {
+	if content := strings.Join(lines, " "); !strings.Contains(content, config.NormalizedLicense()) {
 		logger.Log.Debugln("Content is:", content)
 		logger.Log.Debugln("License is:", config.License)
 
-		result.Failure = append(result.Failure, file)
+		result.Fail(file)
 	} else {
-		result.Success = append(result.Success, file)
+		result.Succeed(file)
 	}
 
 	return nil
diff --git a/pkg/header/config.go b/pkg/header/config.go
new file mode 100644
index 0000000..7ff1371
--- /dev/null
+++ b/pkg/header/config.go
@@ -0,0 +1,69 @@
+// Copyright © 2020 Hoshea Jiang <ho...@apache.org>
+//
+// Licensed 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.
+
+package header
+
+import (
+	"github.com/bmatcuk/doublestar/v2"
+	"gopkg.in/yaml.v3"
+	"io/ioutil"
+	"license-checker/internal/logger"
+	"strings"
+)
+
+type Config struct {
+	License     string   `yaml:"license"`
+	Paths       []string `yaml:"paths"`
+	PathsIgnore []string `yaml:"paths-ignore"`
+}
+
+// NormalizedLicense returns the normalized string of the license content,
+// "normalized" means the linebreaks and CommentChars are all trimmed.
+func (config *Config) NormalizedLicense() string {
+	var lines []string
+	for _, line := range strings.Split(config.License, "\n") {
+		if len(line) > 0 {
+			lines = append(lines, strings.Trim(line, CommentChars))
+		}
+	}
+	return strings.Join(lines, " ")
+}
+
+// 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
+	}
+
+	logger.Log.Infoln("License header is:", config.NormalizedLicense())
+
+	if len(config.Paths) == 0 {
+		config.Paths = []string{"**"}
+	}
+
+	return nil
+}
+
+func (config *Config) ShouldIgnore(path string) (bool, error) {
+	for _, ignorePattern := range config.PathsIgnore {
+		if matched, err := doublestar.Match(ignorePattern, path); matched || err != nil {
+			return matched, err
+		}
+	}
+	return false, nil
+}
diff --git a/pkg/header/model.go b/pkg/header/result.go
similarity index 51%
rename from pkg/header/model.go
rename to pkg/header/result.go
index 5af67fd..c79ce9c 100644
--- a/pkg/header/model.go
+++ b/pkg/header/result.go
@@ -14,13 +14,36 @@
 
 package header
 
-type Config struct {
-	License     string   `yaml:"license"`
-	Paths       []string `yaml:"paths"`
-	PathsIgnore []string `yaml:"paths-ignore"`
-}
+import (
+	"fmt"
+	"strings"
+)
 
 type Result struct {
-	Success []string `yaml:"success"`
-	Failure []string `yaml:"failure"`
+	Success []string
+	Failure []string
+	Ignored []string
+}
+
+func (result *Result) Fail(file string) {
+	result.Failure = append(result.Failure, file)
+}
+
+func (result *Result) Succeed(file string) {
+	result.Success = append(result.Success, file)
+}
+
+func (result *Result) Ignore(file string) {
+	result.Ignored = append(result.Ignored, file)
+}
+
+func (result *Result) HasFailure() bool {
+	return len(result.Failure) > 0
+}
+
+func (result *Result) Error() error {
+	return fmt.Errorf(
+		"The following files don't have a valid license header: \n%v",
+		strings.Join(result.Failure, "\n"),
+	)
 }