Skip to content

Commit

Permalink
feat(finder): add finder
Browse files Browse the repository at this point in the history
  • Loading branch information
anarcher committed Mar 26, 2020
1 parent 81dd7b2 commit 29679ca
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
116 changes: 116 additions & 0 deletions pkg/finder/gitchanged.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package finder

import (
"fmt"
"path/filepath"
"strings"

"github.com/anarcher/kustomize-check-action/pkg/command"
"github.com/anarcher/kustomize-check-action/pkg/config"
"github.com/anarcher/kustomize-check-action/pkg/utils"
)

type GitChanged struct {
base string
cfg *config.Config
cmd command.Commander
}

const (
gitDiffTreeFmt = "diff-tree --no-commit-id --name-only -r %s"
kustomizeFileName = "kustomization.yaml"
)

func NewGitChanged(base string, cfg *config.Config, cmd command.Commander) (*GitChanged, error) {

pf := &GitChanged{
base: base,
cfg: cfg,
cmd: cmd,
}

return pf, nil
}

func (f *GitChanged) PathFind() ([]string, error) {

paths, err := f.gitDiffTree()
if err != nil {
return nil, err
}

paths = f.ensureDir(paths)
paths = f.filterKustomize(paths)
paths, err = f.matchPaths(paths)
if err != nil {
return nil, err
}

return paths, nil
}

func (f *GitChanged) gitDiffTree() ([]string, error) {
cmt := f.cfg.GitHub.Commit
args := fmt.Sprintf(gitDiffTreeFmt, cmt)

out, err := f.cmd.Run("git", strings.Split(args, " ")...)
if err != nil {
return nil, err
}
paths := strings.Split(out, "\n")
return paths, nil
}

func (f *GitChanged) ensureDir(paths []string) []string {
pm := make(map[string]bool)
for _, p := range paths {
if len(p) == 0 {
continue
}
d := filepath.Dir(p)
if ok := pm[d]; !ok {
pm[d] = true
}
}

ps := make([]string, 0, len(pm))
for p := range pm {
ps = append(ps, p)
}
return ps
}

func (f *GitChanged) filterKustomize(paths []string) []string {
var ps []string
for _, p := range paths {
fn := fmt.Sprintf("%s/%s", p, kustomizeFileName)
if utils.FileExists(fn) {
ps = append(ps, p)
}
}
return ps
}

func (f *GitChanged) matchPaths(paths []string) ([]string, error) {
patterns := f.cfg.Inputs.Paths
if len(patterns) == 0 {
return paths, nil
}

var ps []string

for _, p := range paths {
for _, pat := range patterns {
matched, err := filepath.Match(pat, p)
if err != nil {
return nil, err
}
if matched {
ps = append(ps, p)
break
}
}
}

return ps, nil
}
5 changes: 5 additions & 0 deletions pkg/finder/pathfinder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package finder

type PathFinder interface {
PathFind() ([]string, error)
}

0 comments on commit 29679ca

Please sign in to comment.