Add/matchregex (#137)

* Add regex match to filter repos based on regex
This commit is contained in:
Jay Gabriels
2021-08-05 21:50:47 -07:00
committed by GitHub
parent afa80cf95b
commit 39a285bcb9
4 changed files with 39 additions and 1 deletions

View File

@@ -9,8 +9,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
### Added
- integration tests on windows, ubuntu, and mac for github
- GHORG_MATCH_REGEX to filter cloning repos by regex; thanks @petomalina
### Changed
- initial clone will try to checkout a branch if specified thanks @dword-design
- initial clone will try to checkout a branch if specified; thanks @dword-design
- default clone directory to $HOME/ghorg
- users/orgs directory no longer appends "\_ghorg" or forces underscores
### Deprecated

View File

@@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
@@ -41,6 +42,7 @@ var (
cloneInfos []string
targetCloneSource string
matchPrefix string
matchRegex string
)
func init() {
@@ -62,6 +64,7 @@ func init() {
cloneCmd.Flags().StringVarP(&topics, "topics", "", "", "GHORG_TOPICS - comma separated list of github/gitea topics to filter for")
cloneCmd.Flags().StringVarP(&outputDir, "output-dir", "", "", "GHORG_OUTPUT_DIR - name of directory repos will be cloned into (default name of org/repo being cloned")
cloneCmd.Flags().StringVarP(&matchPrefix, "match-prefix", "", "", "GHORG_MATCH_PREFIX - only clone repos with matching prefix, can be a comma separated list (default \"\")")
cloneCmd.Flags().StringVarP(&matchRegex, "match-regex", "", "", "GHORG_MATCH_REGEX - only clone repos that match name to regex provided")
}
var cloneCmd = &cobra.Command{
@@ -131,6 +134,11 @@ func cloneFunc(cmd *cobra.Command, argz []string) {
os.Setenv("GHORG_MATCH_PREFIX", prefix)
}
if cmd.Flags().Changed("match-regex") {
regex := cmd.Flag("match-regex").Value.String()
os.Setenv("GHORG_MATCH_REGEX", regex)
}
if cmd.Flags().Changed("skip-archived") {
os.Setenv("GHORG_SKIP_ARCHIVED", "true")
}
@@ -285,6 +293,21 @@ func readGhorgIgnore() ([]string, error) {
return lines, scanner.Err()
}
func filterByRegex(repos []scm.Repo) []scm.Repo {
filteredRepos := []scm.Repo{}
regex := fmt.Sprintf(`%s`, os.Getenv("GHORG_MATCH_REGEX"))
for i, r := range repos {
re := regexp.MustCompile(regex)
match := re.FindString(getAppNameFromURL(r.URL))
if match != "" {
filteredRepos = append(filteredRepos, repos[i])
}
}
return filteredRepos
}
// CloneAllRepos clones all repos
func CloneAllRepos() {
// resc, errc, infoc := make(chan string), make(chan error), make(chan error)
@@ -312,6 +335,12 @@ func CloneAllRepos() {
os.Exit(0)
}
if os.Getenv("GHORG_MATCH_REGEX") != "" {
colorlog.PrintInfo("Filtering repos down by regex that match the provided...")
fmt.Println("")
cloneTargets = filterByRegex(cloneTargets)
}
// filter repos down based on ghorgignore if one exists
_, err = os.Stat(configs.GhorgIgnoreLocation())
if !os.IsNotExist(err) {
@@ -524,6 +553,9 @@ func PrintConfigs() {
if configs.GhorgIgnoreDetected() == true {
colorlog.PrintInfo("* Ghorgignore : true")
}
if os.Getenv("GHORG_MATCH_REGEX") != "" {
colorlog.PrintInfo("* Regex Match : " + os.Getenv("GHORG_MATCH_REGEX"))
}
if os.Getenv("GHORG_OUTPUT_DIR") != "" {
colorlog.PrintInfo("* Output Dir : " + parentFolder)
}

View File

@@ -96,6 +96,7 @@ func initConfig() {
getOrSetDefaults("GHORG_SCM_BASE_URL")
getOrSetDefaults("GHORG_PRESERVE_DIRECTORY_STRUCTURE")
getOrSetDefaults("GHORG_OUTPUT_DIR")
getOrSetDefaults("GHORG_MATCH_REGEX")
}
// Load triggers the configs to load first, not sure if this is actually needed

View File

@@ -120,3 +120,7 @@ GHORG_OUTPUT_DIR:
# default: ""
# flag (--match-prefix)
GHORG_MATCH_PREFIX:
# Only clone repos that match name to regex provided
# flag (--match-regex)
GHORG_MATCH_REGEX: