-
Notifications
You must be signed in to change notification settings - Fork 2
/
grass.go
98 lines (90 loc) · 2.35 KB
/
grass.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
package main
import (
"errors"
"github.com/docker/docker/api/types/container"
"github.com/rivo/tview"
"github.com/toqueteos/webbrowser"
)
const (
GRASS_IMAGE_NAME = "mrcolorrain/grass:latest"
GRASS_REFERRAL_LINK = "https://app.getgrass.io/register/?referralCode=u154dPm508iVxXy"
)
type GrassConfig struct {
Email string
Password string
Configured bool
}
func (i *GrassConfig) ConfigureForm(form *tview.Form, frame *tview.Frame, app *tview.Application) {
email := ""
password := ""
isError := false
showingError := false
form.AddInputField("Email", i.Email, 50, nil, func(text string) {
email = text
})
form.AddPasswordField("Password", i.Password, 20, '*', func(text string) {
password = text
})
form.AddButton("Save", func() {
isError = stringIsEmpty(email) || stringIsEmpty(password)
if isError {
if !showingError {
form.AddTextView("Error", "All fields are required", 0, 1, true, true)
showingError = true
}
return
}
i.Email = email
i.Password = password
i.Configured = true
returnToMenu(frame, app)
})
form.AddButton("Cancel", func() {
returnToMenu(frame, app)
})
form.AddButton("Register", func() {
modal := tview.NewModal().
SetText("Register on Grass\n" + GRASS_REFERRAL_LINK).
AddButtons([]string{"Open", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Open" {
webbrowser.Open(GRASS_REFERRAL_LINK)
}
app.SetRoot(form, true)
})
app.SetRoot(modal, true)
})
}
func (i *GrassConfig) ConfigureDocker(kind DockerConfigKind, form *tview.Form) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `grass:
image: ` + GRASS_IMAGE_NAME + `
environment:
- GRASS_USER=` + i.Email + `
- GRASS_PASS=` + i.Password + `
restart: unless-stopped
`, nil
case KIND_DIRECTLY_CONFIGURE_DOCKER:
containerConfig := &container.Config{
Image: GRASS_IMAGE_NAME,
Env: []string{
"GRASS_USER=" + i.Email,
"GRASS_PASS=" + i.Password,
},
}
hostConfig := &container.HostConfig{
RestartPolicy: container.RestartPolicy{
Name: "unless-stopped",
},
}
return "", createContainer("grass", containerConfig, hostConfig, form)
default:
return "", errors.New("unknown kind")
}
}
func (i *GrassConfig) IsConfigured() bool {
return i.Configured
}
func (i *GrassConfig) PostConfigure(form *tview.Form, app *tview.Application) {
}