-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (85 loc) · 2.45 KB
/
Copy pathmain.go
File metadata and controls
104 lines (85 loc) · 2.45 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/panz3r/firefox-bookmarks/bookmarks"
)
// printUsage prints the usage information
func printUsage() {
fmt.Printf(`
firefox-bookmarks [-o OUTPUT_FILE] input_file
Converts Firefox bookmark backup files to HTML format.
Supports both .jsonlz4 (compressed backup) and .json (uncompressed) input files.
Examples:
firefox-bookmarks bookmarks-2025-06-11.jsonlz4
firefox-bookmarks -o my_bookmarks.html bookmarks-2025-06-11.jsonlz4
firefox-bookmarks -o bookmarks.html bookmarks.json
Options:
`)
flag.PrintDefaults()
}
func main() {
var outputFile string
var showHelp bool
flag.StringVar(&outputFile, "o", "", "Path to output HTML file. If omitted, uses input filename with .html extension")
flag.BoolVar(&showHelp, "help", false, "Show this help message")
flag.Usage = printUsage
flag.Parse()
if showHelp {
printUsage()
return
}
// Check if input file is provided
if flag.NArg() < 1 {
fmt.Fprintf(os.Stderr, "Error: Input file is required.\n")
printUsage()
os.Exit(1)
}
inputFile := flag.Arg(0)
// Check if input file exists
if _, err := os.Stat(inputFile); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: Input file '%s' does not exist.\n", inputFile)
os.Exit(1)
}
// Determine output filename
var outputPath string
if outputFile != "" {
outputPath = outputFile
} else {
ext := filepath.Ext(inputFile)
outputPath = strings.TrimSuffix(inputFile, ext) + ".html"
}
// Load bookmark data
loader := bookmarks.NewBookmarkLoader()
fmt.Printf("Processing bookmark file: %s\n", inputFile)
bookmarkData, err := loader.LoadBookmarksFromFile(inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Conversion failed: %v\n", err)
os.Exit(1)
}
// Convert to HTML
fmt.Println("Converting bookmarks to HTML format...")
outputFileHandle, err := os.Create(outputPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to create output file: %v\n", err)
os.Exit(1)
}
defer outputFileHandle.Close()
writer := bufio.NewWriter(outputFileHandle)
err = bookmarks.ConvertBookmarksToHTML(writer, bookmarkData)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to convert bookmarks: %v\n", err)
os.Exit(1)
}
// Flush the buffered writer
err = writer.Flush()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to flush output: %v\n", err)
os.Exit(1)
}
fmt.Printf("Successfully converted bookmarks to: %s\n", outputPath)
}