105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
package main
|
||
|
||
import (
|
||
"flag"
|
||
"fmt"
|
||
"os"
|
||
|
||
tea "github.com/charmbracelet/bubbletea"
|
||
)
|
||
|
||
func main() {
|
||
var filePath string
|
||
var configPath string
|
||
var writeConfig bool
|
||
|
||
flag.StringVar(&filePath, "f", "", "File to edit")
|
||
flag.StringVar(&configPath, "config", "", "Path to config file (overrides auto-search)")
|
||
flag.BoolVar(&writeConfig, "write-config", false, "Write default config to standard location and exit")
|
||
flag.Parse()
|
||
|
||
// Поддержка позиционного аргумента: grimoir file.txt или grimoir https://...
|
||
if filePath == "" && flag.NArg() > 0 {
|
||
filePath = flag.Arg(0)
|
||
}
|
||
|
||
// Загружаем конфиг
|
||
var cfg Config
|
||
if configPath != "" {
|
||
data, err := os.ReadFile(configPath)
|
||
if err != nil {
|
||
fmt.Fprintf(os.Stderr, "Error reading config %s: %v\n", configPath, err)
|
||
os.Exit(1)
|
||
}
|
||
cfg = DefaultConfig()
|
||
if err := parseConfig(data, &cfg); err != nil {
|
||
fmt.Fprintf(os.Stderr, "Error parsing config %s: %v\n", configPath, err)
|
||
os.Exit(1)
|
||
}
|
||
cfg.LoadedFrom = configPath
|
||
} else {
|
||
cfg = LoadConfig()
|
||
}
|
||
|
||
// --write-config: записать дефолтный конфиг и выйти
|
||
if writeConfig {
|
||
paths := ConfigPaths()
|
||
target := ""
|
||
for _, p := range paths {
|
||
if p != paths[0] {
|
||
target = p
|
||
break
|
||
}
|
||
}
|
||
if target == "" {
|
||
target = "md-editor.conf"
|
||
}
|
||
if err := WriteDefaultConfig(target); err != nil {
|
||
fmt.Fprintf(os.Stderr, "Error writing config: %v\n", err)
|
||
os.Exit(1)
|
||
}
|
||
fmt.Printf("Default config written to: %s\n", target)
|
||
os.Exit(0)
|
||
}
|
||
|
||
// === Если передан URL — скачиваем и конвертируем в Markdown ===
|
||
if IsURL(filePath) {
|
||
fmt.Fprintf(os.Stderr, "Fetching: %s\n", filePath)
|
||
|
||
result, err := FetchAndConvert(filePath)
|
||
if err != nil {
|
||
fmt.Fprintf(os.Stderr, "Error fetching URL: %v\n", err)
|
||
os.Exit(1)
|
||
}
|
||
|
||
// Сохраняем рядом с текущей директорией
|
||
savedPath, err := SaveFetchResult(result, "")
|
||
if err != nil {
|
||
fmt.Fprintf(os.Stderr, "Error saving file: %v\n", err)
|
||
os.Exit(1)
|
||
}
|
||
|
||
fmt.Fprintf(os.Stderr, "Saved as: %s\n", savedPath)
|
||
filePath = savedPath
|
||
}
|
||
|
||
// === Обычный режим — открываем файл ===
|
||
if filePath == "" {
|
||
filePath = "untitled.md"
|
||
}
|
||
|
||
content, err := LoadFile(filePath)
|
||
if err != nil && !os.IsNotExist(err) {
|
||
fmt.Fprintf(os.Stderr, "Error loading file: %v\n", err)
|
||
os.Exit(1)
|
||
}
|
||
|
||
fileType := DetectFileType(filePath)
|
||
model := NewEditor(filePath, fileType, content, cfg)
|
||
|
||
p := tea.NewProgram(model, tea.WithAltScreen())
|
||
if _, err := p.Run(); err != nil {
|
||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||
os.Exit(1)
|
||
}
|
||
}
|