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