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 7a4704257468abb2d2bc34f22301ba3ea4b07ffa Author: kezhenxu94 <[email protected]> AuthorDate: Mon Dec 21 11:08:39 2020 +0800 Add more supported fixing filetypes and update license --- .github/workflows/build.yaml | 29 ++++++++++++---------- .gitignore | 29 ++++++++++++---------- .licenserc.yaml | 22 ++++++++++------- Dockerfile | 29 ++++++++++++---------- Makefile | 29 ++++++++++++---------- action.yml | 29 ++++++++++++---------- cmd/root.go | 33 +++++++++++++------------ commands/header/check.go | 25 ++++++++++--------- commands/header/fix.go | 35 +++++++++++++++------------ commands/header/header.go | 25 ++++++++++--------- internal/logger/log.go | 25 ++++++++++--------- main.go | 21 +++++++++------- pkg/header/check.go | 27 ++++++++++++--------- pkg/header/config.go | 25 ++++++++++--------- pkg/header/fix/anglebracket.go | 55 ++++++++++++++++++++++++++++++++++++++++++ pkg/header/fix/doubleslash.go | 48 ++++++++++++++++++++++++++++++++++++ pkg/header/fix/fix.go | 52 +++++++++++++++++++++++++++++++++++++++ pkg/header/fix/go.go | 51 --------------------------------------- pkg/header/fix/hashtag.go | 48 ++++++++++++++++++++++++++++++++++++ pkg/header/result.go | 25 ++++++++++--------- 20 files changed, 430 insertions(+), 232 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 55f216f..1fa632a 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,17 +1,20 @@ -# 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. +# 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. - +# +# 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. +# name: Build on: diff --git a/.gitignore b/.gitignore index 1bbee36..b4c85e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,19 @@ -# 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. +# 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. - +# +# 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. +# .idea/ bin/ diff --git a/.licenserc.yaml b/.licenserc.yaml index 14b125c..d14df0b 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -1,21 +1,25 @@ 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. + 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. + 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' - 'test/**' - 'go.mod' diff --git a/Dockerfile b/Dockerfile index 602de2d..a466f44 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,20 @@ -# 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. +# 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. - +# +# 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. +# FROM golang:1.14.3-alpine AS build WORKDIR /src diff --git a/Makefile b/Makefile index 6220eba..e2a933a 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,20 @@ -# 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. +# 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. - +# +# 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. +# PROJECT = license-checker VERSION ?= latest OUT_DIR = bin diff --git a/action.yml b/action.yml index f9773c5..bcdf432 100644 --- a/action.yml +++ b/action.yml @@ -1,17 +1,20 @@ -# 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. +# 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. - +# +# 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. +# name: License Guard description: A tool for checking license headers, which theoretically supports checking all types of files. branding: diff --git a/cmd/root.go b/cmd/root.go index 93a195e..e6d4b98 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,19 +1,20 @@ -/* -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. -*/ - +// 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. +// package cmd import ( diff --git a/commands/header/check.go b/commands/header/check.go index 3c3a39d..ef141cc 100644 --- a/commands/header/check.go +++ b/commands/header/check.go @@ -1,17 +1,20 @@ -// 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. +// 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. - package header import ( diff --git a/commands/header/fix.go b/commands/header/fix.go index 7a14789..dc50e65 100644 --- a/commands/header/fix.go +++ b/commands/header/fix.go @@ -1,22 +1,25 @@ -// 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. +// 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. +// 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 ( + "fmt" "github.com/spf13/cobra" - "license-checker/internal/logger" "license-checker/pkg/header" "license-checker/pkg/header/fix" "strings" @@ -38,15 +41,17 @@ var FixCommand = &cobra.Command{ return err } + var errors []string 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 - } + if err := fix.Fix(file, &config, &result); err != nil { + errors = append(errors, err.Error()) } } + if len(errors) > 0 { + return fmt.Errorf(strings.Join(errors, "\n")) + } + return nil }, } diff --git a/commands/header/header.go b/commands/header/header.go index baa65b6..86d58da 100644 --- a/commands/header/header.go +++ b/commands/header/header.go @@ -1,17 +1,20 @@ -// 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. +// 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. - package header import ( diff --git a/internal/logger/log.go b/internal/logger/log.go index 602437b..7fc102e 100644 --- a/internal/logger/log.go +++ b/internal/logger/log.go @@ -1,17 +1,20 @@ -// 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. +// 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. - package logger import ( diff --git a/main.go b/main.go index 9025a0d..b2f1453 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,19 @@ -// 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. +// 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. +// 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 diff --git a/pkg/header/check.go b/pkg/header/check.go index 32776e3..006fc9c 100644 --- a/pkg/header/check.go +++ b/pkg/header/check.go @@ -1,17 +1,20 @@ -// 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. +// 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. - package header import ( @@ -23,7 +26,7 @@ import ( "strings" ) -const CommentChars = "/*#- !" +const CommentChars = "/*#- !~" // Check checks the license headers of the specified paths/globs. func Check(config *Config, result *Result) error { diff --git a/pkg/header/config.go b/pkg/header/config.go index 97a9e2b..f3629d1 100644 --- a/pkg/header/config.go +++ b/pkg/header/config.go @@ -1,17 +1,20 @@ -// 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. +// 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. - package header import ( diff --git a/pkg/header/fix/anglebracket.go b/pkg/header/fix/anglebracket.go new file mode 100644 index 0000000..e027536 --- /dev/null +++ b/pkg/header/fix/anglebracket.go @@ -0,0 +1,55 @@ +// 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. +// +package fix + +import ( + "fmt" + "io/ioutil" + "license-checker/pkg/header" + "os" + "reflect" + "strings" +) + +// AngleBracket adds the configured license header to files whose comment starts with <!--. +func AngleBracket(file string, config *header.Config, result *header.Result) error { + stat, err := os.Stat(file) + if err != nil { + return err + } + + content, err := ioutil.ReadFile(file) + if err != nil { + return err + } + + if !reflect.DeepEqual(content[0:5], []byte("<?xml")) { // doesn't contains xml declaration + lines := "<!--\n ~ " + strings.Join(strings.Split(config.License, "\n"), "\n ~ ") + "\n-->\n" + + if err := ioutil.WriteFile(file, append([]byte(lines), content...), stat.Mode()); err != nil { + return err + } + + result.Fix(file) + } else { + // TODO: tackle with the "xml declaration" + return fmt.Errorf("xml with xml declaration is not supported yet") + } + + return nil +} diff --git a/pkg/header/fix/doubleslash.go b/pkg/header/fix/doubleslash.go new file mode 100644 index 0000000..ab2819a --- /dev/null +++ b/pkg/header/fix/doubleslash.go @@ -0,0 +1,48 @@ +// 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. +// +package fix + +import ( + "io/ioutil" + "license-checker/pkg/header" + "os" + "strings" +) + +// 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 + } + + 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/fix/fix.go b/pkg/header/fix/fix.go new file mode 100644 index 0000000..48b3c47 --- /dev/null +++ b/pkg/header/fix/fix.go @@ -0,0 +1,52 @@ +// 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. +// +package fix + +import ( + "fmt" + "license-checker/internal/logger" + "license-checker/pkg/header" + "strings" +) + +var suffixToFunc = map[string]func(string, *header.Config, *header.Result) error{ + ".go": DoubleSlash, + ".yml": Hashtag, + ".yaml": Hashtag, + "Dockerfile": Hashtag, + "Makefile": Hashtag, + ".gitignore": Hashtag, + ".md": AngleBracket, +} + +// Fix adds the configured license header to the given file. +func Fix(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 + } + + for suffix, fixFunc := range suffixToFunc { + if strings.HasSuffix(file, suffix) { + return fixFunc(file, config, result) + } + } + + return fmt.Errorf("file type is unsupported yet: %v", file) +} diff --git a/pkg/header/fix/go.go b/pkg/header/fix/go.go deleted file mode 100644 index c0b5eac..0000000 --- a/pkg/header/fix/go.go +++ /dev/null @@ -1,51 +0,0 @@ -// 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/fix/hashtag.go b/pkg/header/fix/hashtag.go new file mode 100644 index 0000000..761acc8 --- /dev/null +++ b/pkg/header/fix/hashtag.go @@ -0,0 +1,48 @@ +// 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. +// +package fix + +import ( + "io/ioutil" + "license-checker/pkg/header" + "os" + "strings" +) + +// Hashtag adds the configured license header to the files whose comment starts with #. +func Hashtag(file string, config *header.Config, result *header.Result) error { + 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 ac35d63..e979154 100644 --- a/pkg/header/result.go +++ b/pkg/header/result.go @@ -1,17 +1,20 @@ -// 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. +// 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. - package header import (
