|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/julianknutsen/wasteland/internal/federation" |
| 12 | + "github.com/julianknutsen/wasteland/internal/style" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +func newDoctorCmd(stdout, stderr io.Writer) *cobra.Command { |
| 17 | + return &cobra.Command{ |
| 18 | + Use: "doctor", |
| 19 | + Short: "Check your wasteland setup for common issues", |
| 20 | + Long: `Run diagnostic checks on your wasteland setup. |
| 21 | +
|
| 22 | +Verifies dolt installation, credentials, environment variables, |
| 23 | +and per-wasteland configuration. |
| 24 | +
|
| 25 | +Examples: |
| 26 | + wl doctor`, |
| 27 | + Args: cobra.NoArgs, |
| 28 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 29 | + return runDoctor(stdout, stderr, exec.LookPath, os.Getenv, federation.NewConfigStore()) |
| 30 | + }, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// doctorDeps holds injectable dependencies for testing. |
| 35 | +type doctorDeps struct { |
| 36 | + lookPath func(string) (string, error) |
| 37 | + getenv func(string) string |
| 38 | + store federation.ConfigStore |
| 39 | +} |
| 40 | + |
| 41 | +func runDoctor(stdout, _ io.Writer, lookPath func(string) (string, error), getenv func(string) string, store federation.ConfigStore) error { |
| 42 | + deps := &doctorDeps{lookPath: lookPath, getenv: getenv, store: store} |
| 43 | + runDoctorChecks(stdout, deps) |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func runDoctorChecks(stdout io.Writer, deps *doctorDeps) { |
| 48 | + // 1. dolt installed |
| 49 | + checkDolt(stdout, deps) |
| 50 | + |
| 51 | + // 2. dolt credentials |
| 52 | + checkDoltCreds(stdout) |
| 53 | + |
| 54 | + // 3. DOLTHUB_TOKEN |
| 55 | + checkEnvVar(stdout, deps, "DOLTHUB_TOKEN") |
| 56 | + |
| 57 | + // 4. DOLTHUB_ORG |
| 58 | + checkEnvVar(stdout, deps, "DOLTHUB_ORG") |
| 59 | + |
| 60 | + // 5. Wastelands joined |
| 61 | + checkWastelands(stdout, deps) |
| 62 | +} |
| 63 | + |
| 64 | +func checkDolt(stdout io.Writer, deps *doctorDeps) { |
| 65 | + doltPath, err := deps.lookPath("dolt") |
| 66 | + if err != nil { |
| 67 | + fmt.Fprintf(stdout, " %s dolt: not found in PATH\n", style.Error.Render(style.IconFail)) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + cmd := exec.Command(doltPath, "version") |
| 72 | + output, err := cmd.Output() |
| 73 | + if err != nil { |
| 74 | + fmt.Fprintf(stdout, " %s dolt: found but 'dolt version' failed: %v\n", style.Warning.Render(style.IconWarn), err) |
| 75 | + return |
| 76 | + } |
| 77 | + ver := strings.TrimSpace(string(output)) |
| 78 | + fmt.Fprintf(stdout, " %s dolt: %s\n", style.Success.Render(style.IconPass), ver) |
| 79 | +} |
| 80 | + |
| 81 | +func checkDoltCreds(stdout io.Writer) { |
| 82 | + home, err := os.UserHomeDir() |
| 83 | + if err != nil { |
| 84 | + fmt.Fprintf(stdout, " %s dolt credentials: cannot determine home directory\n", style.Warning.Render(style.IconWarn)) |
| 85 | + return |
| 86 | + } |
| 87 | + credsDir := filepath.Join(home, ".dolt", "creds") |
| 88 | + entries, err := os.ReadDir(credsDir) |
| 89 | + if err != nil { |
| 90 | + fmt.Fprintf(stdout, " %s dolt credentials: no credentials directory found (%s)\n", style.Warning.Render(style.IconWarn), credsDir) |
| 91 | + return |
| 92 | + } |
| 93 | + var keyCount int |
| 94 | + for _, e := range entries { |
| 95 | + if !e.IsDir() && strings.HasSuffix(e.Name(), ".jwk") { |
| 96 | + keyCount++ |
| 97 | + } |
| 98 | + } |
| 99 | + if keyCount == 0 { |
| 100 | + fmt.Fprintf(stdout, " %s dolt credentials: no key files found in %s\n", style.Warning.Render(style.IconWarn), credsDir) |
| 101 | + } else { |
| 102 | + fmt.Fprintf(stdout, " %s dolt credentials: %d key(s) found\n", style.Success.Render(style.IconPass), keyCount) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func checkEnvVar(stdout io.Writer, deps *doctorDeps, name string) { |
| 107 | + val := deps.getenv(name) |
| 108 | + if val == "" { |
| 109 | + fmt.Fprintf(stdout, " %s %s: not set\n", style.Warning.Render(style.IconWarn), name) |
| 110 | + } else { |
| 111 | + // Show partial value for ORG, hide TOKEN |
| 112 | + if name == "DOLTHUB_ORG" { |
| 113 | + fmt.Fprintf(stdout, " %s %s: set (%s)\n", style.Success.Render(style.IconPass), name, val) |
| 114 | + } else { |
| 115 | + fmt.Fprintf(stdout, " %s %s: set\n", style.Success.Render(style.IconPass), name) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func checkWastelands(stdout io.Writer, deps *doctorDeps) { |
| 121 | + upstreams, err := deps.store.List() |
| 122 | + if err != nil { |
| 123 | + fmt.Fprintf(stdout, " %s wastelands: error listing: %v\n", style.Error.Render(style.IconFail), err) |
| 124 | + return |
| 125 | + } |
| 126 | + if len(upstreams) == 0 { |
| 127 | + fmt.Fprintf(stdout, " %s wastelands: none joined (run 'wl join <upstream>')\n", style.Warning.Render(style.IconWarn)) |
| 128 | + return |
| 129 | + } |
| 130 | + fmt.Fprintf(stdout, " %s %d wasteland(s) joined\n", style.Success.Render(style.IconPass), len(upstreams)) |
| 131 | + |
| 132 | + for _, upstream := range upstreams { |
| 133 | + cfg, err := deps.store.Load(upstream) |
| 134 | + if err != nil { |
| 135 | + fmt.Fprintf(stdout, "\n %s:\n", upstream) |
| 136 | + fmt.Fprintf(stdout, " %s config: failed to load: %v\n", style.Error.Render(style.IconFail), err) |
| 137 | + continue |
| 138 | + } |
| 139 | + fmt.Fprintf(stdout, "\n %s:\n", upstream) |
| 140 | + |
| 141 | + // Local clone exists |
| 142 | + if _, err := os.Stat(cfg.LocalDir); err != nil { |
| 143 | + fmt.Fprintf(stdout, " %s Local clone: missing (%s)\n", style.Error.Render(style.IconFail), cfg.LocalDir) |
| 144 | + } else { |
| 145 | + fmt.Fprintf(stdout, " %s Local clone: %s\n", style.Success.Render(style.IconPass), cfg.LocalDir) |
| 146 | + } |
| 147 | + |
| 148 | + // Mode |
| 149 | + fmt.Fprintf(stdout, " %s Mode: %s\n", style.Success.Render(style.IconPass), cfg.ResolveMode()) |
| 150 | + |
| 151 | + // GPG signing |
| 152 | + checkGPGSigning(stdout, cfg, deps) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func checkGPGSigning(stdout io.Writer, cfg *federation.Config, deps *doctorDeps) { |
| 157 | + if !cfg.Signing { |
| 158 | + fmt.Fprintf(stdout, " %s GPG signing: disabled\n", style.Warning.Render(style.IconWarn)) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + // Check that dolt has a signing key configured. |
| 163 | + doltPath, err := deps.lookPath("dolt") |
| 164 | + if err != nil { |
| 165 | + fmt.Fprintf(stdout, " %s GPG signing: enabled but dolt not found\n", style.Warning.Render(style.IconWarn)) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + cmd := exec.Command(doltPath, "config", "--global", "--get", "sqlserver.global.signingkey") |
| 170 | + keyOut, err := cmd.Output() |
| 171 | + keyID := strings.TrimSpace(string(keyOut)) |
| 172 | + if err != nil || keyID == "" { |
| 173 | + fmt.Fprintf(stdout, " %s GPG signing: enabled but no signing key configured in dolt\n", style.Error.Render(style.IconFail)) |
| 174 | + fmt.Fprintf(stdout, " Run: dolt config --global --add sqlserver.global.signingkey <your-gpg-key-id>\n") |
| 175 | + return |
| 176 | + } |
| 177 | + |
| 178 | + // Check that the GPG key actually exists locally. |
| 179 | + gpgCmd := exec.Command("gpg", "--list-secret-keys", keyID) |
| 180 | + if err := gpgCmd.Run(); err != nil { |
| 181 | + fmt.Fprintf(stdout, " %s GPG signing: key %s not found in GPG keyring\n", style.Error.Render(style.IconFail), keyID) |
| 182 | + fmt.Fprintf(stdout, " Run: gpg --list-secret-keys --keyid-format long\n") |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + fmt.Fprintf(stdout, " %s GPG signing: enabled (key %s)\n", style.Success.Render(style.IconPass), keyID) |
| 187 | +} |
0 commit comments