|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// The install shortcut fetches the typed client for a language from the project's |
| 13 | +// OWN registry, fronted by the public install host — the ONE coordinate this command |
| 14 | +// needs. The host is public (it already appears in the shell installer), so embedding |
| 15 | +// it here leaks nothing; the private Artifact Registry coordinates live only behind |
| 16 | +// that front door (get-service proxies them). Package names are the public client |
| 17 | +// package identities. Override the host with INTERLOCK_GET_HOST or --host for staging. |
| 18 | +const ( |
| 19 | + defaultGetHost = "get.operatorstack.systems" |
| 20 | + npmScope = "@operatorstack" |
| 21 | + npmClientPkg = "@operatorstack/interlock" |
| 22 | + pyClientPkg = "interlock-protocol" |
| 23 | +) |
| 24 | + |
| 25 | +func resolveGetHost(flag string) string { |
| 26 | + if flag != "" { |
| 27 | + return flag |
| 28 | + } |
| 29 | + if env := strings.TrimSpace(os.Getenv("INTERLOCK_GET_HOST")); env != "" { |
| 30 | + return env |
| 31 | + } |
| 32 | + return defaultGetHost |
| 33 | +} |
| 34 | + |
| 35 | +// cmdInstall fetches the typed client for a language from the front-door registry, |
| 36 | +// configuring the package manager's index so the consumer never hand-edits .npmrc / |
| 37 | +// --index-url and never needs GCP credentials. --configure-only writes the registry |
| 38 | +// config and stops (no toolchain required); the default also runs the install. |
| 39 | +func cmdInstall(args []string) error { |
| 40 | + host := "" |
| 41 | + dir := "." |
| 42 | + force := false |
| 43 | + configureOnly := false |
| 44 | + var positional []string |
| 45 | + |
| 46 | + i := 0 |
| 47 | + for i < len(args) { |
| 48 | + switch args[i] { |
| 49 | + case "--host": |
| 50 | + if i+1 >= len(args) { |
| 51 | + return fmt.Errorf("install: --host wants a hostname") |
| 52 | + } |
| 53 | + host = args[i+1] |
| 54 | + i += 2 |
| 55 | + case "--dir": |
| 56 | + if i+1 >= len(args) { |
| 57 | + return fmt.Errorf("install: --dir wants a path") |
| 58 | + } |
| 59 | + dir = args[i+1] |
| 60 | + i += 2 |
| 61 | + case "--configure-only": |
| 62 | + configureOnly = true |
| 63 | + i++ |
| 64 | + case "--force", "-f": |
| 65 | + force = true |
| 66 | + i++ |
| 67 | + default: |
| 68 | + if strings.HasPrefix(args[i], "-") { |
| 69 | + return fmt.Errorf("install: unexpected flag %q", args[i]) |
| 70 | + } |
| 71 | + positional = append(positional, args[i]) |
| 72 | + i++ |
| 73 | + } |
| 74 | + } |
| 75 | + if len(positional) > 1 { |
| 76 | + return fmt.Errorf("install: expected at most one language, got %v", positional) |
| 77 | + } |
| 78 | + |
| 79 | + lang := "" |
| 80 | + if len(positional) == 1 { |
| 81 | + lang = positional[0] |
| 82 | + } else { |
| 83 | + chosen, err := promptLanguage() |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + lang = chosen |
| 88 | + } |
| 89 | + |
| 90 | + host = resolveGetHost(host) |
| 91 | + switch normalizeLang(lang) { |
| 92 | + case "ts": |
| 93 | + return installNPM(dir, host, force, configureOnly) |
| 94 | + case "python": |
| 95 | + return installPython(dir, host, force, configureOnly) |
| 96 | + default: |
| 97 | + return fmt.Errorf("install: unknown language %q (want: ts | python)", lang) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func normalizeLang(lang string) string { |
| 102 | + switch strings.ToLower(strings.TrimSpace(lang)) { |
| 103 | + case "ts", "typescript", "js", "javascript", "npm", "node": |
| 104 | + return "ts" |
| 105 | + case "python", "py", "pip", "uv": |
| 106 | + return "python" |
| 107 | + default: |
| 108 | + return "" |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// promptLanguage renders a numbered picker and reads a choice, mirroring init's |
| 113 | +// promptTemplate. On EOF with no input it returns a clear non-interactive error. |
| 114 | +func promptLanguage() (string, error) { |
| 115 | + fmt.Println("Which typed client do you want to install?") |
| 116 | + fmt.Println() |
| 117 | + fmt.Println(" 1. TypeScript (" + npmClientPkg + ", via npm)") |
| 118 | + fmt.Println(" 2. Python (" + pyClientPkg + ", via uv or pip)") |
| 119 | + fmt.Println() |
| 120 | + fmt.Print("Choice [1-2]: ") |
| 121 | + |
| 122 | + line, _ := bufio.NewReader(os.Stdin).ReadString('\n') |
| 123 | + switch strings.TrimSpace(line) { |
| 124 | + case "1": |
| 125 | + return "ts", nil |
| 126 | + case "2": |
| 127 | + return "python", nil |
| 128 | + case "": |
| 129 | + return "", fmt.Errorf("install: name a language when non-interactive: interlock install ts|python") |
| 130 | + default: |
| 131 | + return "", fmt.Errorf("install: invalid choice %q (want 1 or 2)", strings.TrimSpace(line)) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// upsertLine replaces the first line whose key matches prefix, or appends it, |
| 136 | +// keeping the file idempotent across re-runs. |
| 137 | +func upsertLine(existing, prefix, line string) string { |
| 138 | + out := []string{} |
| 139 | + replaced := false |
| 140 | + for _, l := range strings.Split(existing, "\n") { |
| 141 | + if strings.HasPrefix(strings.TrimSpace(l), prefix) { |
| 142 | + if !replaced { |
| 143 | + out = append(out, line) |
| 144 | + replaced = true |
| 145 | + } |
| 146 | + continue |
| 147 | + } |
| 148 | + out = append(out, l) |
| 149 | + } |
| 150 | + joined := strings.TrimRight(strings.Join(out, "\n"), "\n") |
| 151 | + if !replaced { |
| 152 | + if joined != "" { |
| 153 | + joined += "\n" |
| 154 | + } |
| 155 | + joined += line |
| 156 | + } |
| 157 | + return joined + "\n" |
| 158 | +} |
| 159 | + |
| 160 | +func installNPM(dir, host string, force, configureOnly bool) error { |
| 161 | + registryURL := fmt.Sprintf("https://%s/npm/", host) |
| 162 | + line := fmt.Sprintf("%s:registry=%s", npmScope, registryURL) |
| 163 | + npmrc := filepath.Join(dir, ".npmrc") |
| 164 | + existing := "" |
| 165 | + if b, err := os.ReadFile(npmrc); err == nil { |
| 166 | + existing = string(b) |
| 167 | + } else if !os.IsNotExist(err) { |
| 168 | + return err |
| 169 | + } |
| 170 | + if err := os.WriteFile(npmrc, []byte(upsertLine(existing, npmScope+":registry=", line)), 0o644); err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + fmt.Printf("configured %s -> %s\n", npmrc, registryURL) |
| 174 | + if configureOnly { |
| 175 | + fmt.Printf("run: npm install %s\n", npmClientPkg) |
| 176 | + return nil |
| 177 | + } |
| 178 | + if _, err := exec.LookPath("npm"); err != nil { |
| 179 | + return fmt.Errorf("install: npm not found on PATH (config written; run: npm install %s)", npmClientPkg) |
| 180 | + } |
| 181 | + return runIn(dir, "npm", "install", npmClientPkg) |
| 182 | +} |
| 183 | + |
| 184 | +func installPython(dir, host string, force, configureOnly bool) error { |
| 185 | + indexURL := fmt.Sprintf("https://%s/pip/simple/", host) |
| 186 | + // Persist the index for reuse and print the exact command. uv and pip take the |
| 187 | + // same --index-url flag, so the install line is uniform; the .interlock/registry |
| 188 | + // file records it so re-runs and CI can source one place. |
| 189 | + regFile := filepath.Join(dir, ".interlock", "registry") |
| 190 | + if err := os.MkdirAll(filepath.Dir(regFile), 0o755); err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + if err := os.WriteFile(regFile, []byte("PIP_INDEX_URL="+indexURL+"\n"), 0o644); err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + fmt.Printf("configured %s (PIP_INDEX_URL=%s)\n", regFile, indexURL) |
| 197 | + if configureOnly { |
| 198 | + fmt.Printf("run: uv pip install --index-url %s %s (or: pip install --index-url %s %s)\n", indexURL, pyClientPkg, indexURL, pyClientPkg) |
| 199 | + return nil |
| 200 | + } |
| 201 | + if _, err := exec.LookPath("uv"); err == nil { |
| 202 | + return runIn(dir, "uv", "pip", "install", "--index-url", indexURL, pyClientPkg) |
| 203 | + } |
| 204 | + if _, err := exec.LookPath("pip"); err == nil { |
| 205 | + return runIn(dir, "pip", "install", "--index-url", indexURL, pyClientPkg) |
| 206 | + } |
| 207 | + return fmt.Errorf("install: neither uv nor pip found on PATH (config written; run with --index-url %s)", indexURL) |
| 208 | +} |
| 209 | + |
| 210 | +func runIn(dir, name string, args ...string) error { |
| 211 | + cmd := exec.Command(name, args...) |
| 212 | + cmd.Dir = dir |
| 213 | + cmd.Stdout = os.Stdout |
| 214 | + cmd.Stderr = os.Stderr |
| 215 | + if err := cmd.Run(); err != nil { |
| 216 | + return fmt.Errorf("install: %s %s: %w", name, strings.Join(args, " "), err) |
| 217 | + } |
| 218 | + return nil |
| 219 | +} |
0 commit comments