|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/openbootdotdev/openboot/internal/auth" |
| 12 | + "github.com/openbootdotdev/openboot/internal/httputil" |
| 13 | + syncpkg "github.com/openbootdotdev/openboot/internal/sync" |
| 14 | + "github.com/openbootdotdev/openboot/internal/ui" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +var logCmd = &cobra.Command{ |
| 19 | + Use: "log", |
| 20 | + Short: "Show revision history for your config", |
| 21 | + Long: `List the revision history for your synced config on openboot.dev. |
| 22 | +
|
| 23 | +Each time you run 'openboot push', the previous version is saved as a revision. |
| 24 | +Use 'openboot restore <revision-id>' to roll back to a previous state.`, |
| 25 | + Example: ` # Show revision history for your current config |
| 26 | + openboot log |
| 27 | +
|
| 28 | + # Show history for a specific config slug |
| 29 | + openboot log --slug my-config`, |
| 30 | + SilenceUsage: true, |
| 31 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 32 | + slug, _ := cmd.Flags().GetString("slug") |
| 33 | + return runLog(slug) |
| 34 | + }, |
| 35 | +} |
| 36 | + |
| 37 | +func init() { |
| 38 | + logCmd.Flags().String("slug", "", "config slug to show history for (default: current sync source)") |
| 39 | + rootCmd.AddCommand(logCmd) |
| 40 | +} |
| 41 | + |
| 42 | +type revisionSummary struct { |
| 43 | + ID string `json:"id"` |
| 44 | + Message *string `json:"message"` |
| 45 | + CreatedAt string `json:"created_at"` |
| 46 | + PackageCount int `json:"package_count"` |
| 47 | +} |
| 48 | + |
| 49 | +func runLog(slugOverride string) error { |
| 50 | + apiBase := auth.GetAPIBase() |
| 51 | + |
| 52 | + if !auth.IsAuthenticated() { |
| 53 | + return fmt.Errorf("not logged in — run 'openboot login' first") |
| 54 | + } |
| 55 | + |
| 56 | + stored, err := auth.LoadToken() |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("load auth token: %w", err) |
| 59 | + } |
| 60 | + if stored == nil { |
| 61 | + return fmt.Errorf("no valid auth token found — please log in again") |
| 62 | + } |
| 63 | + |
| 64 | + slug := slugOverride |
| 65 | + if slug == "" { |
| 66 | + source, loadErr := syncpkg.LoadSource() |
| 67 | + if loadErr == nil && source != nil && source.Slug != "" { |
| 68 | + slug = source.Slug |
| 69 | + } |
| 70 | + } |
| 71 | + if slug == "" { |
| 72 | + return fmt.Errorf("no config slug — use --slug or run 'openboot install <config>' first") |
| 73 | + } |
| 74 | + |
| 75 | + reqURL := fmt.Sprintf("%s/api/configs/%s/revisions", apiBase, url.PathEscape(slug)) |
| 76 | + req, err := http.NewRequest(http.MethodGet, reqURL, nil) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("build request: %w", err) |
| 79 | + } |
| 80 | + req.Header.Set("Authorization", "Bearer "+stored.Token) |
| 81 | + |
| 82 | + client := &http.Client{Timeout: 15 * time.Second} |
| 83 | + resp, err := httputil.Do(client, req) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("fetch revisions: %w", err) |
| 86 | + } |
| 87 | + defer resp.Body.Close() |
| 88 | + |
| 89 | + if resp.StatusCode == http.StatusNotFound { |
| 90 | + return fmt.Errorf("config %q not found", slug) |
| 91 | + } |
| 92 | + if resp.StatusCode != http.StatusOK { |
| 93 | + body, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<10)) |
| 94 | + return fmt.Errorf("fetch revisions failed (status %d): %s", resp.StatusCode, string(body)) |
| 95 | + } |
| 96 | + |
| 97 | + var result struct { |
| 98 | + Revisions []revisionSummary `json:"revisions"` |
| 99 | + } |
| 100 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 101 | + return fmt.Errorf("parse response: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + fmt.Println() |
| 105 | + ui.Header(fmt.Sprintf("Revision history: %s", slug)) |
| 106 | + fmt.Println() |
| 107 | + |
| 108 | + if len(result.Revisions) == 0 { |
| 109 | + ui.Muted(" No revisions yet. Push a config update to create one.") |
| 110 | + fmt.Println() |
| 111 | + return nil |
| 112 | + } |
| 113 | + |
| 114 | + for _, rev := range result.Revisions { |
| 115 | + ts := rev.CreatedAt |
| 116 | + if t, parseErr := time.Parse("2006-01-02T15:04:05Z", rev.CreatedAt); parseErr == nil { |
| 117 | + ts = t.Local().Format("2006-01-02 15:04") |
| 118 | + } else if t2, parseErr2 := time.Parse("2006-01-02 15:04:05", rev.CreatedAt); parseErr2 == nil { |
| 119 | + ts = t2.Local().Format("2006-01-02 15:04") |
| 120 | + } |
| 121 | + |
| 122 | + msg := "" |
| 123 | + if rev.Message != nil && *rev.Message != "" { |
| 124 | + msg = fmt.Sprintf(" %s", *rev.Message) |
| 125 | + } |
| 126 | + |
| 127 | + fmt.Printf(" %s %s %d pkgs%s\n", |
| 128 | + ui.Cyan(rev.ID), |
| 129 | + ts, |
| 130 | + rev.PackageCount, |
| 131 | + msg, |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + fmt.Println() |
| 136 | + ui.Muted("Use 'openboot restore <revision-id>' to roll back to a previous state.") |
| 137 | + fmt.Println() |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments