Skip to content

Commit

Permalink
Merge pull request #4 from budougumi0617/ignore-specified-dir
Browse files Browse the repository at this point in the history
ignore specified dir
  • Loading branch information
budougumi0617 committed Jan 11, 2021
2 parents 2f66d51 + b8d9e4c commit c8c73b6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 12 deletions.
51 changes: 47 additions & 4 deletions nrseg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nrseg

import (
"bytes"
"errors"
"flag"
"fmt"
"io"
Expand All @@ -12,8 +13,18 @@ import (
"strings"
)

var (
// ErrShowVersion returns when set version flag.
ErrShowVersion = errors.New("show version")
)

var (
version = "dev"
)

type nrseg struct {
in, dist string
ignoreDirs []string
outStream, errStream io.Writer
}

Expand All @@ -22,9 +33,28 @@ func fill(args []string, outStream, errStream io.Writer) (*nrseg, error) {
flags := flag.NewFlagSet(cn, flag.ContinueOnError)
flags.SetOutput(errStream)

var v bool
vdesc := "print version information and quit."
flags.BoolVar(&v, "version", false, vdesc)
flags.BoolVar(&v, "v", false, vdesc)

var ignoreDirs string
idesc := "ignore directory names. ex: foo,bar,baz"
flags.StringVar(&ignoreDirs, "ignore", "", idesc)
flags.StringVar(&ignoreDirs, "i", "", idesc)

if err := flags.Parse(args[1:]); err != nil {
return nil, err
}
if v {
fmt.Fprintf(errStream, "%s version %s\n", cn, version)
return nil, ErrShowVersion
}

dirs := []string{"testdata"}
if len(ignoreDirs) != 0 {
dirs = append(dirs, strings.Split(ignoreDirs, ",")...)
}

dir := "./"
nargs := flags.Args()
Expand All @@ -37,17 +67,30 @@ func fill(args []string, outStream, errStream io.Writer) (*nrseg, error) {
}

return &nrseg{
in: dir,
outStream: outStream,
errStream: errStream,
in: dir,
ignoreDirs: dirs,
outStream: outStream,
errStream: errStream,
}, nil
}

var c = regexp.MustCompile("(?m)^// Code generated .* DO NOT EDIT\\.$")

func (n *nrseg) skipDir(p string) bool {
for _, dir := range n.ignoreDirs {
if filepath.Base(p) == dir {
return true
}
}
return false
}

func (n *nrseg) run() error {
return filepath.Walk(n.in, func(path string, info os.FileInfo, err error) error {
fmt.Fprintf(n.outStream, "walk %q\n", path)
if info.IsDir() && n.skipDir(path) {
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
Expand All @@ -73,7 +116,7 @@ func (n *nrseg) run() error {
}
got, err := Process(path, org)
if err != nil {
fmt.Fprintf(n.errStream, "Process failed %q: %v\n", path, err)
fmt.Fprintf(n.errStream, "process failed %q: %v\n", path, err)
return err
}
fmt.Fprintf(n.outStream, "got %q\n", got)
Expand Down
13 changes: 5 additions & 8 deletions nrseg_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nrseg

import (
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -37,10 +36,11 @@ func TestNrseg_run(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
dist := t.TempDir()
n := &nrseg{
in: tt.fields.path,
dist: dist,
outStream: tt.fields.outStream,
errStream: tt.fields.errStream,
in: tt.fields.path,
dist: dist,
ignoreDirs: []string{"testdata", "ignore"},
outStream: tt.fields.outStream,
errStream: tt.fields.errStream,
}
if err := n.run(); err != nil {
t.Fatalf("run() error = %v", err)
Expand All @@ -52,9 +52,6 @@ func TestNrseg_run(t *testing.T) {

func validate(t *testing.T, gotpath, wantpath string) {
filepath.Walk(gotpath, func(path string, info os.FileInfo, err error) error {
if filepath.Base(path) == "testdata" {
return fmt.Errorf("skip testdata dir")
}
if info.IsDir() {
return nil
}
Expand Down
23 changes: 23 additions & 0 deletions testdata/input/ignore/must_not_change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"context"
"fmt"
"net/http"
)

type MustNotChange struct{}

func (m *MustNotChange) SampleMethod(ctx context.Context) {
fmt.Println("Hello, playground")
fmt.Println("end function")
}

func SampleFunc(ctx context.Context) {
fmt.Println("Hello, playground")
fmt.Println("end function")
}

func SampleHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Hello, %q", req.URL.Path)
}
23 changes: 23 additions & 0 deletions testdata/want/ignore/must_not_change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"context"
"fmt"
"net/http"
)

type MustNotChange struct{}

func (m *MustNotChange) SampleMethod(ctx context.Context) {
fmt.Println("Hello, playground")
fmt.Println("end function")
}

func SampleFunc(ctx context.Context) {
fmt.Println("Hello, playground")
fmt.Println("end function")
}

func SampleHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Hello, %q", req.URL.Path)
}

0 comments on commit c8c73b6

Please sign in to comment.