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

Implement check and add headers in mage #13215

Merged
merged 10 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,11 @@ check: python-env

.PHONY: check-headers
check-headers:
@go get -u github.com/elastic/go-licenser
@go-licenser -d -exclude x-pack
@go-licenser -d -license Elastic x-pack
@mage checkLicenseHeaders

.PHONY: add-headers
add-headers:
@go get github.com/elastic/go-licenser
@go-licenser -exclude x-pack
@go-licenser -license Elastic x-pack
@mage addLicenseHeaders

# Corrects spelling errors
.PHONY: misspell
Expand Down
16 changes: 16 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,22 @@ Copyright 2017-2018 Elasticsearch B.V.
This product includes software developed at
Elasticsearch, B.V. (https://www.elastic.co/).

--------------------------------------------------------------------
Dependency: github.com/elastic/go-licenser
Version: 0.2.0
Revision: 2b2abd4ee9b58025ebd0630d7621cfd7619f58ac
License type (autodetected): Apache-2.0
./vendor/github.com/elastic/go-licenser/LICENSE:
--------------------------------------------------------------------
Apache License 2.0

-------NOTICE-----
Elastic go-licenser
Copyright 2018 Elasticsearch B.V.

This product includes software developed at
Elasticsearch, B.V. (https://www.elastic.co/).

--------------------------------------------------------------------
Dependency: github.com/elastic/go-lookslike
Version: v0.2.0
Expand Down
10 changes: 10 additions & 0 deletions auditbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,13 @@ func PythonIntegTest(ctx context.Context) error {
return devtools.PythonNoseTest(devtools.DefaultPythonTestIntegrationArgs())
})
}

// AddLicenseHeaders adds license headers
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
func AddLicenseHeaders() {
mg.Deps(devtools.AddLicenseHeaders)
}

// CheckLicenseHeaders checks license headers
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
func CheckLicenseHeaders() {
mg.Deps(devtools.CheckLicenseHeaders)
}
22 changes: 22 additions & 0 deletions dev-tools/mage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/magefile/mage/sh"
"github.com/pkg/errors"

"github.com/elastic/beats/dev-tools/mage/gotool"
"github.com/elastic/beats/libbeat/processors/dissect"
)

Expand Down Expand Up @@ -185,3 +186,24 @@ func GoVet() error {
err := sh.RunV("go", "vet", "./...")
return errors.Wrap(err, "failed running go vet, please fix the issues reported")
}

// CheckLicenseHeaders checks license headers in .go files. It applies the
// appropriate license header based on the value of devtools.BeatLicense.
func CheckLicenseHeaders() error {
fmt.Println(">> fmt - go-licenser: Checking for missing headers")

mg.Deps(InstallGoLicenser)

var license string
switch BeatLicense {
case "ASL2", "ASL 2.0":
license = "ASL2"
case "Elastic", "Elastic License":
license = "Elastic"
default:
return errors.Errorf("unknown license type %v", BeatLicense)
}

licenser := gotool.Licenser
return licenser(licenser.Check(), licenser.License(license))
}
12 changes: 5 additions & 7 deletions dev-tools/mage/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"

"github.com/elastic/beats/dev-tools/mage/gotool"
)

var (
Expand All @@ -35,9 +37,6 @@ var (
// GoImportsLocalPrefix is a string prefix matching imports that should be
// grouped after third-party packages.
GoImportsLocalPrefix = "github.com/elastic"

// GoLicenserImportPath controls the import path used to install go-licenser.
GoLicenserImportPath = "github.com/elastic/go-licenser"
)

// Format adds license headers, formats .go files with goimports, and formats
Expand Down Expand Up @@ -116,9 +115,7 @@ func PythonAutopep8() error {
func AddLicenseHeaders() error {
fmt.Println(">> fmt - go-licenser: Adding missing headers")

if err := sh.Run("go", "get", GoLicenserImportPath); err != nil {
return err
}
mg.Deps(InstallGoLicenser)

var license string
switch BeatLicense {
Expand All @@ -130,5 +127,6 @@ func AddLicenseHeaders() error {
return errors.Errorf("unknown license type %v", BeatLicense)
}

return sh.RunV("go-licenser", "-license", license)
licenser := gotool.Licenser
return licenser(licenser.License(license))
}
31 changes: 31 additions & 0 deletions dev-tools/mage/gotool/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. 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 gotool

type goGet func(opts ...ArgOpt) error

// Test runs `go get` and provides optionals for adding command line arguments.
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
var Get goGet = runGoGet

func runGoGet(opts ...ArgOpt) error {
args := buildArgs(opts)
return runVGo("get", args)
}

func (goGet) Update() ArgOpt { return flagBoolIf("-u", true) }
func (goGet) Package(pkg string) ArgOpt { return posArg(pkg) }
Loading