From e2e4f70f6b14d46d7dbbd2e0d5e7c3c3c69b47cc Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Fri, 18 Oct 2024 14:07:53 -0500 Subject: Initial commit Signed-off-by: Matt Strapp --- main.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 main.go (limited to 'main.go') diff --git a/main.go b/main.go new file mode 100644 index 0000000..2b4487b --- /dev/null +++ b/main.go @@ -0,0 +1,103 @@ +package main + +import ( + "io" + "log" + "os" + + "github.com/adrg/xdg" + "github.com/charmbracelet/huh" + "github.com/locusts-r-us/locusts" + "github.com/pelletier/go-toml/v2" +) + +type Config struct { + HostIP string `toml:"host_ip"` + WslArgs []string `toml:"extra_wsl_args"` + SshArgs []string `toml:"extra_ssh_args"` +} + +type Distribution struct { + Name string `toml:"name"` + Username string `toml:"username"` + Port int `toml:"port"` +} + +type ConfigFile struct { + Config + Distributions []Distribution `toml:"distribution"` +} + +func main() { + locusts.IntroduceLocusts() + + // Create config file location + path, err := xdg.ConfigFile("wsl-tui/config.toml") + if err != nil { + log.Fatal(err) + } + + configFile, err := os.Open(path) + if err != nil { + // Open local config file + configFile, err = os.Open("config.toml") + if err != nil { + log.Fatal(err) + } + } + + configContents, err := io.ReadAll(configFile) + if err != nil { + log.Fatal(err) + } + configFile.Close() + + config := ConfigFile{} + err = toml.Unmarshal(configContents, &config) + if err != nil { + log.Fatal(err) + } + + // fmt.Println(config) + + var distro Distribution + + err = huh.NewForm( + huh.NewGroup( + huh.NewSelect[Distribution](). + Title("Select a distribution to launch!"). + Value(&distro). + OptionsFunc(func() []huh.Option[Distribution] { + options := make([]huh.Option[Distribution], len(config.Distributions)) + for i, distro := range config.Distributions { + options[i] = huh.Option[Distribution]{ + Key: distro.Name, + Value: distro, + } + } + return options + }, &config, + ), + ), + ).WithTheme(huh.ThemeDracula()).Run() + + if err != nil { + if err == huh.ErrUserAborted { + os.Exit(130) + } + log.Fatal(err) + } + + cmd, err := command(distro, config.Config) + if err != nil { + log.Fatal(err) + } + // Pass in the TTY shite + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + if err := cmd.Run(); err != nil { + log.Fatal(cmd.Args[0], " exited with error: ", err) + } +} -- cgit v1.2.3