-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
implement .goitignore
- Loading branch information
Showing
6 changed files
with
154 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package store | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
directoryRegexp = regexp.MustCompile(`.*\/`) | ||
) | ||
|
||
type Ignore struct { | ||
paths []string | ||
} | ||
|
||
func NewIgnore(rootGoitPath string) (*Ignore, error) { | ||
i := newIgnore() | ||
if err := i.load(rootGoitPath); err != nil { | ||
return nil, err | ||
} | ||
return i, nil | ||
} | ||
|
||
func newIgnore() *Ignore { | ||
return &Ignore{ | ||
paths: []string{`\.goit/.*`}, | ||
} | ||
} | ||
|
||
func (i *Ignore) load(rootGoitPath string) error { | ||
goitignorePath := filepath.Join(filepath.Dir(rootGoitPath), ".goitignore") | ||
if _, err := os.Stat(goitignorePath); os.IsNotExist(err) { | ||
return nil | ||
} | ||
f, err := os.Open(goitignorePath) | ||
if err != nil { | ||
return fmt.Errorf("fail to open %s: %w", goitignorePath, err) | ||
} | ||
defer f.Close() | ||
|
||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
text := scanner.Text() | ||
var replacedText string | ||
if directoryRegexp.MatchString(text) { | ||
replacedText = fmt.Sprintf("%s.*", text) | ||
} else { | ||
replacedText = strings.ReplaceAll(text, ".", `\.`) | ||
replacedText = strings.ReplaceAll(replacedText, "*", ".*") | ||
} | ||
i.paths = append(i.paths, replacedText) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// return true if the parameter is included in ignore list | ||
func (i *Ignore) IsIncluded(path string) bool { | ||
target := path | ||
info, _ := os.Stat(path) | ||
if info.IsDir() && !directoryRegexp.MatchString(path) { | ||
target = fmt.Sprintf("%s/", path) | ||
} | ||
for _, exFile := range i.paths { | ||
exRegexp := regexp.MustCompile(exFile) | ||
if exRegexp.MatchString(target) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package store | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNewIgnore(t *testing.T) { | ||
type fields struct { | ||
content string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want *Ignore | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "success: empty", | ||
fields: fields{ | ||
content: "", | ||
}, | ||
want: &Ignore{ | ||
paths: []string{`\.goit/.*`}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "success: some ignore list", | ||
fields: fields{ | ||
content: "*.exe\ndir/", | ||
}, | ||
want: &Ignore{ | ||
paths: []string{`\.goit/.*`, `.*\.exe`, `dir/.*`}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
goitignorePath := filepath.Join(tmpDir, ".goitignore") | ||
f, err := os.Create(goitignorePath) | ||
if err != nil { | ||
t.Log(err) | ||
} | ||
_, err = f.WriteString(tt.fields.content) | ||
if err != nil { | ||
t.Log(err) | ||
} | ||
f.Close() | ||
|
||
goitPath := filepath.Join(tmpDir, ".goit") | ||
i, err := NewIgnore(goitPath) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("got = %v, want = %v", err, tt.wantErr) | ||
} | ||
if !reflect.DeepEqual(i, tt.want) { | ||
t.Errorf("got = %v, want = %v", i, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.exe | ||
dir/dir2/ |