Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add reasonable default config to allow running in a new repo without copying config file #28

Merged
merged 1 commit into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets.go → assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"io/fs"
)

//go:embed assets
//go:embed *
var assets embed.FS

func Asset(file string) ([]byte, error) {
Expand Down
35 changes: 35 additions & 0 deletions assets/default-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# 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. The 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.
#
header:
license:
spdx-id: Apache-2.0
copyright-owner: Apache Software Foundation

paths-ignore:
- '**/*.md'
- '**/*.json'
- '**/*.txt'
- '.gitignore'
- '.gitmodules'
- 'LICENSE'
- 'NOTICE'
- 'go.mod'
- 'go.sum'

comment: on-failure
6 changes: 3 additions & 3 deletions pkg/comments/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"strings"

assets "github.com/apache/skywalking-eyes/license-eye"
"github.com/apache/skywalking-eyes/license-eye/assets"

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -74,7 +74,7 @@ func init() {
}

func initLanguages() {
content, err := assets.Asset("assets/languages.yaml")
content, err := assets.Asset("languages.yaml")
if err != nil {
panic(fmt.Errorf("should never happen: %w", err))
}
Expand All @@ -89,7 +89,7 @@ func initLanguages() {
}

func initCommentStyles() {
content, err := assets.Asset("assets/styles.yaml")
content, err := assets.Asset("styles.yaml")
if err != nil {
panic(fmt.Errorf("should never happen: %w", err))
}
Expand Down
27 changes: 21 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package config

import (
"io/ioutil"
"os"

"github.com/apache/skywalking-eyes/license-eye/assets"
"github.com/apache/skywalking-eyes/license-eye/internal/logger"
"github.com/apache/skywalking-eyes/license-eye/pkg/deps"
"github.com/apache/skywalking-eyes/license-eye/pkg/header"
Expand All @@ -33,12 +34,26 @@ type Config struct {
}

// 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)
func (config *Config) Parse(file string) (err error) {
var bytes []byte

if bytes, err := ioutil.ReadFile(file); err != nil {
return err
} else if err := yaml.Unmarshal(bytes, config); err != nil {
if file != "" {
logger.Log.Infoln("Loading configuration from file:", file)

if bytes, err = os.ReadFile(file); err != nil && !os.IsNotExist(err) {
return err
}
}

if os.IsNotExist(err) {
logger.Log.Infof("No config file is given, using the default config")

if bytes, err = assets.Asset("default-config.yaml"); err != nil {
return err
}
}

if err := yaml.Unmarshal(bytes, config); err != nil {
return err
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/deps/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package deps

import (
"os"
"path/filepath"
)

Expand All @@ -28,6 +29,9 @@ type ConfigDeps struct {
func (config *ConfigDeps) Finalize(configFile string) error {
configFileAbsPath, err := filepath.Abs(configFile)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/header/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strings"
"time"

assets "github.com/apache/skywalking-eyes/license-eye"
"github.com/apache/skywalking-eyes/license-eye/assets"
"github.com/apache/skywalking-eyes/license-eye/internal/logger"
"github.com/apache/skywalking-eyes/license-eye/pkg/license"

Expand Down Expand Up @@ -137,12 +137,12 @@ func (config *ConfigHeader) GetLicenseContent() string {

func readLicenseFromSpdx(config *ConfigHeader) (string, error) {
spdxID, owner := config.License.SpdxID, config.License.CopyrightOwner
filename := fmt.Sprintf("assets/header-templates/%v.txt", spdxID)
filename := fmt.Sprintf("header-templates/%v.txt", spdxID)

if spdxID == "Apache-2.0" && ASFNames.MatchString(owner) {
// Note that the Apache Software Foundation uses a different source header that is related to our use of a CLA.
// Our instructions for our project's source headers are here (https://www.apache.org/legal/src-headers.html#headers).
filename = "assets/header-templates/Apache-2.0-ASF.txt"
filename = "header-templates/Apache-2.0-ASF.txt"
}

content, err := assets.Asset(filename)
Expand Down
4 changes: 2 additions & 2 deletions pkg/license/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"regexp"
"strings"

assets "github.com/apache/skywalking-eyes/license-eye"
"github.com/apache/skywalking-eyes/license-eye/assets"
)

const templatesDir = "assets/lcs-templates"
const templatesDir = "lcs-templates"

var dualLicensePatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)This project is covered by two different licenses: (?P<license>[^.]+)`),
Expand Down