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 ea1f818097d8b35025f8208cd372ffb03e375352
Author: kezhenxu94 <[email protected]>
AuthorDate: Mon Dec 21 10:10:32 2020 +0800

    Add `fix` command to fix license header
---
 .licenserc.yaml                      |  2 +-
 commands/header/check.go             |  4 ---
 commands/header/{check.go => fix.go} | 33 +++++++++++------------
 commands/header/header.go            |  2 ++
 go.sum                               |  8 +++++-
 main.go                              | 29 ++++++++++----------
 pkg/header/check.go                  | 18 ++++++-------
 pkg/header/config.go                 |  2 +-
 pkg/header/fix/go.go                 | 51 ++++++++++++++++++++++++++++++++++++
 pkg/header/result.go                 |  5 ++++
 10 files changed, 106 insertions(+), 48 deletions(-)

diff --git a/.licenserc.yaml b/.licenserc.yaml
index dff9cb9..14b125c 100644
--- a/.licenserc.yaml
+++ b/.licenserc.yaml
@@ -1,4 +1,4 @@
-license: >-
+license: |
   Copyright © 2020 Hoshea Jiang <[email protected]>
 
   Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/commands/header/check.go b/commands/header/check.go
index 9668894..3c3a39d 100644
--- a/commands/header/check.go
+++ b/commands/header/check.go
@@ -47,7 +47,3 @@ var CheckCommand = &cobra.Command{
                return nil
        },
 }
-
-func init() {
-       CheckCommand.Flags().StringVarP(&cfgFile, "config", "c", 
".licenserc.yaml", "the config file")
-}
diff --git a/commands/header/check.go b/commands/header/fix.go
similarity index 61%
copy from commands/header/check.go
copy to commands/header/fix.go
index 9668894..7a14789 100644
--- a/commands/header/check.go
+++ b/commands/header/fix.go
@@ -4,30 +4,28 @@
 // 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.
-
+//
 package header
 
 import (
        "github.com/spf13/cobra"
+       "license-checker/internal/logger"
        "license-checker/pkg/header"
+       "license-checker/pkg/header/fix"
+       "strings"
 )
 
-var (
-       // cfgFile is the config path to the config file of header command.
-       cfgFile string
-)
-
-var CheckCommand = &cobra.Command{
-       Use:     "check",
-       Long:    "`check` command walks the specified paths recursively and 
checks if the specified files have the license header in the config file.",
-       Aliases: []string{"c"},
+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.",
        RunE: func(cmd *cobra.Command, args []string) error {
                var config header.Config
                var result header.Result
@@ -40,14 +38,15 @@ var CheckCommand = &cobra.Command{
                        return err
                }
 
-               if result.HasFailure() {
-                       return result.Error()
+               for _, file := range result.Failure {
+                       if strings.HasSuffix(file, ".go") {
+                               logger.Log.Infoln("Fixing file:", file)
+                               if err := fix.GoLang(file, &config, &result); 
err != nil {
+                                       return err
+                               }
+                       }
                }
 
                return nil
        },
 }
-
-func init() {
-       CheckCommand.Flags().StringVarP(&cfgFile, "config", "c", 
".licenserc.yaml", "the config file")
-}
diff --git a/commands/header/header.go b/commands/header/header.go
index d6cb659..baa65b6 100644
--- a/commands/header/header.go
+++ b/commands/header/header.go
@@ -25,5 +25,7 @@ var Header = &cobra.Command{
 }
 
 func init() {
+       Header.PersistentFlags().StringVarP(&cfgFile, "config", "c", 
".licenserc.yaml", "the config file")
        Header.AddCommand(CheckCommand)
+       Header.AddCommand(FixCommand)
 }
diff --git a/go.sum b/go.sum
index fda907d..40b819b 100644
--- a/go.sum
+++ b/go.sum
@@ -23,7 +23,6 @@ github.com/beorn7/perks 
v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
 github.com/beorn7/perks v1.0.0/go.mod 
h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
 github.com/bgentry/speakeasy v0.1.0/go.mod 
h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
 github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod 
h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
-github.com/bmatcuk/doublestar v1.3.4 
h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0=
 github.com/bmatcuk/doublestar/v2 v2.0.4 
h1:6I6oUiT/sU27eE2OFcWqBhL1SwjyvQuOssxT4a1yidI=
 github.com/bmatcuk/doublestar/v2 v2.0.4/go.mod 
h1:QMmcs3H2AUQICWhfzLXz+IYln8lRQmTZRptLie8RgRw=
 github.com/cespare/xxhash v1.1.0/go.mod 
h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
@@ -35,6 +34,7 @@ github.com/coreos/go-systemd 
v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
 github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod 
h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
 github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod 
h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
 github.com/davecgh/go-spew v1.1.0/go.mod 
h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 
h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod 
h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod 
h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
 github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod 
h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
@@ -104,8 +104,10 @@ github.com/kisielk/errcheck v1.1.0/go.mod 
h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
 github.com/kisielk/gotool v1.0.0/go.mod 
h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod 
h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
 github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod 
h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
+github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
 github.com/kr/pretty v0.1.0/go.mod 
h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
 github.com/kr/text v0.1.0/go.mod 
h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/magiconair/properties v1.8.1 
h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
 github.com/magiconair/properties v1.8.1/go.mod 
h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
@@ -132,6 +134,7 @@ github.com/pelletier/go-toml v1.2.0 
h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181
 github.com/pelletier/go-toml v1.2.0/go.mod 
h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
 github.com/pkg/errors v0.8.0/go.mod 
h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pkg/errors v0.8.1/go.mod 
h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0 
h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod 
h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/posener/complete v1.1.1/go.mod 
h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
 github.com/prometheus/client_golang v0.9.1/go.mod 
h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
@@ -172,6 +175,7 @@ github.com/spf13/viper v1.7.0/go.mod 
h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
 github.com/stretchr/objx v0.1.0/go.mod 
h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/objx v0.1.1/go.mod 
h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/testify v1.2.2/go.mod 
h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
+github.com/stretchr/testify v1.3.0 
h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
 github.com/stretchr/testify v1.3.0/go.mod 
h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/subosito/gotenv v1.2.0 
h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
 github.com/subosito/gotenv v1.2.0/go.mod 
h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
@@ -266,6 +270,7 @@ golang.org/x/tools 
v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgw
 golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod 
h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod 
h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod 
h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc 
h1:NCy3Ohtk6Iny5V/reW2Ktypo4zIpWBdRJ1uFMjBxdg8=
 golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod 
h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod 
h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 google.golang.org/api v0.4.0/go.mod 
h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
@@ -291,6 +296,7 @@ google.golang.org/grpc v1.20.1/go.mod 
h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiq
 google.golang.org/grpc v1.21.1/go.mod 
h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
 gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod 
h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod 
h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 
h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod 
h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
 gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
diff --git a/main.go b/main.go
index 3f262a7..9025a0d 100644
--- a/main.go
+++ b/main.go
@@ -1,18 +1,17 @@
-/*
-Copyright © 2020 Hoshea Jiang <[email protected]>
-
-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.
-*/
+// Copyright © 2020 Hoshea Jiang <[email protected]>
+//
+// 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 main
 
 import (
diff --git a/pkg/header/check.go b/pkg/header/check.go
index 9f1cf62..32776e3 100644
--- a/pkg/header/check.go
+++ b/pkg/header/check.go
@@ -55,8 +55,12 @@ func checkPattern(pattern string, result *Result, config 
*Config) error {
        return nil
 }
 
+var seen = make(map[string]bool)
+
 func checkPath(path string, result *Result, config *Config) error {
-       if yes, err := config.ShouldIgnore(path); yes || err != nil {
+       defer func() { seen[path] = true }()
+
+       if yes, err := config.ShouldIgnore(path); yes || seen[path] || err != 
nil {
                return err
        }
 
@@ -80,21 +84,17 @@ func checkPath(path string, result *Result, config *Config) 
error {
                        return err
                }
        case mode.IsRegular():
-               return checkFile(path, result, config)
+               return CheckFile(path, config, result)
        }
        return nil
 }
 
-var seen = make(map[string]bool)
-
-func checkFile(file string, result *Result, config *Config) error {
-       if yes, err := config.ShouldIgnore(file); yes || seen[file] || err != 
nil {
-               seen[file] = true
+// CheckFile checks whether or not the file contains the configured license 
header.
+func CheckFile(file string, config *Config, result *Result) error {
+       if yes, err := config.ShouldIgnore(file); yes || err != nil {
                return err
        }
 
-       seen[file] = true
-
        logger.Log.Debugln("Checking file:", file)
 
        reader, err := os.Open(file)
diff --git a/pkg/header/config.go b/pkg/header/config.go
index 7ff1371..97a9e2b 100644
--- a/pkg/header/config.go
+++ b/pkg/header/config.go
@@ -50,7 +50,7 @@ func (config *Config) Parse(file string) error {
                return err
        }
 
-       logger.Log.Infoln("License header is:", config.NormalizedLicense())
+       logger.Log.Debugln("License header is:", config.NormalizedLicense())
 
        if len(config.Paths) == 0 {
                config.Paths = []string{"**"}
diff --git a/pkg/header/fix/go.go b/pkg/header/fix/go.go
new file mode 100644
index 0000000..c0b5eac
--- /dev/null
+++ b/pkg/header/fix/go.go
@@ -0,0 +1,51 @@
+// Copyright © 2020 Hoshea Jiang <[email protected]>
+//
+// 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 fix
+
+import (
+       "io/ioutil"
+       "license-checker/internal/logger"
+       "license-checker/pkg/header"
+       "os"
+       "strings"
+)
+
+func GoLang(file string, config *header.Config, 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)
+               return err
+       }
+
+       stat, err := os.Stat(file)
+       if err != nil {
+               return err
+       }
+
+       content, err := ioutil.ReadFile(file)
+       if 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 {
+               return err
+       }
+
+       result.Fix(file)
+
+       return nil
+}
diff --git a/pkg/header/result.go b/pkg/header/result.go
index c79ce9c..ac35d63 100644
--- a/pkg/header/result.go
+++ b/pkg/header/result.go
@@ -23,6 +23,7 @@ type Result struct {
        Success []string
        Failure []string
        Ignored []string
+       Fixed   []string
 }
 
 func (result *Result) Fail(file string) {
@@ -37,6 +38,10 @@ func (result *Result) Ignore(file string) {
        result.Ignored = append(result.Ignored, file)
 }
 
+func (result *Result) Fix(file string) {
+       result.Fixed = append(result.Fixed, file)
+}
+
 func (result *Result) HasFailure() bool {
        return len(result.Failure) > 0
 }

Reply via email to