-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (89 loc) · 1.95 KB
/
main.go
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
package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)
type runtimeModel struct {
choices []string
cursor int
selected string
}
func (m runtimeModel) Init() tea.Cmd {
return nil
}
func (m runtimeModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "j", "down":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case "k", "up":
if m.cursor > 0 {
m.cursor--
}
case "enter":
m.selected = m.choices[m.cursor]
return m, tea.Quit
}
}
return m, nil
}
func (m runtimeModel) View() string {
s := "Choose a container runtime:\n\n"
for i, choice := range m.choices {
cursor := " "
if m.cursor == i {
cursor = ">"
}
s += fmt.Sprintf("%s %s\n", cursor, choice)
}
return s
}
func installRuntime(runtime string) {
fmt.Printf("You selected %s, installing...\n", runtime)
switch runtime {
case "firecracker":
installFirecracker()
case "runc":
installRunc()
case "youki":
installYouki()
default:
fmt.Println("No valid runtime selected.")
os.Exit(1)
}
}
func main() {
var rootCmd = &cobra.Command{
Use: "hakolatecli",
Short: "hakolatecli is a CLI tool for setting up container runtimes and nerdctl",
}
var installCmd = &cobra.Command{
Use: "install",
Short: "Install container runtime and nerdctl",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("hakolate setup: First, we will install nerdctl...")
installNerdctl()
m := runtimeModel{
choices: []string{"firecracker", "runc", "youki"},
}
p := tea.NewProgram(m)
model, err := p.Run()
if err != nil {
fmt.Printf("Could not start the selection program: %s\n", err)
os.Exit(1)
}
selectedRuntime := model.(runtimeModel).selected
installRuntime(selectedRuntime)
},
}
rootCmd.AddCommand(installCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}