|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/javaBin/javabin-cli/internal/config" |
| 12 | + gh "github.com/javaBin/javabin-cli/internal/github" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var registerCmd = &cobra.Command{ |
| 17 | + Use: "register", |
| 18 | + Short: "Register a new app with the Javabin platform", |
| 19 | + Long: "Interactive wizard that creates a registration PR against javaBin/registry.", |
| 20 | + RunE: runRegister, |
| 21 | +} |
| 22 | + |
| 23 | +func runRegister(cmd *cobra.Command, args []string) error { |
| 24 | + reader := bufio.NewReader(os.Stdin) |
| 25 | + prompt := func(label, defaultVal string) string { |
| 26 | + if defaultVal != "" { |
| 27 | + fmt.Printf("%s [%s]: ", label, defaultVal) |
| 28 | + } else { |
| 29 | + fmt.Printf("%s: ", label) |
| 30 | + } |
| 31 | + input, _ := reader.ReadString('\n') |
| 32 | + input = strings.TrimSpace(input) |
| 33 | + if input == "" { |
| 34 | + return defaultVal |
| 35 | + } |
| 36 | + return input |
| 37 | + } |
| 38 | + |
| 39 | + token, err := gh.GetToken() |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("GitHub auth required: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + // Repo name |
| 45 | + repoName := prompt("Repository name (e.g. moresleep)", "") |
| 46 | + if repoName == "" { |
| 47 | + return fmt.Errorf("repository name is required") |
| 48 | + } |
| 49 | + |
| 50 | + // Validate repo exists |
| 51 | + fmt.Printf("Checking javaBin/%s exists... ", repoName) |
| 52 | + if !repoExists(token, repoName) { |
| 53 | + fmt.Println("not found") |
| 54 | + return fmt.Errorf("repository javaBin/%s does not exist", repoName) |
| 55 | + } |
| 56 | + fmt.Println("ok") |
| 57 | + |
| 58 | + // List teams from registry |
| 59 | + fmt.Println("\nAvailable teams:") |
| 60 | + teams, err := listTeams(token) |
| 61 | + if err != nil { |
| 62 | + fmt.Printf(" (could not fetch teams: %v)\n", err) |
| 63 | + } else { |
| 64 | + for _, t := range teams { |
| 65 | + fmt.Printf(" - %s\n", t) |
| 66 | + } |
| 67 | + } |
| 68 | + team := prompt("\nTeam", "") |
| 69 | + if team == "" { |
| 70 | + return fmt.Errorf("team is required") |
| 71 | + } |
| 72 | + |
| 73 | + // Auth |
| 74 | + fmt.Println("\nAuth options: internal, external, both, none") |
| 75 | + auth := prompt("Auth", "none") |
| 76 | + |
| 77 | + // Budget |
| 78 | + budget := prompt("Monthly budget (NOK)", "1000") |
| 79 | + |
| 80 | + // Dev environment |
| 81 | + devEnv := prompt("Need a dev environment? (y/n)", "n") |
| 82 | + |
| 83 | + // Confirm |
| 84 | + fmt.Println("\n--- Registration Summary ---") |
| 85 | + fmt.Printf(" Repo: javaBin/%s\n", repoName) |
| 86 | + fmt.Printf(" Team: %s\n", team) |
| 87 | + fmt.Printf(" Auth: %s\n", auth) |
| 88 | + fmt.Printf(" Budget: %s NOK\n", budget) |
| 89 | + if strings.ToLower(devEnv) == "y" { |
| 90 | + fmt.Println(" Dev: yes") |
| 91 | + } |
| 92 | + fmt.Println() |
| 93 | + |
| 94 | + confirm := prompt("Create registration PR? (y/n)", "y") |
| 95 | + if strings.ToLower(confirm) != "y" { |
| 96 | + fmt.Println("Cancelled.") |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + // Build app YAML content |
| 101 | + var yamlLines []string |
| 102 | + yamlLines = append(yamlLines, fmt.Sprintf("name: %s", repoName)) |
| 103 | + yamlLines = append(yamlLines, fmt.Sprintf("team: %s", team)) |
| 104 | + yamlLines = append(yamlLines, fmt.Sprintf("repo: javaBin/%s", repoName)) |
| 105 | + if auth != "none" && auth != "" { |
| 106 | + yamlLines = append(yamlLines, fmt.Sprintf("auth: %s", auth)) |
| 107 | + } |
| 108 | + if budget != "1000" && budget != "" { |
| 109 | + yamlLines = append(yamlLines, fmt.Sprintf("budget_alert_nok: %s", budget)) |
| 110 | + } |
| 111 | + yamlContent := strings.Join(yamlLines, "\n") + "\n" |
| 112 | + |
| 113 | + // Create PR via GitHub API |
| 114 | + filePath := fmt.Sprintf("apps/%s.yaml", repoName) |
| 115 | + branchName := fmt.Sprintf("register-%s", repoName) |
| 116 | + prTitle := fmt.Sprintf("Register %s", repoName) |
| 117 | + prBody := fmt.Sprintf("Register `javaBin/%s` with team `%s`.\n\nCreated by `javabin register`.", repoName, team) |
| 118 | + |
| 119 | + prURL, err := gh.CreateRegistrationPR(token, branchName, filePath, yamlContent, prTitle, prBody) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed to create PR: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + fmt.Printf("\nRegistration PR created: %s\n", prURL) |
| 125 | + fmt.Println("A platform owner will review and merge it.") |
| 126 | + |
| 127 | + _ = config.EnsureConfigDir() |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func repoExists(token, name string) bool { |
| 132 | + req, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/javaBin/%s", name), nil) |
| 133 | + req.Header.Set("Authorization", "Bearer "+token) |
| 134 | + resp, err := http.DefaultClient.Do(req) |
| 135 | + if err != nil { |
| 136 | + return false |
| 137 | + } |
| 138 | + defer resp.Body.Close() |
| 139 | + return resp.StatusCode == 200 |
| 140 | +} |
| 141 | + |
| 142 | +func listTeams(token string) ([]string, error) { |
| 143 | + url := "https://api.github.com/repos/javaBin/registry/contents/teams" |
| 144 | + req, _ := http.NewRequest("GET", url, nil) |
| 145 | + req.Header.Set("Authorization", "Bearer "+token) |
| 146 | + resp, err := http.DefaultClient.Do(req) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + defer resp.Body.Close() |
| 151 | + if resp.StatusCode != 200 { |
| 152 | + return nil, fmt.Errorf("HTTP %d", resp.StatusCode) |
| 153 | + } |
| 154 | + var items []struct { |
| 155 | + Name string `json:"name"` |
| 156 | + } |
| 157 | + if err := json.NewDecoder(resp.Body).Decode(&items); err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + var teams []string |
| 161 | + for _, item := range items { |
| 162 | + if strings.HasSuffix(item.Name, ".yaml") { |
| 163 | + teams = append(teams, strings.TrimSuffix(item.Name, ".yaml")) |
| 164 | + } |
| 165 | + } |
| 166 | + return teams, nil |
| 167 | +} |
0 commit comments