From 6c9fcd2a6663cc55f27867ff30673168ddb8cd76 Mon Sep 17 00:00:00 2001 From: sewn Date: Tue, 22 Aug 2023 10:34:54 +0300 Subject: [PATCH] feat: edit config lunar is angry at me for not having this feature --- cmd/vinegar/vinegar.go | 5 ++- internal/config/editor/editor.go | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 internal/config/editor/editor.go diff --git a/cmd/vinegar/vinegar.go b/cmd/vinegar/vinegar.go index a3ca6156..00cbb910 100644 --- a/cmd/vinegar/vinegar.go +++ b/cmd/vinegar/vinegar.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/vinegarhq/vinegar/internal/config" + "github.com/vinegarhq/vinegar/internal/config/editor" "github.com/vinegarhq/vinegar/internal/config/state" "github.com/vinegarhq/vinegar/internal/dirs" "github.com/vinegarhq/vinegar/internal/logs" @@ -22,7 +23,7 @@ var Version string func usage() { fmt.Fprintln(os.Stderr, "usage: vinegar player|studio|exec [args...]") - fmt.Fprintln(os.Stderr, " vinegar kill|uninstall|delete|version") + fmt.Fprintln(os.Stderr, " vinegar edit|kill|uninstall|delete|version") os.Exit(1) } @@ -61,6 +62,8 @@ func main() { Binary(roblox.Player, &cfg, &pfx, os.Args[2:]...) case "studio": Binary(roblox.Studio, &cfg, &pfx, os.Args[2:]...) + case "edit": + editor.EditConfig() case "exec": if err := pfx.ExecWine(os.Args[2:]...); err != nil { log.Fatal(err) diff --git a/internal/config/editor/editor.go b/internal/config/editor/editor.go new file mode 100644 index 00000000..7b058846 --- /dev/null +++ b/internal/config/editor/editor.go @@ -0,0 +1,77 @@ +package editor + +import ( + "errors" + "fmt" + "log" + "os" + "os/exec" + + "github.com/BurntSushi/toml" + "github.com/vinegarhq/vinegar/internal/config" + "github.com/vinegarhq/vinegar/internal/dirs" +) + +func EditConfig() { + var cfg config.Config + + if err := dirs.Mkdirs(dirs.Config); err != nil { + log.Fatal(err) + } + + file, err := os.OpenFile(config.Path, os.O_WRONLY|os.O_CREATE, 0o644) + if err != nil { + log.Fatal(err) + } + + info, err := file.Stat() + if err != nil { + file.Close() + log.Fatal(err) + } + + template := "# See how to configure Vinegar on the documentation website:\n" + + "# https://vinegarhq.github.io/Configuration\n\n" + + if info.Size() < 1 { + log.Println("Writing Configuration template") + + if _, err := file.WriteString(template); err != nil { + log.Fatal(err) + } + } + + file.Close() + + for { + if err := Editor(config.Path); err != nil { + log.Fatal(err) + } + + if _, err := toml.DecodeFile(config.Path, &cfg); err != nil { + log.Println(err) + log.Println("Press enter to re-edit configuration file") + fmt.Scanln() + + continue + } + + break + } +} + +func Editor(path string) error { + editor, ok := os.LookupEnv("EDITOR") + + if !ok { + return errors.New("no $EDITOR variable set") + } + + cmd := exec.Command(editor, path) + + cmd.Stdin = os.Stdin + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + return cmd.Run() +}