hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/internal/cli/check.go
blob: 1df5137c7c49683a6cdfe1dc1f5ec12296fd9616 (plain)
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
package cli

import (
	"fmt"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"

	"github.com/hachem-wtf/benson/internal/config"
	"github.com/hachem-wtf/benson/internal/modrinth"
)

type compatMod struct {
	slug     string
	filename string
}

type checkResult struct {
	compatible   []compatMod
	incompatible []string
	failed       []string
}

func runCheck(m *model) tea.Cmd {
	m.busy = true
	cfg := m.cfg
	return tea.Batch(m.spin.Tick, func() tea.Msg {
		return outputMsg{text: checkReport(cfg)}
	})
}

func checkReport(cfg *config.Config) string {
	client := modrinth.NewClient()
	var res checkResult

	for _, slug := range cfg.ModrinthMods {
		versions, err := client.GetVersions(slug)
		if err != nil {
			res.failed = append(res.failed, slug)
			continue
		}

		match := modrinth.CompatibleVersion(versions, cfg.MinecraftVersion, "fabric")
		if match == nil {
			res.incompatible = append(res.incompatible, slug)
			continue
		}

		filename := match.VersionNumber
		if len(match.Files) > 0 {
			filename = match.Files[0].Filename
		}
		res.compatible = append(res.compatible, compatMod{slug: slug, filename: filename})
	}

	return renderCheck(cfg, res)
}

func renderCheck(cfg *config.Config, res checkResult) string {
	rows := []string{
		headingStyle.Render(fmt.Sprintf("Compatibility · MC %s · Fabric", cfg.MinecraftVersion)),
		"",
	}

	if len(res.compatible) > 0 {
		rows = append(rows, successStyle.Render("Compatible"))
		for _, c := range res.compatible {
			rows = append(rows, "  "+successStyle.Render("✓ ")+c.slug+mutedStyle.Render(" → "+c.filename))
		}
		rows = append(rows, "")
	}

	if len(res.incompatible) > 0 {
		rows = append(rows, errorStyle.Render("No compatible version"))
		for _, slug := range res.incompatible {
			rows = append(rows, "  "+errorStyle.Render("✗ ")+slug)
		}
		rows = append(rows, "")
	}

	if len(res.failed) > 0 {
		rows = append(rows, warnStyle.Render("Failed to check"))
		for _, slug := range res.failed {
			rows = append(rows, "  "+warnStyle.Render("! ")+slug)
		}
		rows = append(rows, "")
	}

	total := len(cfg.ModrinthMods)
	ok := len(res.compatible)
	rows = append(rows, mutedStyle.Render(fmt.Sprintf("Summary: %d/%d mods compatible", ok, total)))

	if len(res.incompatible) == 0 && len(res.failed) == 0 {
		rows = append(rows, successStyle.Render("Safe to generate."))
	} else {
		rows = append(rows, warnStyle.Render("Not all mods are compatible — generate may have issues."))
	}

	return lipgloss.JoinVertical(lipgloss.Left, rows...)
}