-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
300 lines (253 loc) · 7.13 KB
/
Copy pathmain.go
File metadata and controls
300 lines (253 loc) · 7.13 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
)
var version = "dev" // Set by GoReleaser at build time
// Markdown to HTML converter using goldmark
var md goldmark.Markdown
func init() {
// Configure goldmark with table support and safe HTML rendering
md = goldmark.New(
goldmark.WithExtensions(
extension.Table, // Enable Markdown table support
),
goldmark.WithRendererOptions(
html.WithUnsafe(), // Allow raw HTML passthrough
),
)
}
// convertMarkdownToHTML converts Markdown text to HTML
func convertMarkdownToHTML(markdown string) (string, error) {
var buf bytes.Buffer
if err := md.Convert([]byte(markdown), &buf); err != nil {
return "", fmt.Errorf("markdown conversion failed: %w", err)
}
// Trim trailing newline (goldmark adds it)
html := strings.TrimSuffix(buf.String(), "\n")
return html, nil
}
// readAndConvertFile reads a file and converts it to HTML if it's Markdown
func readAndConvertFile(path string) (string, error) {
content, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read file %s: %w", path, err)
}
ext := strings.ToLower(filepath.Ext(path))
// If .html file, return as-is
if ext == ".html" || ext == ".htm" {
return string(content), nil
}
// If .md file or no extension, convert Markdown to HTML
if ext == ".md" || ext == "" {
return convertMarkdownToHTML(string(content))
}
// Unknown extension - assume Markdown for safety (agent-friendly default)
return convertMarkdownToHTML(string(content))
}
// processArgs intercepts and modifies arguments for Markdown conversion
func processArgs(args []string) ([]string, error) {
result := make([]string, 0, len(args))
i := 0
for i < len(args) {
arg := args[i]
// Check for flags that need conversion
if arg == "--description" || arg == "--body" {
if i+1 >= len(args) {
return nil, fmt.Errorf("flag %s requires a value", arg)
}
// Convert inline Markdown text to HTML
markdown := args[i+1]
html, err := convertMarkdownToHTML(markdown)
if err != nil {
return nil, fmt.Errorf("converting %s: %w", arg, err)
}
result = append(result, arg, html)
i += 2
continue
}
if arg == "--description_file" || arg == "--body_file" {
if i+1 >= len(args) {
return nil, fmt.Errorf("flag %s requires a file path", arg)
}
// Read file and convert to HTML
filePath := args[i+1]
html, err := readAndConvertFile(filePath)
if err != nil {
return nil, err
}
// Create temp file with HTML content
tmpFile, err := os.CreateTemp("", "fizzy-md-*.html")
if err != nil {
return nil, fmt.Errorf("failed to create temp file: %w", err)
}
defer tmpFile.Close()
if _, err := tmpFile.WriteString(html); err != nil {
return nil, fmt.Errorf("failed to write temp file: %w", err)
}
// Replace flag with temp file path
result = append(result, arg, tmpFile.Name())
i += 2
continue
}
// Pass through all other arguments unchanged
result = append(result, arg)
i++
}
return result, nil
}
// findFizzy locates the real fizzy binary, avoiding circular resolution.
// Priority: FIZZY_PATH env var → PATH lookup (skipping our own binary).
func findFizzy() string {
// 1. Explicit override via env var
if p := os.Getenv("FIZZY_PATH"); p != "" {
if _, err := os.Stat(p); err == nil {
return p
}
}
// 2. Resolve our own executable path so we can skip it
self, _ := os.Executable()
if self != "" {
self, _ = filepath.EvalSymlinks(self)
}
// 3. Walk PATH entries looking for a "fizzy" that isn't us
pathEnv := os.Getenv("PATH")
for _, dir := range filepath.SplitList(pathEnv) {
for _, candidate := range fizzyCandidates(dir) {
info, err := os.Stat(candidate)
if err != nil || info.IsDir() {
continue
}
real, _ := filepath.EvalSymlinks(candidate)
// Skip if it resolves to ourselves (fizzy-md)
if self != "" && real == self {
continue
}
// Skip shell scripts that call fizzy-md (wrapper scripts)
if isShellWrapperForFizzyMd(candidate) {
continue
}
return candidate
}
}
return ""
}
// isShellWrapperForFizzyMd does a quick check if a file is a small shell script
// that references fizzy-md (i.e. a wrapper that would cause a loop).
func isShellWrapperForFizzyMd(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
if info.IsDir() {
return false
}
// Only check small files (real fizzy binary is >1MB)
if info.Size() > 4096 {
return false
}
file, err := os.Open(path)
if err != nil {
return false
}
defer file.Close()
buf := make([]byte, 4096)
n, err := file.Read(buf)
if err != nil && err != io.EOF {
return false
}
content := string(buf[:n])
return strings.Contains(content, "fizzy-md")
}
func fizzyCandidates(dir string) []string {
base := "fizzy"
if runtime.GOOS != "windows" {
return []string{filepath.Join(dir, base)}
}
if filepath.Ext(base) != "" {
return []string{filepath.Join(dir, base)}
}
pathext := os.Getenv("PATHEXT")
var exts []string
if pathext == "" {
exts = []string{".com", ".exe", ".bat", ".cmd"}
} else {
for _, ext := range strings.Split(pathext, ";") {
clean := strings.TrimSpace(ext)
if clean == "" {
continue
}
if !strings.HasPrefix(clean, ".") {
clean = "." + clean
}
exts = append(exts, clean)
}
}
paths := make([]string, 0, len(exts))
for _, ext := range exts {
paths = append(paths, filepath.Join(dir, base+ext))
}
return paths
}
func main() {
// Get original args (skip program name)
args := os.Args[1:]
// Handle --version flag
if len(args) == 1 && (args[0] == "--version" || args[0] == "-v") {
fmt.Printf("fizzy-md version %s\n", version)
os.Exit(0)
}
// Handle stdin mode: if no args and stdin is piped, convert and output
if len(args) == 0 {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
// Stdin is piped, not a terminal
var buf bytes.Buffer
if _, err := buf.ReadFrom(os.Stdin); err != nil {
fmt.Fprintf(os.Stderr, "fizzy-md error: failed to read stdin: %v\n", err)
os.Exit(1)
}
html, err := convertMarkdownToHTML(buf.String())
if err != nil {
fmt.Fprintf(os.Stderr, "fizzy-md error: %v\n", err)
os.Exit(1)
}
fmt.Print(html)
os.Exit(0)
}
}
// Process args for Markdown conversion
processedArgs, err := processArgs(args)
if err != nil {
fmt.Fprintf(os.Stderr, "fizzy-md error: %v\n", err)
os.Exit(1)
}
// Find fizzy executable, avoiding circular resolution back to ourselves
fizzyPath := findFizzy()
if fizzyPath == "" {
fmt.Fprintf(os.Stderr, "fizzy-md error: fizzy command not found\n")
fmt.Fprintf(os.Stderr, "Set FIZZY_PATH or install fizzy-cli: https://github.com/robzolkos/fizzy-cli\n")
os.Exit(1)
}
// Execute real fizzy with processed args
cmd := exec.Command(fizzyPath, processedArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
os.Exit(exitErr.ExitCode())
}
fmt.Fprintf(os.Stderr, "fizzy-md error: %v\n", err)
os.Exit(1)
}
}