|
| 1 | +/* Go wizard using posthog-agent with PostHog MCP */ |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import type { WizardRunOptions } from '@utils/types'; |
| 5 | +import type { FrameworkConfig } from '@lib/framework-config'; |
| 6 | +import { goModulesPackageManager } from '@lib/detection/package-manager'; |
| 7 | +import { Integration } from '@lib/constants'; |
| 8 | + |
| 9 | +type GoContext = { |
| 10 | + goVersion?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +function readGoMod(installDir: string): string | undefined { |
| 14 | + const goModPath = path.join(installDir, 'go.mod'); |
| 15 | + if (!fs.existsSync(goModPath)) { |
| 16 | + return undefined; |
| 17 | + } |
| 18 | + return fs.readFileSync(goModPath, 'utf-8'); |
| 19 | +} |
| 20 | + |
| 21 | +/** The `go 1.x` directive from go.mod (the toolchain floor, not a patch version). */ |
| 22 | +function getGoVersion(installDir: string): string | undefined { |
| 23 | + const goMod = readGoMod(installDir); |
| 24 | + return goMod?.match(/^go\s+([\d.]+)/m)?.[1]; |
| 25 | +} |
| 26 | + |
| 27 | +export const GO_AGENT_CONFIG: FrameworkConfig<GoContext> = { |
| 28 | + metadata: { |
| 29 | + name: 'Go', |
| 30 | + integration: Integration.go, |
| 31 | + docsUrl: 'https://posthog.com/docs/libraries/go', |
| 32 | + gatherContext: (options: WizardRunOptions) => { |
| 33 | + const goVersion = getGoVersion(options.installDir); |
| 34 | + return Promise.resolve({ goVersion }); |
| 35 | + }, |
| 36 | + }, |
| 37 | + |
| 38 | + detection: { |
| 39 | + packageName: 'posthog-go', |
| 40 | + packageDisplayName: 'Go', |
| 41 | + usesPackageJson: false, |
| 42 | + getVersion: () => undefined, |
| 43 | + // A go.mod with a module directive marks a Go module root; a bare |
| 44 | + // directory containing .go files without one is not integratable. |
| 45 | + detect: (options) => { |
| 46 | + const goMod = readGoMod(options.installDir); |
| 47 | + return Promise.resolve(!!goMod && /^module\s+\S+/m.test(goMod)); |
| 48 | + }, |
| 49 | + detectPackageManager: goModulesPackageManager, |
| 50 | + }, |
| 51 | + |
| 52 | + environment: { |
| 53 | + uploadToHosting: false, |
| 54 | + getEnvVars: (apiKey: string, host: string) => ({ |
| 55 | + POSTHOG_API_KEY: apiKey, |
| 56 | + POSTHOG_HOST: host, |
| 57 | + }), |
| 58 | + }, |
| 59 | + |
| 60 | + analytics: { |
| 61 | + getTags: (context) => ({ |
| 62 | + goVersion: context.goVersion || 'unknown', |
| 63 | + }), |
| 64 | + }, |
| 65 | + |
| 66 | + prompts: { |
| 67 | + projectTypeDetection: |
| 68 | + 'This is a Go project. Look for go.mod, go.sum, cmd/, internal/, and main packages to confirm.', |
| 69 | + packageInstallation: |
| 70 | + 'Install the PostHog Go SDK with `go get github.com/posthog/posthog-go`. Do not manually edit go.mod or go.sum; the go tool updates them automatically. Run `go mod tidy` afterwards if imports change.', |
| 71 | + getAdditionalContextLines: (context) => { |
| 72 | + const lines = [ |
| 73 | + `Framework docs ID: go (use posthog://docs/frameworks/go for documentation)`, |
| 74 | + ]; |
| 75 | + if (context.goVersion) { |
| 76 | + lines.push(`Go version (go.mod directive): ${context.goVersion}`); |
| 77 | + } |
| 78 | + lines.push( |
| 79 | + 'Create one PostHog client per process and close it during graceful shutdown (`defer client.Close()`) so queued events flush.', |
| 80 | + ); |
| 81 | + return lines; |
| 82 | + }, |
| 83 | + }, |
| 84 | + |
| 85 | + ui: { |
| 86 | + successMessage: 'PostHog integration complete', |
| 87 | + estimatedDurationMinutes: 5, |
| 88 | + getOutroChanges: () => [ |
| 89 | + 'Analyzed your Go project structure', |
| 90 | + 'Installed the posthog-go SDK via go get', |
| 91 | + 'Initialized a shared PostHog client configured from environment variables', |
| 92 | + 'Instrumented meaningful server events with client.Enqueue(posthog.Capture{...})', |
| 93 | + ], |
| 94 | + getOutroNextSteps: () => [ |
| 95 | + 'Run your Go service and trigger the instrumented code paths', |
| 96 | + 'Visit your PostHog dashboard to see incoming events', |
| 97 | + 'Use client.Enqueue(posthog.Capture{...}) to track custom events', |
| 98 | + 'Keep client.Close() in your graceful shutdown path so queued events flush', |
| 99 | + ], |
| 100 | + }, |
| 101 | +}; |
0 commit comments