finished remote action
df7db726
1 file(s) · +56 −12
| 7 | 7 | "io/ioutil" |
|
| 8 | 8 | "net/http" |
|
| 9 | 9 | "os" |
|
| 10 | + | "os/exec" |
|
| 10 | 11 | "strings" |
|
| 11 | 12 | ||
| 12 | 13 | "github.com/AlecAivazis/survey/v2" |
|
| 14 | + | "github.com/fatih/color" |
|
| 13 | 15 | ) |
|
| 14 | 16 | ||
| 15 | 17 | func RemoteClone() error { |
|
| 16 | - | fmt.Print("Enter the name of GitHub user or org") |
|
| 18 | + | fmt.Print("Enter the name of GitHub user or org: ") |
|
| 17 | 19 | ||
| 18 | 20 | reader := bufio.NewReader(os.Stdin) |
|
| 19 | 21 | input, err := reader.ReadString('\n') |
|
| 20 | 22 | if err != nil { |
|
| 21 | - | fmt.Println("An error occured while reading the input. Please try again", err) |
|
| 23 | + | fmt.Println("An error occurred while reading the input. Please try again", err) |
|
| 22 | 24 | return err |
|
| 23 | 25 | } |
|
| 24 | 26 | input = strings.TrimSuffix(input, "\n") |
|
| 27 | 29 | ||
| 28 | 30 | response, err := http.Get(apiURL) |
|
| 29 | 31 | if err != nil { |
|
| 30 | - | fmt.Println("Github API request failed with error", err) |
|
| 32 | + | fmt.Println("GitHub API request failed with error", err) |
|
| 31 | 33 | return err |
|
| 32 | 34 | } |
|
| 35 | + | defer response.Body.Close() |
|
| 33 | 36 | ||
| 34 | 37 | body, err := ioutil.ReadAll(response.Body) |
|
| 35 | 38 | if err != nil { |
|
| 44 | 47 | return err |
|
| 45 | 48 | } |
|
| 46 | 49 | ||
| 47 | - | newRepos := selectNewRepos(repos) |
|
| 48 | - | ||
| 49 | - | for _, repo := range newRepos { |
|
| 50 | - | fmt.Printf("Repository: %s\n", repo.FullName) |
|
| 51 | - | fmt.Printf("Description: %s\n", repo.Description) |
|
| 52 | - | fmt.Printf("URL: %s\n", repo.HTMLURL) |
|
| 53 | - | fmt.Printf("---\n") |
|
| 54 | - | } |
|
| 50 | + | selectedRepos := selectNewRepos(repos) |
|
| 51 | + | confirmAndCloneRepos(selectedRepos) |
|
| 55 | 52 | ||
| 56 | 53 | return nil |
|
| 57 | 54 | } |
|
| 64 | 61 | ||
| 65 | 62 | var selected []string |
|
| 66 | 63 | prompt := &survey.MultiSelect{ |
|
| 67 | - | Message: "Select repositories to initialize:", |
|
| 64 | + | Message: "Select repositories to clone and initialize:", |
|
| 68 | 65 | Options: options, |
|
| 69 | 66 | } |
|
| 70 | 67 | survey.AskOne(prompt, &selected) |
|
| 81 | 78 | ||
| 82 | 79 | return selectedRepos |
|
| 83 | 80 | } |
|
| 81 | + | ||
| 82 | + | func confirmAndCloneRepos(repos []Repository) { |
|
| 83 | + | confirm := false |
|
| 84 | + | prompt := &survey.Confirm{ |
|
| 85 | + | Message: fmt.Sprintf("Clone and initialize %d repositories?", len(repos)), |
|
| 86 | + | } |
|
| 87 | + | survey.AskOne(prompt, &confirm) |
|
| 88 | + | ||
| 89 | + | if !confirm { |
|
| 90 | + | fmt.Println("Radicalization cancelled.") |
|
| 91 | + | return |
|
| 92 | + | } |
|
| 93 | + | ||
| 94 | + | fmt.Printf("Cloning and initializing %d repositories...\n", len(repos)) |
|
| 95 | + | ||
| 96 | + | for i, repo := range repos { |
|
| 97 | + | fmt.Printf("Processing %s (%d/%d)...\n", repo.Name, i+1, len(repos)) |
|
| 98 | + | ||
| 99 | + | // Clone the repository |
|
| 100 | + | err := cloneRepo(repo.CloneURL, repo.Name) |
|
| 101 | + | if err != nil { |
|
| 102 | + | color.Red("Error cloning %s: %v\n", repo.Name, err) |
|
| 103 | + | continue |
|
| 104 | + | } |
|
| 105 | + | ||
| 106 | + | // Initialize with rad |
|
| 107 | + | err = runRadInitRemote(repo.Name, repo.Name) |
|
| 108 | + | if err != nil { |
|
| 109 | + | color.Red("Error initializing %s: %v\n", repo.Name, err) |
|
| 110 | + | } else { |
|
| 111 | + | color.Green("Cloned and initialized %s\n", repo.Name) |
|
| 112 | + | } |
|
| 113 | + | } |
|
| 114 | + | ||
| 115 | + | fmt.Println("Radicalization Complete") |
|
| 116 | + | } |
|
| 117 | + | ||
| 118 | + | func cloneRepo(url, name string) error { |
|
| 119 | + | cmd := exec.Command("git", "clone", url, name) |
|
| 120 | + | return cmd.Run() |
|
| 121 | + | } |
|
| 122 | + | ||
| 123 | + | func runRadInitRemote(path, name string) error { |
|
| 124 | + | cmd := exec.Command("rad", "init", "--name", name, "--description", "", "--public", "--no-confirm") |
|
| 125 | + | cmd.Dir = path |
|
| 126 | + | return cmd.Run() |
|
| 127 | + | } |
|