From ba36cba7bf01686669ba6d1b1dcef019cd724f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Fri, 9 Feb 2024 10:53:06 -0800 Subject: [PATCH] initial code commit - basic functionality works - automatically adds mods found in mods folder that aren't in mod-list.json - read/write modlist from mod-list.json - enable/disable mods --- cmd/fml/fml.go | 48 ++++++++++ go.mod | 34 +++++++ go.sum | 63 +++++++++++++ internal/modlist/modlist.go | 176 ++++++++++++++++++++++++++++++++++++ internal/prompt/prompt.go | 44 +++++++++ magefile.go | 38 ++++++++ 6 files changed, 403 insertions(+) create mode 100644 cmd/fml/fml.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/modlist/modlist.go create mode 100644 internal/prompt/prompt.go create mode 100644 magefile.go diff --git a/cmd/fml/fml.go b/cmd/fml/fml.go new file mode 100644 index 0000000..1e014d2 --- /dev/null +++ b/cmd/fml/fml.go @@ -0,0 +1,48 @@ +package main + +import ( + "os" + "time" + + "git.burning.moe/celediel/fml/internal/modlist" + "git.burning.moe/celediel/fml/internal/prompt" + + "github.com/charmbracelet/log" +) + +// TODO: set this per os to support Windows + Mac +var modsdir string = os.Getenv("HOME") + "/.factorio/mods" +var modlistfile string = modsdir + "/mod-list.json" + +func main() { + // load this from config or cli option or env or something + log.SetLevel(log.WarnLevel) + log.SetTimeFormat(time.TimeOnly) + log.SetReportCaller(true) + + mods, err := modlist.ReadFromFile(modlistfile) + if err != nil { + log.Fatal(err) + } + + err = modlist.AddModsNotInList(modsdir, &mods) + if err != nil { + log.Fatal(err) + } + + selected, err := prompt.Show(&mods) + if err != nil { + log.Fatal(err) + } + + // TODO: handle this in a better way + // TODO: list intersections or something + mods.DisableAll() + + mods.EnableMods(selected...) + + err = modlist.WriteToFile(modlistfile, &mods) + if err != nil { + log.Fatal(err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1c2096d --- /dev/null +++ b/go.mod @@ -0,0 +1,34 @@ +module git.burning.moe/celediel/fml + +go 1.21.5 + +require ( + github.com/charmbracelet/huh v0.3.0 + github.com/charmbracelet/log v0.3.1 + github.com/magefile/mage v1.15.0 +) + +require ( + github.com/atotto/clipboard v0.1.4 // indirect + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/catppuccin/go v0.2.0 // indirect + github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6 // indirect + github.com/charmbracelet/bubbletea v0.25.0 // indirect + github.com/charmbracelet/lipgloss v0.9.1 // indirect + github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect + github.com/go-logfmt/logfmt v0.6.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/rivo/uniseg v0.4.4 // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect + golang.org/x/sync v0.4.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/term v0.13.0 // indirect + golang.org/x/text v0.13.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c705d2d --- /dev/null +++ b/go.sum @@ -0,0 +1,63 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA= +github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc= +github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6 h1:6nVCV8pqGaeyxetur3gpX3AAaiyKgzjIoCPV3NXKZBE= +github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6/go.mod h1:9HxZWlkCqz2PRwsCbYl7a3KXvGzFaDHpYbSYMJ+nE3o= +github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM= +github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg= +github.com/charmbracelet/huh v0.3.0 h1:CxPplWkgW2yUTDDG0Z4S5HH8SJOosWHd4LxCvi0XsKE= +github.com/charmbracelet/huh v0.3.0/go.mod h1:fujUdKX8tC45CCSaRQdw789O6uaCRwx8l2NDyKfC4jA= +github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg= +github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I= +github.com/charmbracelet/log v0.3.1 h1:TjuY4OBNbxmHWSwO3tosgqs5I3biyY8sQPny/eCMTYw= +github.com/charmbracelet/log v0.3.1/go.mod h1:OR4E1hutLsax3ZKpXbgUqPtTjQfrh1pG3zwHGWuuq8g= +github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= +github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg= +github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= +github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= +golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/modlist/modlist.go b/internal/modlist/modlist.go new file mode 100644 index 0000000..1164532 --- /dev/null +++ b/internal/modlist/modlist.go @@ -0,0 +1,176 @@ +package modlist + +import ( + "encoding/json" + "os" + "regexp" + "strings" + + "github.com/charmbracelet/log" +) + +// fileRegex matches +const fileRegex string = "([\\w-]+)_([0-9.]+).zip" + +/// Modlist types and related functions. + +// Mod holds data about a single mod. +type Mod struct { + Name string `json:"name"` + Enabled bool `json:"enabled"` +} + +// Modlist holds a slice of mods. +type Modlist struct { + Mods []Mod `json:"mods"` +} + +/// Non exported functions + +func (modlist *Modlist) setModStatus(name string, status bool) { + for i := range modlist.Mods { + if modlist.Mods[i].Name == name && modlist.Mods[i].Enabled != status { + modlist.Mods[i].Enabled = status + log.Debugf("Setting status of mod %s to %v", modlist.Mods[i].Name, modlist.Mods[i].Enabled) + } + } +} + +func (modlist *Modlist) setAllStatus(status bool) { + for i := range modlist.Mods { + modlist.Mods[i].Enabled = status + log.Debugf("Setting status of mod %s to %v", modlist.Mods[i].Name, modlist.Mods[i].Enabled) + } +} + +// EnableMod enables the given mod. +func (modlist *Modlist) EnableMod(name string) { + modlist.setModStatus(name, true) +} + +// EnableMods enables the given mods. +func (modlist *Modlist) EnableMods(names ...string) { + for _, name := range names { + modlist.EnableMod(name) + } +} + +// DisableMod disables the given mod. +func (modlist *Modlist) DisableMod(name string) { + modlist.setModStatus(name, false) +} + +// DisableMods disables the given mods. +func (modlist *Modlist) DisableMods(names ...string) { + for _, name := range names { + modlist.DisableMod(name) + } +} + +// EnableAll enables all mods. +func (modlist *Modlist) EnableAll() { + modlist.setAllStatus(true) +} + +// DisableAll disables all mods. +func (modlist *Modlist) DisableAll() { + modlist.setAllStatus(false) +} + +// HasMod reports if a modlist has a mod with name `s` +func (modlist *Modlist) HasMod(s string) bool { + for _, mod := range modlist.Mods { + if mod.Name == s { + return true + } + } + return false +} + +// HasMod reports if a modlist has an enabled mod with name `s` +func (modlist *Modlist) HasModEnabled(s string) bool { + for _, mod := range modlist.Mods { + if mod.Name == s && mod.Enabled { + return true + } + } + return false +} + +// Print out each mod and its status. +func (modlist *Modlist) Print(prefix string) { + log.Info(prefix + ": {") + for _, mod := range modlist.Mods { + var endis string + // I wish go had a ternary so this could all be in one line + if mod.Enabled { + endis = "en" + } else { + endis = "dis" + } + log.Infof("\t%s: %sabled,", mod.Name, endis) + } + log.Info("}") +} + +func (modlist *Modlist) String() { + modlist.Print("mods") +} + +/// Functions related to the modlist but that shouldn't be attached to the types + +// WriteToFile writes the modlist to the given filename. +func WriteToFile(filename string, mods *Modlist) error { + data, err := json.MarshalIndent(&mods, "", " ") + if err != nil { + return err + } + + os.WriteFile(filename, data, os.FileMode(0744)) + return nil +} + +// ReadFromFile reads the modlist from the given filename. +func ReadFromFile(filename string) (Modlist, error) { + var mods Modlist = Modlist{} + + data, err := os.ReadFile(filename) + if err != nil { + return Modlist{}, err + } + + err = json.Unmarshal(data, &mods) + if err != nil { + return Modlist{}, err + } + + return mods, nil +} + +// AddModsNotInList finds mod archives in the mod folder that aren't in the modlist, and adds them. +func AddModsNotInList(modsdir string, mods *Modlist) error { + r := regexp.MustCompile(fileRegex) + + files, err := os.ReadDir(modsdir) + if err != nil { + return err + } + + for _, file := range files { + if strings.HasSuffix(file.Name(), ".zip") && !file.IsDir() { + groups := r.FindAllStringSubmatch(file.Name(), -1) + name := groups[0][1] + version := groups[0][2] + + if !mods.HasMod(name) { + log.Printf("%s isn't enabled, adding v%s in disabled state.", name, version) + mods.Mods = append(mods.Mods, Mod{ + Name: name, + Enabled: false, + }) + } + } + } + + return nil +} diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go new file mode 100644 index 0000000..0a90d11 --- /dev/null +++ b/internal/prompt/prompt.go @@ -0,0 +1,44 @@ +package prompt + +import ( + "git.burning.moe/celediel/fml/internal/modlist" + + "github.com/charmbracelet/huh" + "github.com/charmbracelet/log" +) + +func makeOptions(mods *modlist.Modlist) []huh.Option[string] { + options := []huh.Option[string]{} + + for _, mod := range mods.Mods { + option := huh.NewOption[string](mod.Name, mod.Name).Selected(mod.Enabled) + options = append(options, option) + } + + return options +} + +// Show shows the huh prompt to enable/disable mods from provided Modlist +// returns the names of enabled mods +func Show(mods *modlist.Modlist) ([]string, error) { + // TODO: use huh to enable/disable the mods somehow + var selected []string + + form := huh.NewForm( + huh.NewGroup( + huh.NewMultiSelect[string](). + Options(makeOptions(mods)...). + Title("Factorio Mod List"). + Value(&selected), + ), + ) + + err := form.Run() + if err != nil { + return []string{}, err + } + + log.Debug(selected) + + return selected, nil +} diff --git a/magefile.go b/magefile.go new file mode 100644 index 0000000..5fea045 --- /dev/null +++ b/magefile.go @@ -0,0 +1,38 @@ +//go:build mage + +package main + +import ( + "fmt" + "os" + + "github.com/magefile/mage/sh" +) + +var ( + binaryName string = "fml" + buildDir string = "bin" + cmd string = fmt.Sprintf(".%[1]ccmd%[1]c%[2]s", os.PathSeparator, binaryName) + output string = fmt.Sprintf(".%[1]c%[2]s%[1]c%[3]s", os.PathSeparator, buildDir, binaryName) +) + +func Build() error { + fmt.Println("Building...") + return sh.Run("go", "build", "-o", output, cmd) +} + +func Run() error { + fmt.Println("Running...") + return sh.RunV("go", "run", cmd) +} + +func RunBinary() error { + Build() + fmt.Println("Running binary...") + return sh.RunV(output) +} + +func Clean() error { + fmt.Println("Cleaning...") + return os.Remove(output) +}