Skip to content

Commit

Permalink
Add exclude flag to exclude paths
Browse files Browse the repository at this point in the history
This change adds an -exclude flag that can be specified as many times
as desired to build a list of excluded paths that will not be factored
in the go-licenser checks.

Signed-off-by: Marc Lopez <[email protected]>
  • Loading branch information
marclop committed Jul 19, 2018
1 parent 5a4a578 commit e52a042
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 23 deletions.
2 changes: 2 additions & 0 deletions fixtures/excludedpath/file.testdata
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package excludedpath holds test data for the licensing cmd
package excludedpath
19 changes: 19 additions & 0 deletions golden/excludedpath/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 excludedpath holds test data for the licensing cmd
package excludedpath
34 changes: 29 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/elastic/go-licenser/licensing"
)
Expand Down Expand Up @@ -78,10 +79,27 @@ var (
extension string
args []string
headerBytes []byte
exclude sliceFlag
defaultExludedDirs = []string{"vendor", ".git"}
)

type sliceFlag []string

func (f *sliceFlag) String() string {
var s string
for _, i := range *f {
s += i + " "
}
return s
}

func (f *sliceFlag) Set(value string) error {
*f = append(*f, value)
return nil
}

func init() {
flag.Var(&exclude, "exclude", `path to exclude (can be specified multiple times).`)
flag.BoolVar(&dryRun, "d", false, `skips rewriting files and returns exitcode 1 if any discrepancies are found.`)
flag.StringVar(&extension, "ext", defaultExt, "sets the file extension to scan for.")
flag.Usage = usageFlag
Expand All @@ -94,15 +112,15 @@ func init() {
}

func main() {
err := run(args, defaultExludedDirs, extension, dryRun, os.Stdout)
err := run(args, exclude, extension, dryRun, os.Stdout)
if err != nil && err.Error() != "<nil>" {
fmt.Println(err)
fmt.Fprint(os.Stderr, err)
}

os.Exit(Code(err))
}

func run(args, exclDirs []string, ext string, dry bool, out io.Writer) error {
func run(args, exclude []string, ext string, dry bool, out io.Writer) error {
var path = defaultPath
if len(args) > 0 {
path = args[0]
Expand All @@ -112,7 +130,7 @@ func run(args, exclDirs []string, ext string, dry bool, out io.Writer) error {
return &Error{err: err, code: exitFailedToStatTree}
}

return walk(path, ext, defaultExludedDirs, dry, out)
return walk(path, ext, exclude, dry, out)
}

func reportFile(out io.Writer, f string) {
Expand All @@ -132,7 +150,13 @@ func walk(p, ext string, exclude []string, dry bool, out io.Writer) error {
return walkErr
}

if info.IsDir() && stringInSlice(info.Name(), exclude) {
var currentPath = cleanPathPrefixes(
strings.Replace(path, p, "", 1),
[]string{"/"},
)

var excludedDir = info.IsDir() && stringInSlice(info.Name(), defaultExludedDirs)
if needsExclusion(currentPath, exclude) || excludedDir {
return filepath.SkipDir
}

Expand Down
35 changes: 17 additions & 18 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ func dcopy(src, dest string, info os.FileInfo) error {

func Test_run(t *testing.T) {
type args struct {
args []string
exclDirs []string
ext string
dry bool
args []string
exclude []string
ext string
dry bool
}
tests := []struct {
name string
Expand All @@ -121,10 +121,10 @@ func Test_run(t *testing.T) {
{
name: "Run a diff prints a list of files that need the license header",
args: args{
args: []string{"testdata"},
exclDirs: defaultExludedDirs,
ext: defaultExt,
dry: true,
args: []string{"testdata"},
exclude: []string{"excludedpath"},
ext: defaultExt,
dry: true,
},
want: 1,
err: &Error{code: 1},
Expand All @@ -142,21 +142,20 @@ testdata/singlelevel/wrapper.go: is missing the license header
{
name: "Run against an unexisting dir fails",
args: args{
args: []string{"ignore"},
exclDirs: defaultExludedDirs,
ext: defaultExt,
dry: false,
args: []string{"ignore"},
ext: defaultExt,
dry: false,
},
want: 2,
err: goosPathError(2, "ignore"),
},
{
name: "Run with default mode rewrites the source files",
args: args{
args: []string{"testdata"},
exclDirs: defaultExludedDirs,
ext: defaultExt,
dry: false,
args: []string{"testdata"},
exclude: []string{"excludedpath"},
ext: defaultExt,
dry: false,
},
want: 0,
wantGolden: true,
Expand All @@ -169,7 +168,7 @@ testdata/singlelevel/wrapper.go: is missing the license header
}

var buf = new(bytes.Buffer)
var err = run(tt.args.args, tt.args.exclDirs, tt.args.ext, tt.args.dry, buf)
var err = run(tt.args.args, tt.args.exclude, tt.args.ext, tt.args.dry, buf)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("run() error = %v, wantErr %v", err, tt.err)
return
Expand All @@ -189,7 +188,7 @@ testdata/singlelevel/wrapper.go: is missing the license header
if tt.wantGolden {
if *update {
copyFixtures(t, "golden")
if err := run([]string{"golden"}, tt.args.exclDirs, tt.args.ext, tt.args.dry, buf); err != nil {
if err := run([]string{"golden"}, tt.args.exclude, tt.args.ext, tt.args.dry, buf); err != nil {
t.Fatal(err)
}
}
Expand Down
51 changes: 51 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 main

import "strings"

func needsExclusion(path string, exclude []string) bool {
for _, excluded := range exclude {
excluded = cleanPathSuffixes(excluded, []string{"*", "/"})
if strings.HasPrefix(path, excluded) {
return true
}
}

return false
}

func cleanPathSuffixes(path string, sufixes []string) string {
for _, suffix := range sufixes {
for strings.HasSuffix(path, suffix) && len(path) > 0 {
path = path[:len(path)-len(suffix)]
}
}

return path
}

func cleanPathPrefixes(path string, prefixes []string) string {
for _, prefix := range prefixes {
for strings.HasPrefix(path, prefix) && len(path) > 0 {
path = path[len(prefix):]
}
}

return path
}
Loading

0 comments on commit e52a042

Please sign in to comment.