Skip to content

Commit

Permalink
feat: edit config
Browse files Browse the repository at this point in the history
lunar is angry at me for not having this feature
  • Loading branch information
apprehensions committed Aug 22, 2023
1 parent d8a894f commit 6c9fcd2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/vinegar/vinegar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
77 changes: 77 additions & 0 deletions internal/config/editor/editor.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 6c9fcd2

Please sign in to comment.