aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: 3220553c58b357ea711a33b08a78f3e40248c4cd (plain) (blame)
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
101
102
103
104
//go:generate go-winres make --product-version=git-tag
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)
	}

	accessibleMode := os.Getenv("ACCESSIBLE") != ""

	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,
				),
		),
	).WithAccessible(accessibleMode).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)
	}
}