From 20817b9736d44c64bb918459df73f99619e31cc6 Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Sun, 31 Jul 2022 17:20:53 -0700 Subject: [PATCH] Unset envs in ghorg reclone --- cmd/reclone.go | 35 ++++++++++++++++++++++++----------- cmd/root.go | 3 ++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/cmd/reclone.go b/cmd/reclone.go index bcb882d..a266c8e 100644 --- a/cmd/reclone.go +++ b/cmd/reclone.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io/ioutil" + "os" "os/exec" "strings" @@ -58,14 +59,15 @@ func reCloneFunc(cmd *cobra.Command, argz []string) { } func printFinalOutput(argz []string, reCloneMap map[string]ReClone) { + fmt.Println("") + fmt.Println("Completed! The following reclones were ran...") if len(argz) == 0 { - for key, value := range reCloneMap { - fmt.Printf("%v: %v\n", key, value) + for key, _ := range reCloneMap { + fmt.Printf(" * %v\n", key) } } else { for _, arg := range argz { - key, value := reCloneMap[arg] - fmt.Printf("%v: %v\n", key, value) + fmt.Printf(" * %v\n", arg) } } } @@ -80,13 +82,25 @@ func runReClone(rc ReClone) { } ghorgClone := exec.Command("ghorg", remainingCommand...) - stdout, err := ghorgClone.StdoutPipe() - if err != nil { - e := fmt.Sprintf("ERROR: Problem with piping to stdout, err: %v", err) - colorlog.PrintErrorAndExit(e) + // have to unset all ghorg envs because root command will set them on initialization of ghorg cmd + for _, e := range os.Environ() { + keyValue := strings.SplitN(e, "=", 2) + ghorgEnv := strings.HasPrefix(keyValue[0], "GHORG_") + if ghorgEnv { + os.Unsetenv(keyValue[0]) + } } - ghorgClone.Start() + stdout, err := ghorgClone.StdoutPipe() + if err != nil { + fmt.Printf("ERROR: Problem with piping to stdout, err: %v", err) + } + + err = ghorgClone.Start() + + if err != nil { + fmt.Printf("ERROR: Starting ghorg clone cmd: %v, err: %v", rc.Cmd, err) + } scanner := bufio.NewScanner(stdout) for scanner.Scan() { @@ -96,7 +110,6 @@ func runReClone(rc ReClone) { err = ghorgClone.Wait() if err != nil { - e := fmt.Sprintf("ERROR: Running ghorg clone cmd: %v, err: %v", rc.Cmd, err) - colorlog.PrintErrorAndExit(e) + fmt.Printf("ERROR: Running ghorg clone cmd: %v, err: %v", rc.Cmd, err) } } diff --git a/cmd/root.go b/cmd/root.go index 40079f9..d416d11 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -269,9 +269,10 @@ func init() { cloneCmd.Flags().StringVarP(&excludeMatchRegex, "exclude-match-regex", "", "", "GHORG_EXCLUDE_MATCH_REGEX - Exclude cloning repos that match name to regex provided") cloneCmd.Flags().StringVarP(&gitlabGroupExcludeMatchRegex, "gitlab-group-exclude-match-regex", "", "", "GHORG_GITLAB_GROUP_EXCLUDE_MATCH_REGEX - Exclude cloning gitlab groups that match name to regex provided") cloneCmd.Flags().StringVarP(&ghorgIgnorePath, "ghorgignore-path", "", "", "GHORG_IGNORE_PATH - If you want to set a path other than $HOME/.config/ghorg/ghorgignore for your ghorgignore") - cloneCmd.Flags().StringVarP(&ghorgReClonePath, "reclone-path", "", "", "GHORG_RECLONE_PATH - If you want to set a path other than $HOME/.config/ghorg/reclone.yaml for your reclone configuration") cloneCmd.Flags().StringVarP(&exitCodeOnCloneInfos, "exit-code-on-clone-infos", "", "", "GHORG_EXIT_CODE_ON_CLONE_INFOS - Allows you to control the exit code when ghorg runs into a problem (info level message) cloning a repo from the remote. Info messages will appear after a clone is complete, similar to success messages. (default 0)") cloneCmd.Flags().StringVarP(&exitCodeOnCloneIssues, "exit-code-on-clone-issues", "", "", "GHORG_EXIT_CODE_ON_CLONE_ISSUES - Allows you to control the exit code when ghorg runs into a problem (issue level message) cloning a repo from the remote. Issue messages will appear after a clone is complete, similar to success messages (default 1)") + + reCloneCmd.Flags().StringVarP(&ghorgReClonePath, "reclone-path", "", "", "GHORG_RECLONE_PATH - If you want to set a path other than $HOME/.config/ghorg/reclone.yaml for your reclone configuration") rootCmd.AddCommand(lsCmd, versionCmd, cloneCmd, reCloneCmd) }