-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
132 lines (115 loc) · 3.63 KB
/
Copy pathdiff.go
File metadata and controls
132 lines (115 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/bladeacer/ocd/internal/cache"
"github.com/bladeacer/ocd/internal/core"
"github.com/bladeacer/ocd/internal/sources"
"github.com/bladeacer/ocd/internal/tui"
)
func expandPath(p string) string {
if strings.HasPrefix(p, "~/") {
home, _ := os.UserHomeDir()
p = filepath.Join(home, p[2:])
}
return os.ExpandEnv(p)
}
func printTLDR(t *core.TLDRResult, exportPath string) {
fmt.Println(t.String())
if exportPath != "" {
fmt.Printf("Exported: %s\n", exportPath)
}
}
func ensureCSS(version string) error {
p := filepath.Join(".obsidian_cache", "css", version, "app.css")
if _, err := os.Stat(p); err == nil {
return nil
}
fmt.Printf("Extracting app.css for v%s...\n", version)
_, err := core.ExtractCSS(version)
return err
}
func NewDiffCmd() *cobra.Command {
var forceRefresh bool
var interactive bool
var tldr bool
var tldrFormat string
var tldrOutput string
cmd := &cobra.Command{
Use: "diff [version-a] [version-b]",
Short: "Show CSS diff between two Obsidian versions",
Long: `Display a unified diff of app.css between two Obsidian versions.
Versions are auto-extracted if not already cached.
If no arguments are provided, or --pick is used, an interactive
version picker is launched.
Use --tldr to print a summary of CSS changes and export to file.`,
Args: cobra.MaximumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
var versionA, versionB string
if len(args) == 2 {
versionA = args[0]
versionB = args[1]
} else if interactive || len(args) == 0 {
c, err := cache.New(0)
if err != nil {
return fmt.Errorf("cache init: %w", err)
}
f := sources.NewFetcher(c)
versionA, versionB, err = tui.PickVersions(f, forceRefresh)
if err != nil {
return fmt.Errorf("picker: %w", err)
}
if versionA == "" || versionB == "" {
fmt.Println("Selection cancelled.")
return nil
}
} else {
return fmt.Errorf("usage: diff <version-a> <version-b> or diff --pick")
}
if err := ensureCSS(versionA); err != nil {
return fmt.Errorf("extract %s: %w", versionA, err)
}
if err := ensureCSS(versionB); err != nil {
return fmt.Errorf("extract %s: %w", versionB, err)
}
result := core.DiffCSS(versionA, versionB)
if result.Error != nil {
return fmt.Errorf("diff: %w", result.Error)
}
if tldr {
tldrResult := core.AnalyzeDiff(result.Diff)
tldrResult.VersionA = versionA
tldrResult.VersionB = versionB
tldrResult.SemverBump = core.SemverBump(versionA, versionB)
exportPath := ""
if tldrOutput != "" {
exportPath = expandPath(tldrOutput)
} else {
wd, wdErr := os.Getwd()
if wdErr != nil {
exportPath = "."
} else {
exportPath = wd
}
}
fname := fmt.Sprintf("ocd-tldr-%s-%s.%s", versionA, versionB, tldrFormat)
fullPath := filepath.Join(exportPath, fname)
if err := core.ExportTLDR(tldrResult, fullPath, tldrFormat); err != nil {
return fmt.Errorf("export tldr: %w", err)
}
printTLDR(tldrResult, fullPath)
return nil
}
return tui.RunDiffViewer(result)
},
}
cmd.Flags().BoolVarP(&forceRefresh, "refresh", "r", false, "Force refresh metadata cache")
cmd.Flags().BoolVarP(&interactive, "pick", "p", false, "Launch interactive version picker")
cmd.Flags().BoolVar(&tldr, "tldr", false, "Print TLDR analysis and export to file")
cmd.Flags().StringVar(&tldrFormat, "tldr-format", "toml", "Export format: toml (default), json, or yaml")
cmd.Flags().StringVar(&tldrOutput, "tldr-output", "", "Output directory (supports ~, $HOME, $XDG_CONFIG_HOME)")
return cmd
}