Add support for GHORG_IGNORE_PATH (#194)

Enable users to pass a path to an ignore file via the
environment/config variable at GHORG_IGNORE_PATH.
This commit is contained in:
Jeffrey Lo
2022-03-15 13:31:24 -04:00
committed by GitHub
parent 174fe43b5d
commit d58e64a910
3 changed files with 12 additions and 14 deletions

View File

@@ -28,7 +28,6 @@ var cloneCmd = &cobra.Command{
}
func cloneFunc(cmd *cobra.Command, argz []string) {
if cmd.Flags().Changed("path") {
absolutePath := configs.EnsureTrailingSlashOnFilePath((cmd.Flag("path").Value.String()))
os.Setenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO", absolutePath)
@@ -160,7 +159,6 @@ func cloneFunc(cmd *cobra.Command, argz []string) {
} else {
os.Setenv("GHORG_BITBUCKET_OAUTH_TOKEN", cmd.Flag("token").Value.String())
}
} else if os.Getenv("GHORG_SCM_TYPE") == "gitea" {
os.Setenv("GHORG_GITEA_TOKEN", cmd.Flag("token").Value.String())
}
@@ -241,7 +239,7 @@ func getCloneUrls(isOrg bool) ([]scm.Repo, error) {
func createDirIfNotExist() {
if _, err := os.Stat(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO") + parentFolder); os.IsNotExist(err) {
err = os.MkdirAll(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), 0700)
err = os.MkdirAll(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), 0o700)
if err != nil {
panic(err)
}
@@ -393,7 +391,6 @@ func printDryRun(repos []scm.Repo) {
// CloneAllRepos clones all repos
func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
// Filter repos that have attributes that don't need specific scm api calls
if os.Getenv("GHORG_MATCH_REGEX") != "" {
colorlog.PrintInfo("Filtering repos down by including regex matches...")
@@ -468,7 +465,6 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
createDirIfNotExist()
l, err := strconv.Atoi(os.Getenv("GHORG_CONCURRENCY"))
if err != nil {
log.Fatal("Could not determine GHORG_CONCURRENCY")
}
@@ -534,7 +530,6 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
} else {
err := git.Checkout(repo)
if err != nil {
e := fmt.Sprintf("Could not checkout out %s, branch may not exist, no changes made Repo: %s Error: %v", repo.CloneBranch, repo.URL, err)
cloneInfos = append(cloneInfos, e)
@@ -687,7 +682,7 @@ func PrintConfigs() {
colorlog.PrintInfo("* Wikis : " + os.Getenv("GHORG_CLONE_WIKI"))
}
if configs.GhorgIgnoreDetected() {
colorlog.PrintInfo("* Ghorgignore : true")
colorlog.PrintInfo("* Ghorgignore : " + configs.GhorgIgnoreLocation())
}
if os.Getenv("GHORG_MATCH_REGEX") != "" {
colorlog.PrintInfo("* Regex Match : " + os.Getenv("GHORG_MATCH_REGEX"))

View File

@@ -57,7 +57,6 @@ var rootCmd = &cobra.Command{
// reads in configuration file and updates anything not set to default
func getOrSetDefaults(envVar string) {
if envVar == "GHORG_COLOR" {
if color == "enabled" {
os.Setenv("GHORG_COLOR", "enabled")
@@ -80,6 +79,8 @@ func getOrSetDefaults(envVar string) {
switch envVar {
case "GHORG_ABSOLUTE_PATH_TO_CLONE_TO":
os.Setenv(envVar, configs.GetAbsolutePathToCloneTo())
case "GHORG_IGNORE_PATH":
os.Setenv(envVar, configs.GhorgIgnoreLocation())
case "GHORG_CLONE_PROTOCOL":
os.Setenv(envVar, "https")
case "GHORG_CLONE_TYPE":
@@ -184,13 +185,13 @@ func InitConfig() {
getOrSetDefaults("GHORG_MATCH_PREFIX")
getOrSetDefaults("GHORG_EXCLUDE_MATCH_PREFIX")
getOrSetDefaults("GHORG_GITLAB_GROUP_EXCLUDE_MATCH_REGEX")
getOrSetDefaults("GHORG_IGNORE_PATH")
if os.Getenv("GHORG_DEBUG") != "" {
viper.Debug()
fmt.Println("Viper config file used:", viper.ConfigFileUsed())
fmt.Printf("GHORG_CONFIG SET TO: %s\n", os.Getenv("GHORG_CONFIG"))
}
}
func init() {

View File

@@ -102,16 +102,18 @@ func GetCorrectFilePathSeparator() string {
// GhorgIgnoreLocation returns the path of users ghorgignore
func GhorgIgnoreLocation() string {
ignoreLocation := os.Getenv("GHORG_IGNORE_PATH")
if ignoreLocation != "" {
return ignoreLocation
}
return filepath.Join(GhorgDir(), "ghorgignore")
}
// GhorgIgnoreDetected identify if a ghorgignore file exists in users .config/ghorg directory
// GhorgIgnoreDetected returns true if a ghorgignore file exists.
func GhorgIgnoreDetected() bool {
_, err := os.Stat(GhorgIgnoreLocation())
if os.IsNotExist(err) {
return false
}
return true
return !os.IsNotExist(err)
}
// GhorgDir returns the ghorg directory path