From 440e7fecfc1048739779b25841c926f8b0988616 Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Tue, 23 Jan 2024 22:44:57 -0800 Subject: [PATCH] Add GHORG_TARGET_REPOS_PATH --- CHANGELOG.md | 9 ++++++++ cmd/clone.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 3 +++ cmd/version.go | 2 +- sample-conf.yaml | 5 ++++ 5 files changed, 78 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8e495ea8c..906624f2bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +## [1.9.11] - Unreleased +### Added +- GHORG_TARGET_REPOS_PATH +### Changed +### Deprecated +### Removed +### Fixed +### Security + ## [1.9.10] - 1/21/24 ### Added - Automated docker images (#354); thanks @afonsoc12 diff --git a/cmd/clone.go b/cmd/clone.go index 394865a7ef..3fc8e35e86 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -139,6 +139,11 @@ func cloneFunc(cmd *cobra.Command, argz []string) { os.Setenv("GHORG_IGNORE_PATH", path) } + if cmd.Flags().Changed("target-repos-path") { + path := cmd.Flag("target-repos-path").Value.String() + os.Setenv("GHORG_TARGET_REPOS_PATH", path) + } + if cmd.Flags().Changed("git-filter") { filter := cmd.Flag("git-filter").Value.String() os.Setenv("GHORG_GIT_FILTER", filter) @@ -353,6 +358,23 @@ func printRemainingMessages() { } } +func readTargetReposFile() ([]string, error) { + file, err := os.Open(os.Getenv("GHORG_TARGET_REPOS_PATH")) + if err != nil { + return nil, err + } + defer file.Close() + + var lines []string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + if scanner.Text() != "" { + lines = append(lines, scanner.Text()) + } + } + return lines, scanner.Err() +} + func readGhorgIgnore() ([]string, error) { file, err := os.Open(configs.GhorgIgnoreLocation()) if err != nil { @@ -538,6 +560,44 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { cloneTargets = filterByExcludeMatchPrefix(cloneTargets) } + // filter down repos based on target repos flag file path + if os.Getenv("GHORG_TARGET_REPOS_PATH") != "" { + _, err := os.Stat(os.Getenv("GHORG_TARGET_REPOS_PATH")) + + if err != nil { + colorlog.PrintErrorAndExit(fmt.Sprintf("Error finding your GHORG_TARGET_REPOS_PATH file, error: %v", err)) + } + + if !os.IsNotExist(err) { + // Open the file parse each line and remove cloneTargets containing + toTarget, err := readTargetReposFile() + if err != nil { + colorlog.PrintErrorAndExit(fmt.Sprintf("Error parsing your GHORG_TARGET_REPOS_PATH file, error: %v", err)) + } + + colorlog.PrintInfo("Using GHORG_TARGET_REPOS_PATH, filtering repos down...") + + filteredCloneTargets := []scm.Repo{} + var flag bool + for _, cloned := range cloneTargets { + flag = false + for _, targetRepo := range toTarget { + clonedRepoName := strings.TrimSuffix(filepath.Base(cloned.URL), ".git") + if strings.EqualFold(clonedRepoName, targetRepo) { + flag = true + } + } + + if flag { + filteredCloneTargets = append(filteredCloneTargets, cloned) + } + } + + cloneTargets = filteredCloneTargets + + } + } + // filter repos down based on ghorgignore if one exists _, err := os.Stat(configs.GhorgIgnoreLocation()) if !os.IsNotExist(err) { diff --git a/cmd/root.go b/cmd/root.go index 98d39d68b3..f9c03017e2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -40,6 +40,7 @@ var ( config string gitlabGroupExcludeMatchRegex string ghorgIgnorePath string + targetReposPath string ghorgReClonePath string githubAppID string githubAppPemPath string @@ -233,6 +234,7 @@ func InitConfig() { getOrSetDefaults("GHORG_EXIT_CODE_ON_CLONE_INFOS") getOrSetDefaults("GHORG_EXIT_CODE_ON_CLONE_ISSUES") // Optionally set + getOrSetDefaults("GHORG_TARGET_REPOS_PATH") getOrSetDefaults("GHORG_CLONE_DEPTH") getOrSetDefaults("GHORG_GITHUB_TOKEN") getOrSetDefaults("GHORG_COLOR") @@ -278,6 +280,7 @@ func init() { _ = viper.BindPFlag("color", rootCmd.PersistentFlags().Lookup("color")) _ = viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) + cloneCmd.Flags().StringVar(&targetReposPath, "target-repos-path", "", "GHORG_TARGET_REPOS_PATH - Path to file with list of repo names to clone, file should contain one repo name per line") cloneCmd.Flags().StringVar(&protocol, "protocol", "", "GHORG_CLONE_PROTOCOL - Protocol to clone with, ssh or https, (default https)") cloneCmd.Flags().StringVarP(&path, "path", "p", "", "GHORG_ABSOLUTE_PATH_TO_CLONE_TO - Absolute path to the home for ghorg clones. Must start with / (default $HOME/ghorg)") cloneCmd.Flags().StringVarP(&branch, "branch", "b", "", "GHORG_BRANCH - Branch left checked out for each repo cloned (default master)") diff --git a/cmd/version.go b/cmd/version.go index c5a8db4e91..f7805a6f37 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" ) -const ghorgVersion = "v1.9.10" +const ghorgVersion = "v1.9.11" var versionCmd = &cobra.Command{ Use: "version", diff --git a/sample-conf.yaml b/sample-conf.yaml index 69e5055ae4..a2b78db011 100644 --- a/sample-conf.yaml +++ b/sample-conf.yaml @@ -81,6 +81,11 @@ GHORG_CONCURRENCY: 25 # flag (--clone-depth) eg: --clone-depth=1 GHORG_CLONE_DEPTH: +# Path to file with list of repo names to clone, file should contain one repo name per line +# This allows you to only clone a specific list of repos +# flag (--target-repos-path) +GHORG_TARGET_REPOS_PATH: + # A comma separated list of topics to filter by, currently github/gitlab/gitea only # flag (--topics) eg: --topics=frontend # If any topics exist here, ghorg will only clone repos that match at least one of these topics