github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/browser.go (about)

     1  package browser
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/ismailbayram/bigpicture/internal/graph"
     6  	"regexp"
     7  	"strings"
     8  )
     9  
    10  const (
    11  	LangGo   = "go"
    12  	LangPy   = "py"
    13  	LangJava = "java"
    14  )
    15  
    16  type Browser interface {
    17  	Browse(parentPath string)
    18  }
    19  
    20  func NewBrowser(lang string, tree *graph.Tree, ignoredPaths []string, rootDir string) Browser {
    21  	switch lang {
    22  	case LangGo:
    23  		return &GoBrowser{
    24  			ignoredPaths: ignoredPaths,
    25  			tree:         tree,
    26  		}
    27  	case LangPy:
    28  		return &PythonBrowser{
    29  			ignoredPaths: ignoredPaths,
    30  			tree:         tree,
    31  			rootDir:      rootDir,
    32  		}
    33  	case LangJava:
    34  		return &JavaBrowser{
    35  			ignoredPaths: ignoredPaths,
    36  			tree:         tree,
    37  			rootDir:      rootDir,
    38  		}
    39  	}
    40  	return nil
    41  }
    42  
    43  func isIgnored(ignoredPaths []string, entryPath string) bool {
    44  	isIgnored := false
    45  	for _, ignore := range ignoredPaths {
    46  		regxp := ignore
    47  		if strings.HasPrefix(ignore, "*") {
    48  			regxp = fmt.Sprintf("^%s$", ignore)
    49  		}
    50  		re := regexp.MustCompile(regxp)
    51  		if re.MatchString(entryPath) {
    52  			isIgnored = true
    53  			break
    54  		}
    55  	}
    56  
    57  	return isIgnored
    58  }