|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + |
| 12 | + "github.com/supermodeltools/cli/internal/api" |
| 13 | + "github.com/supermodeltools/cli/internal/audit" |
| 14 | + "github.com/supermodeltools/cli/internal/cache" |
| 15 | + "github.com/supermodeltools/cli/internal/config" |
| 16 | +) |
| 17 | + |
| 18 | +func init() { |
| 19 | + var dir string |
| 20 | + |
| 21 | + c := &cobra.Command{ |
| 22 | + Use: "share", |
| 23 | + Short: "Upload your codebase health report and get a public URL", |
| 24 | + Long: `Runs a health audit and uploads the report to supermodeltools.com, |
| 25 | +returning a short public URL you can share or embed as a README badge. |
| 26 | +
|
| 27 | +Example: |
| 28 | +
|
| 29 | + supermodel share |
| 30 | + supermodel share --dir ./path/to/project`, |
| 31 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 32 | + return runShare(cmd, dir) |
| 33 | + }, |
| 34 | + SilenceUsage: true, |
| 35 | + } |
| 36 | + |
| 37 | + c.Flags().StringVar(&dir, "dir", "", "project directory (default: current working directory)") |
| 38 | + rootCmd.AddCommand(c) |
| 39 | +} |
| 40 | + |
| 41 | +func runShare(cmd *cobra.Command, dir string) error { |
| 42 | + rootDir, projectName, err := resolveAuditDir(dir) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + cfg, err := config.Load() |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + // Run the full audit pipeline. |
| 53 | + ir, err := shareAnalyze(cmd, cfg, rootDir, projectName) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + report := audit.Analyze(ir, projectName) |
| 59 | + |
| 60 | + impact, err := runImpactForShare(cmd, cfg, rootDir) |
| 61 | + if err != nil { |
| 62 | + fmt.Fprintf(cmd.ErrOrStderr(), "Warning: impact analysis unavailable: %v\n", err) |
| 63 | + } else { |
| 64 | + audit.EnrichWithImpact(report, impact) |
| 65 | + } |
| 66 | + |
| 67 | + // Render to Markdown. |
| 68 | + var buf bytes.Buffer |
| 69 | + audit.RenderHealth(&buf, report) |
| 70 | + |
| 71 | + // Upload and get public URL. |
| 72 | + client := api.New(cfg) |
| 73 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) |
| 74 | + defer cancel() |
| 75 | + |
| 76 | + fmt.Fprintln(cmd.ErrOrStderr(), "Uploading report…") |
| 77 | + url, err := client.Share(ctx, api.ShareRequest{ |
| 78 | + ProjectName: projectName, |
| 79 | + Status: string(report.Status), |
| 80 | + Content: buf.String(), |
| 81 | + }) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("upload failed: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + fmt.Fprintf(cmd.OutOrStdout(), "\n Report: %s\n\n", url) |
| 87 | + fmt.Fprintf(cmd.OutOrStdout(), " Add this badge to your README:\n\n") |
| 88 | + fmt.Fprintf(cmd.OutOrStdout(), |
| 89 | + " [](%s)\n\n", |
| 90 | + report.Status, url) |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func shareAnalyze(cmd *cobra.Command, cfg *config.Config, rootDir, projectName string) (*api.SupermodelIR, error) { |
| 96 | + if err := cfg.RequireAPIKey(); err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + fmt.Fprintln(cmd.ErrOrStderr(), "Creating repository archive…") |
| 101 | + zipPath, err := audit.CreateZip(rootDir) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("create archive: %w", err) |
| 104 | + } |
| 105 | + defer func() { _ = os.Remove(zipPath) }() |
| 106 | + |
| 107 | + hash, err := cache.HashFile(zipPath) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("hash archive: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + client := api.New(cfg) |
| 113 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) |
| 114 | + defer cancel() |
| 115 | + |
| 116 | + fmt.Fprintf(cmd.ErrOrStderr(), "Analyzing %s…\n", projectName) |
| 117 | + return client.AnalyzeDomains(ctx, zipPath, "share-"+hash[:16]) |
| 118 | +} |
| 119 | + |
| 120 | +func runImpactForShare(cmd *cobra.Command, cfg *config.Config, rootDir string) (*api.ImpactResult, error) { |
| 121 | + zipPath, err := audit.CreateZip(rootDir) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + defer func() { _ = os.Remove(zipPath) }() |
| 126 | + |
| 127 | + hash, err := cache.HashFile(zipPath) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + client := api.New(cfg) |
| 133 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) |
| 134 | + defer cancel() |
| 135 | + |
| 136 | + fmt.Fprintln(cmd.ErrOrStderr(), "Running impact analysis…") |
| 137 | + return client.Impact(ctx, zipPath, "share-impact-"+hash[:16], "", "") |
| 138 | +} |
| 139 | + |
| 140 | +// resolveAuditDir and findGitRoot are defined in cmd/audit.go (same package). |
0 commit comments