Skip to content

Commit

Permalink
add ignore to client (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 17, 2023
1 parent 9a99b9d commit 8f01113
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
9 changes: 7 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ func init() {
fmt.Println(err)
os.Exit(1)
}
r, err := store.NewRefs(rootGoitPath)
refs, err := store.NewRefs(rootGoitPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
client = store.NewClient(config, index, head, r, rootGoitPath)
ignore, err := store.NewIgnore(rootGoitPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
client = store.NewClient(config, index, head, refs, ignore, rootGoitPath)

gLogger = log.NewGoitLogger(client.RootGoitPath)

Expand Down
6 changes: 3 additions & 3 deletions internal/store/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ type Client struct {
Idx *Index
Head *Head
Refs *Refs
Ignore *Ignore
RootGoitPath string
ExcludePaths []string
}

func NewClient(config *Config, index *Index, head *Head, refs *Refs, rootGoitPath string) *Client {
func NewClient(config *Config, index *Index, head *Head, refs *Refs, ignore *Ignore, rootGoitPath string) *Client {
return &Client{
Conf: config,
Idx: index,
Head: head,
Refs: refs,
Ignore: ignore,
RootGoitPath: rootGoitPath,
ExcludePaths: []string{".goit"},
}
}
59 changes: 59 additions & 0 deletions internal/store/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package store

import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
)

var (
directoryRegexp = regexp.MustCompile(`.*\/`)
)

type Ignore struct {
file []string
directory []string
}

func NewIgnore(rootGoitPath string) (*Ignore, error) {
i := newIgnore()
if err := i.load(rootGoitPath); err != nil {
return nil, err
}
fmt.Println(i.file, i.directory)
return i, nil
}

func newIgnore() *Ignore {
return &Ignore{
file: make([]string, 0),
directory: []string{".goit/"},
}
}

func (i *Ignore) load(rootGoitPath string) error {
goitignorePath := filepath.Join(filepath.Dir(rootGoitPath), ".goitignore")
fmt.Println(goitignorePath)
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()
if directoryRegexp.MatchString(text) {
i.directory = append(i.directory, text)
} else {
i.file = append(i.file, text)
}
}

return nil
}

0 comments on commit 8f01113

Please sign in to comment.