Skip to content

Commit

Permalink
Redesign docker container creation UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpl0itU committed Mar 26, 2024
1 parent 894ab7b commit 116e386
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 40 deletions.
8 changes: 5 additions & 3 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ services:
return dockerComposeFile
}

func batchCreateDockerContainers(menuItems []MenuItem, logView *tview.TextView) []error {
func batchCreateDockerContainers(menuItems []MenuItem, frame *tview.Frame) []error {
var errors []error
for _, item := range menuItems {
if !item.Config.IsConfigured() {
continue
}
_, err := item.Config.ConfigureDocker(KIND_DIRECTLY_CONFIGURE_DOCKER, logView)
_, err := item.Config.ConfigureDocker(KIND_DIRECTLY_CONFIGURE_DOCKER, frame)
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -94,12 +94,14 @@ func pullImageBlocking(imageName string, logView *tview.TextView) error {
return nil
}

func createContainer(name string, containerConfig *container.Config, hostConfig *container.HostConfig, logView *tview.TextView) error {
func createContainer(name string, containerConfig *container.Config, hostConfig *container.HostConfig, frame *tview.Frame) error {
client, err := getDockerClient()
if err != nil {
return err
}

logView := frame.GetPrimitive().(*tview.TextView)

if err := pullImageBlocking(containerConfig.Image, logView); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions earnapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (i *EarnAppConfig) ConfigureForm(form *tview.Form, frame *tview.Frame, app
})
}

func (i *EarnAppConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error) {
func (i *EarnAppConfig) ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `earnapp:
Expand Down Expand Up @@ -96,7 +96,7 @@ func (i *EarnAppConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.Te
},
},
}
return "", createContainer("earnapp", containerConfig, hostConfig, logView)
return "", createContainer("earnapp", containerConfig, hostConfig, frame)
default:
return "", errors.New("unknown kind")
}
Expand Down
4 changes: 2 additions & 2 deletions grass.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (i *GrassConfig) ConfigureForm(form *tview.Form, frame *tview.Frame, app *t
})
}

func (i *GrassConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error) {
func (i *GrassConfig) ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `grass:
Expand All @@ -74,7 +74,7 @@ func (i *GrassConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.Text
Name: "unless-stopped",
},
}
return "", createContainer("grass", containerConfig, hostConfig, logView)
return "", createContainer("grass", containerConfig, hostConfig, frame)
default:
return "", errors.New("unknown kind")
}
Expand Down
4 changes: 2 additions & 2 deletions honeygain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (i *HoneygainConfig) ConfigureForm(form *tview.Form, frame *tview.Frame, ap
})
}

func (i *HoneygainConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error) {
func (i *HoneygainConfig) ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `honeygain:
Expand Down Expand Up @@ -87,7 +87,7 @@ func (i *HoneygainConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.
i.DeviceName,
},
}
return "", createContainer("honeygain", containerConfig, hostConfig, logView)
return "", createContainer("honeygain", containerConfig, hostConfig, frame)
default:
return "", errors.New("unknown kind")
}
Expand Down
45 changes: 26 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {

form := tview.NewForm()

frame := tview.NewFrame(list).
mainFrame := tview.NewFrame(list).
AddText("Passive Machine", true, tview.AlignCenter, tview.Styles.PrimaryTextColor).
AddText("Tip: Use the Register button to sign up for the service", true, tview.AlignCenter, tview.Styles.ContrastSecondaryTextColor)

Expand All @@ -32,7 +32,7 @@ func main() {
for _, item := range menuItems {
list.AddItem(item.GetName(), item.GetDescription(), 0, func() {
form.Clear(true)
item.Config.ConfigureForm(form, frame, app)
item.Config.ConfigureForm(form, mainFrame, app)
app.SetRoot(form, true)
})
}
Expand All @@ -49,28 +49,35 @@ func main() {
}
})
form.AddButton("Return", func() {
returnToMenu(frame, app)
returnToMenu(mainFrame, app)
})
app.SetRoot(form, true)
})

list.AddItem("[*] Create Docker Containers", "Create docker containers from the selected items (Recommended)", 0, func() {
logView := tview.NewTextView()
form.Clear(true)
form.AddFormItem(logView)
errors := batchCreateDockerContainers(menuItems, logView)
if len(errors) == 0 {
form.AddTextView("Success", "All containers created successfully", 0, 0, true, false)
} else {
form.AddTextView("Errors", "Some containers failed to create", 0, 0, true, false)
for _, err := range errors {
form.AddTextView("Error", err.Error(), 0, 1, true, false)
dockerFrame := tview.NewFrame(tview.NewTextView().
SetDynamicColors(true).
SetChangedFunc(func() {
app.Draw()
})).
AddText("Creating Docker Containers", true, tview.AlignCenter, tview.Styles.PrimaryTextColor).
AddText("This may take a while", true, tview.AlignCenter, tview.Styles.ContrastSecondaryTextColor)

go func() {
errors := batchCreateDockerContainers(menuItems, dockerFrame)
if len(errors) == 0 {
form.AddTextView("Success", "All containers created successfully", 0, 0, true, false)
} else {
form.AddTextView("Errors", "Some containers failed to create", 0, 0, true, false)
for _, err := range errors {
form.AddTextView("Error", err.Error(), 0, 1, true, false)
}
}
}
form.AddButton("Return", func() {
returnToMenu(frame, app)
})
app.SetRoot(form, true)
form.AddButton("Return", func() {
returnToMenu(mainFrame, app)
})
}()
app.SetRoot(dockerFrame, true)
})
app.SetRoot(frame, true).Run()
app.SetRoot(mainFrame, true).Run()
}
2 changes: 1 addition & 1 deletion menuItem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/rivo/tview"

type MenuItemConfig interface {
ConfigureForm(form *tview.Form, frame *tview.Frame, app *tview.Application)
ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error)
ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error)
IsConfigured() bool
}

Expand Down
4 changes: 2 additions & 2 deletions mystnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (i *MystConfig) ConfigureForm(form *tview.Form, frame *tview.Frame, app *tv
})
}

func (i *MystConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error) {
func (i *MystConfig) ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `myst:
Expand Down Expand Up @@ -73,7 +73,7 @@ func (i *MystConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextV
},
},
}
return "", createContainer("myst", containerConfig, hostConfig, logView)
return "", createContainer("myst", containerConfig, hostConfig, frame)
}
return "", errors.New("unknown kind")
}
Expand Down
4 changes: 2 additions & 2 deletions packetstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (i *PacketStreamConfig) ConfigureForm(form *tview.Form, frame *tview.Frame,
})
}

func (i *PacketStreamConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error) {
func (i *PacketStreamConfig) ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `packetstream:
Expand All @@ -66,7 +66,7 @@ func (i *PacketStreamConfig) ConfigureDocker(kind DockerConfigKind, logView *tvi
Name: "unless-stopped",
},
}
return "", createContainer("packetstream", containerConfig, hostConfig, logView)
return "", createContainer("packetstream", containerConfig, hostConfig, frame)
default:
return "", errors.New("unknown kind")
}
Expand Down
4 changes: 2 additions & 2 deletions pawnsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (i *PawnsAppConfig) ConfigureForm(form *tview.Form, frame *tview.Frame, app
})
}

func (i *PawnsAppConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error) {
func (i *PawnsAppConfig) ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `pawnsapp:
Expand All @@ -80,7 +80,7 @@ func (i *PawnsAppConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.T
Name: "unless-stopped",
},
}
return "", createContainer("pawnsapp", containerConfig, hostConfig, logView)
return "", createContainer("pawnsapp", containerConfig, hostConfig, frame)
default:
return "", errors.New("unknown kind")
}
Expand Down
6 changes: 3 additions & 3 deletions peer2profit.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (i *Peer2ProfitConfig) ConfigureForm(form *tview.Form, frame *tview.Frame,
})
}

func (i *Peer2ProfitConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error) {
func (i *Peer2ProfitConfig) ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
compose := `peer2profit:
Expand Down Expand Up @@ -80,7 +80,7 @@ func (i *Peer2ProfitConfig) ConfigureDocker(kind DockerConfigKind, logView *tvie
Name: "unless-stopped",
},
}
if err := createContainer("binfmt", containerConfig, hostConfig, logView); err != nil {
if err := createContainer("binfmt", containerConfig, hostConfig, frame); err != nil {
return "", err
}
}
Expand All @@ -96,7 +96,7 @@ func (i *Peer2ProfitConfig) ConfigureDocker(kind DockerConfigKind, logView *tvie
Name: "unless-stopped",
},
}
return "", createContainer("peer2profit", containerConfig, hostConfig, logView)
return "", createContainer("peer2profit", containerConfig, hostConfig, frame)
}
return "", errors.New("unknown kind")
}
Expand Down
4 changes: 2 additions & 2 deletions watchtower.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (i *WatchtowerConfig) ConfigureForm(form *tview.Form, frame *tview.Frame, a
})
}

func (i *WatchtowerConfig) ConfigureDocker(kind DockerConfigKind, logView *tview.TextView) (string, error) {
func (i *WatchtowerConfig) ConfigureDocker(kind DockerConfigKind, frame *tview.Frame) (string, error) {
switch kind {
case KIND_DOCKER_COMPOSE:
return `watchtower:
Expand All @@ -50,7 +50,7 @@ func (i *WatchtowerConfig) ConfigureDocker(kind DockerConfigKind, logView *tview
Name: "always",
},
}
return "", createContainer("watchtower", containerConfig, hostConfig, logView)
return "", createContainer("watchtower", containerConfig, hostConfig, frame)
default:
return "", errors.New("unknown kind")
}
Expand Down

0 comments on commit 116e386

Please sign in to comment.