-
Notifications
You must be signed in to change notification settings - Fork 2
/
pawnsapp.go
104 lines (96 loc) · 2.76 KB
/
pawnsapp.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
103
104
package main
import (
"errors"
"github.com/docker/docker/api/types/container"
"github.com/rivo/tview"
"github.com/toqueteos/webbrowser"
)
const (
PAWNSAPP_IMAGE_NAME = "iproyal/pawns-cli:latest"
PAWNSAPP_REFERRAL_LINK = "https://pawns.app/?r=1112060"
)
type PawnsAppConfig struct {
Email string
Password string
DeviceName string
Configured bool
}
func (i *PawnsAppConfig) ConfigureForm(form *tview.Form, frame *tview.Frame, app *tview.Application) {
email := ""
password := ""
deviceName := ""
isError := false
showingError := false
form.AddInputField("Device Name", i.DeviceName, 15, nil, func(text string) {
deviceName = text
})
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) || stringIsEmpty(deviceName)
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.DeviceName = deviceName
i.Configured = true
returnToMenu(frame, app)
})
form.AddButton("Cancel", func() {
returnToMenu(frame, app)
})
form.AddButton("Register", func() {
modal := tview.NewModal().
SetText("Register on PawnsApp\n" + PAWNSAPP_REFERRAL_LINK).
AddButtons([]string{"Open", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Open" {
webbrowser.Open(PAWNSAPP_REFERRAL_LINK)
}
app.SetRoot(form, true)
})
app.SetRoot(modal, true)
})
}
func (i *PawnsAppConfig) ConfigureDocker(kind DockerConfigKind, form *tview.Form) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `pawnsapp:
image: ` + PAWNSAPP_IMAGE_NAME + `
environment:
- IPROYALPAWNS_DUMMY=''
command: -accept-tos -email=` + i.Email + ` -password=` + i.Password + ` -device-name=` + i.DeviceName + ` -device-id=id_` + i.DeviceName + `
restart: unless-stopped
`, nil
case KIND_DIRECTLY_CONFIGURE_DOCKER:
containerConfig := &container.Config{
Image: PAWNSAPP_IMAGE_NAME,
Cmd: []string{"-accept-tos", "-email=" + i.Email, "-password=" + i.Password, "-device-name=" + i.DeviceName, "-device-id=id_" + i.DeviceName},
Env: []string{
"IPROYALPAWNS_DUMMY=",
},
}
hostConfig := &container.HostConfig{
RestartPolicy: container.RestartPolicy{
Name: "unless-stopped",
},
}
return "", createContainer("pawnsapp", containerConfig, hostConfig, form)
default:
return "", errors.New("unknown kind")
}
}
func (i *PawnsAppConfig) IsConfigured() bool {
return i.Configured
}
func (i *PawnsAppConfig) PostConfigure(form *tview.Form, app *tview.Application) {
}