diff --git a/CHANGELOG.md b/CHANGELOG.md index eee8fd2..74f7d2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cmd/clone.go b/cmd/clone.go index d86a094..22d1e34 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -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) } diff --git a/configs/configs.go b/configs/configs.go index 27f1955..0a366d0 100644 --- a/configs/configs.go +++ b/configs/configs.go @@ -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 diff --git a/sample-conf.yaml b/sample-conf.yaml index 8dfd9c3..5a4717b 100644 --- a/sample-conf.yaml +++ b/sample-conf.yaml @@ -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: