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 42ecc96dae3c76f19346d40471b5efc4c190be3d
Author: Hoshea <[email protected]>
AuthorDate: Mon Nov 23 19:59:33 2020 +0800

    dev
---
 .licenserc.json                                    | 10 ++-
 Makefile                                           |  2 +-
 README.md                                          | 36 ++++++++-
 cmd/root.go                                        | 91 +++++++++++++++-------
 main_test.go                                       |  1 +
 .licenserc.json => test/.licenserc_for_test.json   | 13 +---
 test/exclude_test/directories/testcase.go          |  1 +
 test/exclude_test/extensions/testcase.json         |  3 +
 test/{ => exclude_test/extensions}/testcase.md     |  0
 test/exclude_test/extensions/testcase.xml          |  4 +
 test/{ => include_test/with_license}/testcase.go   |  2 +-
 .../with_license/testcase.graphql}                 |  0
 test/{ => include_test/with_license}/testcase.java |  2 +-
 .../with_license/testcase.py}                      |  0
 .../with_license/testcase.sh}                      |  2 +
 .../with_license/testcase.yaml}                    |  2 +
 .../with_license/testcase.yml}                     |  0
 test/include_test/without_license/testcase.go      | 19 +++++
 .../without_license/testcase.graphql}              |  0
 test/include_test/without_license/testcase.java    | 17 ++++
 test/include_test/without_license/testcase.py      | 17 ++++
 .../without_license/testcase.sh}                   |  0
 .../without_license/testcase.yaml}                 |  0
 .../without_license/testcase.yml}                  |  0
 util/str.go                                        | 10 +++
 util/str_test.go                                   | 57 +++++++++++++-
 26 files changed, 242 insertions(+), 47 deletions(-)

diff --git a/.licenserc.json b/.licenserc.json
index 3d262cc..a15366c 100644
--- a/.licenserc.json
+++ b/.licenserc.json
@@ -14,8 +14,7 @@
     "limitations under the License."
   ],
   "licenseLoose": [
-    "Apache License",
-    "Version 2.0"
+    "Apache License, Version 2.0"
   ],
   "targetFiles": [
     "java",
@@ -31,7 +30,9 @@
       ".gitignore",
       "NOTICE",
       "go.mod",
-      "go.sum"
+      "go.sum",
+      ".DS_Store",
+      "LICENSE"
     ],
     "extensions": [
       "md",
@@ -42,7 +43,8 @@
       "bin",
       ".github",
       ".git",
-      ".idea"
+      ".idea",
+      "test"
     ]
   }
 }
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 4e83903..04af575 100644
--- a/Makefile
+++ b/Makefile
@@ -37,7 +37,7 @@ all: clean deps lint test build
 tools:
        mkdir -p $(GO_PATH)/bin
        $(GO_LINT) version || curl -sfL 
https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh 
-s -- -b $(GO_PATH)/bin v1.21.0
-       $(GO_LICENSER) -version || GO111MODULE=off $(GO_GET) -u 
github.com/elastic/go-licenser
+       #$(GO_LICENSER) -version || GO111MODULE=off $(GO_GET) -u 
github.com/elastic/go-licenser
 
 deps: tools
        $(GO_GET) -v -t -d ./...
diff --git a/README.md b/README.md
index 6aadc5a..b637500 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,36 @@
 # license-checker
-A CLI tool for checking license headers
+
+A CLI tool for checking license headers, which theoretically supports checking 
all types of files.
+
+## Install
+
+```bash 
+git clone 
+cd license-checker
+make
+```
+
+## Usage
+
+```bash
+Usage: license-checker [flags]
+
+license-checker walks the specified path recursively and checks 
+if the specified files have the license header in the config file.
+
+Usage:
+  license-checker [flags]
+
+Flags:
+  -c, --config string   the config file (default ".licenserc.json")
+  -h, --help            help for license-checker
+  -l, --loose           loose mode
+  -p, --path string     the path to check (default ".")
+  -v, --verbose         verbose mode
+```
+
+## Test
+
+```bash
+bin/license-checker -p test -c test/.licenserc_for_test.json 
+```
diff --git a/cmd/root.go b/cmd/root.go
index fed1758..84873e9 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -27,9 +27,12 @@ import (
        "strings"
 )
 
-var cfgFile string
-var checkPath string
-var loose bool
+var (
+       cfgFile   string
+       checkPath string
+       loose     bool
+       verbose   bool
+)
 
 // rootCmd represents the base command when called without any subcommands
 var rootCmd = &cobra.Command{
@@ -42,27 +45,25 @@ if the specified files have the license header in the 
config file.`,
                        fmt.Println(err)
                }
 
-               if err := Walk(checkPath, config); err != nil {
+               res, err := WalkAndCheck(checkPath, config)
+               if err != nil {
                        fmt.Println(err)
                }
+               printResult(res)
        },
 }
 
 // Execute adds all child commands to the root command and sets flags 
appropriately.
 // This is called by main.main(). It only needs to happen once to the rootCmd.
 func Execute() {
-       if err := rootCmd.Execute(); err != nil {
-               fmt.Println(err)
-               os.Exit(1)
-       }
-}
-
-func init() {
        rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", 
".licenserc.json", "the config file")
-       rootCmd.PersistentFlags().StringVarP(&checkPath, "path", "p", "", "the 
path to check (required)")
+       rootCmd.PersistentFlags().StringVarP(&checkPath, "path", "p", ".", "the 
path to check")
        rootCmd.PersistentFlags().BoolVarP(&loose, "loose", "l", false, "loose 
mode")
-       if err := rootCmd.MarkPersistentFlagRequired("path"); err != nil {
+       rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, 
"verbose mode")
+
+       if err := rootCmd.Execute(); err != nil {
                fmt.Println(err)
+               os.Exit(1)
        }
 }
 
@@ -100,18 +101,19 @@ func LoadConfig() (*Config, error) {
        return &config, nil
 }
 
-func Walk(p string, cfg *Config) error {
+func WalkAndCheck(p string, cfg *Config) (*Result, error) {
        var license []string
        if loose {
-               license = cfg.LicenseStrict
-       } else {
                license = cfg.LicenseLoose
+       } else {
+               license = cfg.LicenseStrict
        }
 
        inExcludeDir := util.InStrSliceMapKeyFunc(cfg.Exclude.Directories)
        inExcludeExt := util.InStrSliceMapKeyFunc(cfg.Exclude.Extensions)
        inExcludeFiles := util.InStrSliceMapKeyFunc(cfg.Exclude.Files)
 
+       var result Result
        err := filepath.Walk(p, func(path string, fi os.FileInfo, err error) 
error {
                if err != nil {
                        fmt.Println(err) // can't walk here,
@@ -119,7 +121,8 @@ func Walk(p string, cfg *Config) error {
                }
 
                if fi.IsDir() {
-                       if inExcludeDir(fi.Name()) {
+                       if inExcludeDir(fi.Name()) ||
+                               inExcludeDir(util.CleanPathPrefixes(path, 
[]string{p, string(os.PathSeparator)})) {
                                return filepath.SkipDir
                        }
                } else {
@@ -128,25 +131,57 @@ func Walk(p string, cfg *Config) error {
                                return nil
                        }
 
-                       // TODO: open the file and check
-                       fmt.Println(path)
-
-                       file, err := os.Open(path)
+                       ok, err := CheckLicense(path, license)
                        if err != nil {
                                return err
                        }
-                       defer file.Close()
 
-                       scanner := bufio.NewScanner(file)
-                       for scanner.Scan() {
-                               line := scanner.Text()
-                               //if strings.Contains()
+                       if ok {
+                               result.Success = append(result.Success, 
fmt.Sprintf("[Pass]: %s", path))
+                       } else {
+                               result.Failure = append(result.Failure, 
fmt.Sprintf("[No Specified License]: %s", path))
                        }
-
                }
 
                return nil
        })
 
-       return err
+       return &result, err
+}
+
+func CheckLicense(filePath string, license []string) (bool, error) {
+       file, err := os.Open(filePath)
+       if err != nil {
+               return false, err
+       }
+       defer file.Close()
+
+       index := 0
+       scanner := bufio.NewScanner(file)
+       for scanner.Scan() && index < len(license) {
+               line := scanner.Text()
+               if strings.Contains(line, license[index]) {
+                       index++
+               }
+       }
+
+       if index != len(license) {
+               return false, nil
+       }
+       return true, nil
+}
+
+func printResult(r *Result) {
+       if verbose {
+               for _, s := range r.Success {
+                       fmt.Println(s)
+               }
+       }
+
+       for _, s := range r.Failure {
+               fmt.Println(s)
+       }
+
+       fmt.Printf("Total check %d files, success: %d, failure: %d\n",
+               len(r.Success)+len(r.Failure), len(r.Success), len(r.Failure))
 }
diff --git a/main_test.go b/main_test.go
new file mode 100644
index 0000000..06ab7d0
--- /dev/null
+++ b/main_test.go
@@ -0,0 +1 @@
+package main
diff --git a/.licenserc.json b/test/.licenserc_for_test.json
similarity index 87%
copy from .licenserc.json
copy to test/.licenserc_for_test.json
index 3d262cc..8eaaa64 100644
--- a/.licenserc.json
+++ b/test/.licenserc_for_test.json
@@ -14,8 +14,7 @@
     "limitations under the License."
   ],
   "licenseLoose": [
-    "Apache License",
-    "Version 2.0"
+    "Apache License, Version 2.0"
   ],
   "targetFiles": [
     "java",
@@ -28,10 +27,7 @@
   ],
   "exclude": {
     "files": [
-      ".gitignore",
-      "NOTICE",
-      "go.mod",
-      "go.sum"
+      ".DS_Store"
     ],
     "extensions": [
       "md",
@@ -39,10 +35,7 @@
       "json"
     ],
     "directories": [
-      "bin",
-      ".github",
-      ".git",
-      ".idea"
+      "exclude_test/directories"
     ]
   }
 }
\ No newline at end of file
diff --git a/test/exclude_test/directories/testcase.go 
b/test/exclude_test/directories/testcase.go
new file mode 100644
index 0000000..672d5e8
--- /dev/null
+++ b/test/exclude_test/directories/testcase.go
@@ -0,0 +1 @@
+package directories
diff --git a/test/exclude_test/extensions/testcase.json 
b/test/exclude_test/extensions/testcase.json
new file mode 100644
index 0000000..0e0dcd2
--- /dev/null
+++ b/test/exclude_test/extensions/testcase.json
@@ -0,0 +1,3 @@
+{
+
+}
\ No newline at end of file
diff --git a/test/testcase.md b/test/exclude_test/extensions/testcase.md
similarity index 100%
copy from test/testcase.md
copy to test/exclude_test/extensions/testcase.md
diff --git a/test/exclude_test/extensions/testcase.xml 
b/test/exclude_test/extensions/testcase.xml
new file mode 100644
index 0000000..02302d8
--- /dev/null
+++ b/test/exclude_test/extensions/testcase.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<root>
+    <test>test</test>
+</root>
\ No newline at end of file
diff --git a/test/testcase.go b/test/include_test/with_license/testcase.go
similarity index 97%
rename from test/testcase.go
rename to test/include_test/with_license/testcase.go
index 4020ef8..6015e2e 100644
--- a/test/testcase.go
+++ b/test/include_test/with_license/testcase.go
@@ -15,4 +15,4 @@
  * limitations under the License.
  */
 
-package test
+package with_license
diff --git a/test/testcase3.py b/test/include_test/with_license/testcase.graphql
similarity index 100%
copy from test/testcase3.py
copy to test/include_test/with_license/testcase.graphql
diff --git a/test/testcase.java b/test/include_test/with_license/testcase.java
similarity index 95%
rename from test/testcase.java
rename to test/include_test/with_license/testcase.java
index 43a94f3..63f7276 100644
--- a/test/testcase.java
+++ b/test/include_test/with_license/testcase.java
@@ -11,4 +11,4 @@
 // 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.
+// limitations under the License.
diff --git a/test/testcase3.py b/test/include_test/with_license/testcase.py
similarity index 100%
copy from test/testcase3.py
copy to test/include_test/with_license/testcase.py
diff --git a/test/testcase3.py b/test/include_test/with_license/testcase.sh
similarity index 98%
copy from test/testcase3.py
copy to test/include_test/with_license/testcase.sh
index a9fd83f..c251402 100644
--- a/test/testcase3.py
+++ b/test/include_test/with_license/testcase.sh
@@ -1,3 +1,5 @@
+# /bin/bash
+
 #
 # Licensed to the Apache Software Foundation (ASF) under one or more
 # contributor license agreements.  See the NOTICE file distributed with
diff --git a/test/testcase3.py b/test/include_test/with_license/testcase.yaml
similarity index 99%
copy from test/testcase3.py
copy to test/include_test/with_license/testcase.yaml
index a9fd83f..c2ff44f 100644
--- a/test/testcase3.py
+++ b/test/include_test/with_license/testcase.yaml
@@ -1,4 +1,6 @@
 #
+#
+#
 # Licensed to the 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.
diff --git a/test/testcase3.py b/test/include_test/with_license/testcase.yml
similarity index 100%
rename from test/testcase3.py
rename to test/include_test/with_license/testcase.yml
diff --git a/test/include_test/without_license/testcase.go 
b/test/include_test/without_license/testcase.go
new file mode 100644
index 0000000..fab4aaa
--- /dev/null
+++ b/test/include_test/without_license/testcase.go
@@ -0,0 +1,19 @@
+// Esse enim dolore adipisicing in cillum eiusmod excepteur quis nisi sit 
dolor anim anim id id nostrud nostrud tempor.
+// Elit sit enim cillum adipisicing non magna aute nostrud ullamco dolor 
dolore consequat ut ea occaecat veniam inci
+//  occaecat consectetur eiusmod sint eiusmod aute eu duis fugiat dolore in 
laboris enim eiusmod aliquip nisi aliqua
+//  nulla proident ut ut aliqua et in ad deserunt nisi anim non dolore ut 
commodo excepteur ut eiusmod ex exercitation
+//  in sunt cillum ullamco dolor magna ex proident elit in mollit incididunt 
excepteur dolor pariatur consectetur
+//  exercitation qui ut cupidatat dolor do esse tempor commodo magna ad in 
duis voluptate est nisi in pariatur
+//  duis mollit eiusmod est magna in velit aute anim ullamco nostrud 
adipisicing cupidatat non fugiat proident
+//   commodo minim sint minim pariatur nostrud sit mollit cillum dolor minim 
quis nisi id officia veniam
+//   mollit tempor exercitation dolore duis cillum sunt esse nostrud deserunt 
amet ad quis reprehenderit
+//   do in proident est incididunt voluptate et ullamco nisi dolore commodo 
mollit elit sint aute in occaecat
+//   excepteur do pariatur laborum occaecat ad et nisi ut est in consequat 
commodo consectetur consequat ullamco m
+//   ollit aute dolor velit do deserunt eu velit reprehenderit incididunt 
commodo duis anim dolor velit nostrud eu
+//   irure dolor mollit labore sunt consectetur ullamco sed consectetur sed 
sint consectetur eu fugiat amet nostrud
+//   elit reprehenderit sunt ullamco fugiat eiusmod elit est est in duis quis 
occaecat proident aliquip adipisicing
+//    eu sint pariatur ullamco adipisicing cupidatat sed ut pariatur ut in 
amet deserunt laboris consectetur in
+//    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
diff --git a/test/testcase.md 
b/test/include_test/without_license/testcase.graphql
similarity index 100%
copy from test/testcase.md
copy to test/include_test/without_license/testcase.graphql
diff --git a/test/include_test/without_license/testcase.java 
b/test/include_test/without_license/testcase.java
new file mode 100644
index 0000000..0ba5691
--- /dev/null
+++ b/test/include_test/without_license/testcase.java
@@ -0,0 +1,17 @@
+// Esse enim dolore adipisicing in cillum eiusmod excepteur quis nisi sit 
dolor anim anim id id nostrud nostrud tempor.
+// Elit sit enim cillum adipisicing non magna aute nostrud ullamco dolor 
dolore consequat ut ea occaecat veniam incididunt
+//  occaecat consectetur eiusmod sint eiusmod aute eu duis fugiat dolore in 
laboris enim eiusmod aliquip nisi aliqua irure 
+//  nulla proident ut ut aliqua et in ad deserunt nisi anim non dolore ut 
commodo excepteur ut eiusmod ex exercitation 
+//  in sunt cillum ullamco dolor magna ex proident elit in mollit incididunt 
excepteur dolor pariatur consectetur 
+//  exercitation qui ut cupidatat dolor do esse tempor commodo magna ad in 
duis voluptate est nisi in pariatur 
+//  duis mollit eiusmod est magna in velit aute anim ullamco nostrud 
adipisicing cupidatat non fugiat proident
+//   commodo minim sint minim pariatur nostrud sit mollit cillum dolor minim 
quis nisi id officia veniam 
+//   mollit tempor exercitation dolore duis cillum sunt esse nostrud deserunt 
amet ad quis reprehenderit 
+//   do in proident est incididunt voluptate et ullamco nisi dolore commodo 
mollit elit sint aute in occaecat 
+//   excepteur do pariatur laborum occaecat ad et nisi ut est in consequat 
commodo consectetur consequat ullamco m
+//   ollit aute dolor velit do deserunt eu velit reprehenderit incididunt 
commodo duis anim dolor velit nostrud eu 
+//   irure dolor mollit labore sunt consectetur ullamco sed consectetur sed 
sint consectetur eu fugiat amet nostrud 
+//   elit reprehenderit sunt ullamco fugiat eiusmod elit est est in duis quis 
occaecat proident aliquip adipisicing
+//    eu sint pariatur ullamco adipisicing cupidatat sed ut pariatur ut in 
amet deserunt laboris consectetur in 
+//    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.
diff --git a/test/include_test/without_license/testcase.py 
b/test/include_test/without_license/testcase.py
new file mode 100644
index 0000000..5030e1c
--- /dev/null
+++ b/test/include_test/without_license/testcase.py
@@ -0,0 +1,17 @@
+# Esse enim dolore adipisicing in cillum eiusmod excepteur quis nisi sit dolor 
anim anim id id nostrud nostrud tempor.
+# Elit sit enim cillum adipisicing non magna aute nostrud ullamco dolor dolore 
consequat ut ea occaecat veniam incididunt
+#  occaecat consectetur eiusmod sint eiusmod aute eu duis fugiat dolore in 
laboris enim eiusmod aliquip nisi aliqua irure 
+#  nulla proident ut ut aliqua et in ad deserunt nisi anim non dolore ut 
commodo excepteur ut eiusmod ex exercitation 
+#  in sunt cillum ullamco dolor magna ex proident elit in mollit incididunt 
excepteur dolor pariatur consectetur 
+#  exercitation qui ut cupidatat dolor do esse tempor commodo magna ad in duis 
voluptate est nisi in pariatur 
+#  duis mollit eiusmod est magna in velit aute anim ullamco nostrud 
adipisicing cupidatat non fugiat proident
+#   commodo minim sint minim pariatur nostrud sit mollit cillum dolor minim 
quis nisi id officia veniam 
+#   mollit tempor exercitation dolore duis cillum sunt esse nostrud deserunt 
amet ad quis reprehenderit 
+#   do in proident est incididunt voluptate et ullamco nisi dolore commodo 
mollit elit sint aute in occaecat 
+#   excepteur do pariatur laborum occaecat ad et nisi ut est in consequat 
commodo consectetur consequat ullamco m
+#   ollit aute dolor velit do deserunt eu velit reprehenderit incididunt 
commodo duis anim dolor velit nostrud eu 
+#   irure dolor mollit labore sunt consectetur ullamco sed consectetur sed 
sint consectetur eu fugiat amet nostrud 
+#   elit reprehenderit sunt ullamco fugiat eiusmod elit est est in duis quis 
occaecat proident aliquip adipisicing
+#    eu sint pariatur ullamco adipisicing cupidatat sed ut pariatur ut in amet 
deserunt laboris consectetur in 
+#    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.
diff --git a/test/testcase.md b/test/include_test/without_license/testcase.sh
similarity index 100%
copy from test/testcase.md
copy to test/include_test/without_license/testcase.sh
diff --git a/test/testcase.md b/test/include_test/without_license/testcase.yaml
similarity index 100%
copy from test/testcase.md
copy to test/include_test/without_license/testcase.yaml
diff --git a/test/testcase.md b/test/include_test/without_license/testcase.yml
similarity index 100%
rename from test/testcase.md
rename to test/include_test/without_license/testcase.yml
diff --git a/util/str.go b/util/str.go
index b953e48..e907b90 100644
--- a/util/str.go
+++ b/util/str.go
@@ -41,3 +41,13 @@ func GetFileExtension(filename string) string {
        }
        return ""
 }
+
+func CleanPathPrefixes(path string, prefixes []string) string {
+       for _, prefix := range prefixes {
+               if strings.HasPrefix(path, prefix) && len(path) > 0 {
+                       path = path[len(prefix):]
+               }
+       }
+
+       return path
+}
diff --git a/util/str_test.go b/util/str_test.go
index fce8965..cf64a96 100644
--- a/util/str_test.go
+++ b/util/str_test.go
@@ -15,7 +15,10 @@ limitations under the License.
 */
 package util
 
-import "testing"
+import (
+       "os"
+       "testing"
+)
 
 func TestGetFileExtension(t *testing.T) {
        type args struct {
@@ -56,3 +59,55 @@ func TestGetFileExtension(t *testing.T) {
                })
        }
 }
+
+func Test_cleanPathPrefixes(t *testing.T) {
+       type args struct {
+               path     string
+               prefixes []string
+       }
+       tests := []struct {
+               name string
+               args args
+               want string
+       }{
+               {
+                       name: "",
+                       args: args{
+                               path:     "test/exclude_test",
+                               prefixes: []string{"test", 
string(os.PathSeparator)},
+                       },
+                       want: "exclude_test",
+               },
+               {
+                       name: "",
+                       args: args{
+                               path:     "test/exclude_test/directories",
+                               prefixes: []string{"test", 
string(os.PathSeparator)},
+                       },
+                       want: "exclude_test/directories",
+               },
+               {
+                       name: "",
+                       args: args{
+                               path:     "./.git/",
+                               prefixes: []string{".", 
string(os.PathSeparator)},
+                       },
+                       want: ".git/",
+               },
+               {
+                       name: "",
+                       args: args{
+                               path:     "test/exclude_test/directories",
+                               prefixes: []string{"test/", 
string(os.PathSeparator)},
+                       },
+                       want: "exclude_test/directories",
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       if got := CleanPathPrefixes(tt.args.path, 
tt.args.prefixes); got != tt.want {
+                               t.Errorf("CleanPathPrefixes() = %v, want %v", 
got, tt.want)
+                       }
+               })
+       }
+}

Reply via email to