Skip to content

Commit

Permalink
Implement check and add headers in mage (elastic#13215)
Browse files Browse the repository at this point in the history
Add a framework to wrap go tooling in mage, taken from go-txtfile.

Using it, reimplement all uses of go-licenser in mage.

go-licenser is vendored.
  • Loading branch information
jsoriano authored Sep 18, 2019
1 parent ebd8ddb commit 4131241
Show file tree
Hide file tree
Showing 26 changed files with 1,440 additions and 30 deletions.
12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,12 @@ check: python-env
@git diff-index --exit-code HEAD --

.PHONY: check-headers
check-headers:
@go get -u github.com/elastic/go-licenser
@go-licenser -d -exclude x-pack -exclude generator/beat/\{beat\} -exclude generator/metricbeat/\{beat\}
@go-licenser -d -license Elastic x-pack
check-headers: mage
@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
add-headers: mage
@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 @@ -859,6 +859,22 @@ License type (autodetected): Apache-2.0
Apache License 2.0


--------------------------------------------------------------------
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.3.0
Expand Down
21 changes: 21 additions & 0 deletions dev-tools/mage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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 @@ -189,6 +190,26 @@ func GoVet() error {
return errors.Wrap(err, "failed running go vet, please fix the issues reported")
}

// CheckLicenseHeaders checks license headers in .go files.
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))
}

// CheckDashboardsFormat checks the format of dashboards
func CheckDashboardsFormat() error {
dashboardSubDir := "/_meta/kibana/"
Expand Down
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 @@ -120,9 +119,7 @@ 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 @@ -134,5 +131,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

// Get runs `go get` and provides optionals for adding command line arguments.
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

0 comments on commit 4131241

Please sign in to comment.