-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ghorg reclone command
- Loading branch information
Showing
10 changed files
with
181 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/gabrie30/ghorg/colorlog" | ||
"github.com/gabrie30/ghorg/configs" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var reCloneCmd = &cobra.Command{ | ||
Use: "reclone", | ||
Short: "Reruns one, multiple, or all preconfigured clones from configuration set in $HOME/.config/ghorg/reclone.yaml", | ||
Long: `Allows you to set preconfigured clone commands for handling multiple users/orgs at once. See README.md at https://github.com/gabrie30/ghorg for setup information.`, | ||
Run: reCloneFunc, | ||
} | ||
|
||
type ReClone struct { | ||
Cmd string `yaml:"cmd"` | ||
} | ||
|
||
func reCloneFunc(cmd *cobra.Command, argz []string) { | ||
path := configs.GhorgReCloneLocation() | ||
yamlBytes, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
e := fmt.Sprintf("ERROR: parsing reclone.yaml, error: %v", err) | ||
colorlog.PrintErrorAndExit(e) | ||
} | ||
|
||
mapOfReClones := make(map[string]ReClone) | ||
|
||
err = yaml.Unmarshal(yamlBytes, &mapOfReClones) | ||
if err != nil { | ||
e := fmt.Sprintf("ERROR: unmarshaling reclone.yaml, error:%v", err) | ||
colorlog.PrintErrorAndExit(e) | ||
} | ||
|
||
if len(argz) == 0 { | ||
for _, key := range mapOfReClones { | ||
runReClone(key) | ||
} | ||
} else { | ||
for _, arg := range argz { | ||
if _, ok := mapOfReClones[arg]; !ok { | ||
e := fmt.Sprintf("ERROR: The key %v was not found in reclone.yaml", arg) | ||
colorlog.PrintErrorAndExit(e) | ||
} | ||
runReClone(mapOfReClones[arg]) | ||
} | ||
} | ||
|
||
} | ||
|
||
func runReClone(rc ReClone) { | ||
// make sure command starts with ghorg clone | ||
splitCommand := strings.Split(rc.Cmd, " ") | ||
ghorg, clone, remainingCommand := splitCommand[0], splitCommand[1], splitCommand[1:] | ||
|
||
if ghorg != "ghorg" || clone != "clone" { | ||
colorlog.PrintErrorAndExit("ERROR: Only ghorg clone commands are permitted in your reclone.yaml") | ||
} | ||
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) | ||
} | ||
|
||
err = ghorgClone.Start() | ||
|
||
if err != nil { | ||
e := fmt.Sprintf("ERROR: Running ghorg clone cmd: %v, err: %v", rc.Cmd, err) | ||
colorlog.PrintErrorAndExit(e) | ||
} | ||
|
||
scanner := bufio.NewScanner(stdout) | ||
for scanner.Scan() { | ||
m := scanner.Text() | ||
fmt.Println(m) | ||
} | ||
ghorgClone.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Example format below. You can reclone, one, multiple or all at once just use with no arguments e.g. ghorg reclone | ||
|
||
# name-of-reclone: | ||
# cmd: "ghorg clone command here" | ||
# backup-my-org: | ||
# cmd: "ghorg clone my-org --backup --clone-wiki" | ||
# kubernetes-sig: | ||
# cmd: "ghorg clone kubernetes --match-regex=^sig- --output-dir=kubernetes-sig-only" | ||
# gitlab-examples: | ||
# cmd: "ghorg clone gitlab-examples --scm=gitlab --preserve-dir --config=/Users/me/.config/ghorg/gitlab-cloud-conf.yaml" |