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

Add exclude flag to exclude paths #14

Merged
merged 2 commits into from
Aug 1, 2018
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: 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{string(os.PathSeparator)},
)

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
54 changes: 54 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 (
"os"
"strings"
)

func needsExclusion(path string, exclude []string) bool {
for _, excluded := range exclude {
excluded = cleanPathSuffixes(excluded, []string{"*", string(os.PathSeparator)})
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