postmortem/main.go
2026-07-21 14:52:07 +03:00

97 lines
2.6 KiB
Go
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
func main() {
printBanner()
// 1. Сбор данных
pm := collect()
// 2. Сохранение
section("💾 Сохранение")
filename := askFilename(pm.Title)
content := renderMarkdown(pm)
if err := os.WriteFile(filename, []byte(content), 0644); err != nil {
fmt.Printf(" %s Ошибка записи файла: %v\n", clr(red, "✘"), err)
os.Exit(1)
}
fmt.Println()
fmt.Println(clr(green+bold, " ✔ Готово!"))
fmt.Printf(" %s Файл сохранён: %s\n", clr(green, "→"), bld(filename))
fmt.Println()
// 3. Предложить открыть в редакторе
choice := choose("Открыть файл в редакторе?", []string{"Да", "Нет"})
if choice == "Да" {
openInEditor(filename)
}
fmt.Println()
fmt.Println(clr(dim, " Удачного постмортема! 🚀"))
fmt.Println()
}
func printBanner() {
fmt.Println()
fmt.Println(clr(bold+blue, "╔══════════════════════════════════════════════╗"))
fmt.Println(clr(bold+blue, "║") + clr(bold+white, " 🔥 Post Mortem Generator v1.0 ") + clr(bold+blue, "║"))
fmt.Println(clr(bold+blue, "╚══════════════════════════════════════════════╝"))
fmt.Println(clr(dim, " Blameless post mortem — цель: улучшить систему"))
fmt.Println()
}
func askFilename(title string) string {
slug := strings.ToLower(title)
slug = strings.ReplaceAll(slug, " ", "_")
for _, ch := range []string{"/", "\\", ":", "*", "?", "\"", "<", ">", "|"} {
slug = strings.ReplaceAll(slug, ch, "")
}
if len(slug) > 40 {
slug = slug[:40]
}
defaultName := fmt.Sprintf("postmortem_%s_%s.md", time.Now().Format("2006-01-02"), slug)
name := askDefault("Имя файла:", defaultName)
if !strings.HasSuffix(name, ".md") {
name += ".md"
}
return name
}
func openInEditor(path string) {
editor := os.Getenv("EDITOR")
if editor == "" {
switch runtime.GOOS {
case "windows":
editor = "notepad"
case "darwin":
editor = "open"
default:
for _, e := range []string{"nano", "vim", "vi"} {
if _, err := exec.LookPath(e); err == nil {
editor = e
break
}
}
}
}
if editor == "" {
fmt.Printf(" %s Редактор не найден — откройте файл вручную.\n", clr(yellow, "⚠"))
return
}
cmd := exec.Command(editor, path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
}