ui: support --sudo to run all docker subcommands with sudo

This commit is contained in:
mappu 2025-01-06 19:07:03 +13:00
parent c23090739a
commit 6f8a984f3c

78
main.go
View File

@ -19,6 +19,11 @@ const (
NetworksTab int = 2 NetworksTab int = 2
VolumesTab int = 3 VolumesTab int = 3
) )
var (
dockerSudo bool
)
func NewStatusDelegate(status string) *qt.QWidget { func NewStatusDelegate(status string) *qt.QWidget {
mw := qt.NewQWidget2() mw := qt.NewQWidget2()
@ -44,8 +49,32 @@ func NewStatusDelegate(status string) *qt.QWidget {
return mw return mw
} }
func dockerCommand(args ...string) *exec.Cmd {
if dockerSudo {
newargs := []string{"docker"}
newargs = append(newargs, args...)
return exec.Command("sudo", newargs...)
}
return dockerCommand(args...)
}
func openTerminal(container_id string) { func openTerminal(container_id string) {
popupCommand("docker exec -it " + container_id + " sh -c '[ -x /bin/bash ] && exec /bin/bash || exec /bin/sh'") command := "docker exec -it " + container_id + " sh -c '[ -x /bin/bash ] && exec /bin/bash || exec /bin/sh'"
if dockerSudo {
command = "sudo " + command
}
popupCommand(command)
}
func openLogs(container_id string) {
command := "docker logs -f " + container_id
if dockerSudo {
command = "sudo " + command
}
popupCommand(command)
} }
func popupCommand(command string) { func popupCommand(command string) {
@ -64,10 +93,6 @@ func popupCommand(command string) {
} }
} }
func openLogs(container_id string) {
popupCommand("docker logs -f " + container_id)
}
type DockerGUITab struct { type DockerGUITab struct {
*qt.QWidget *qt.QWidget
@ -459,11 +484,11 @@ func (self *DockerGUI) handle_action(action string) {
if action == "Terminal" { if action == "Terminal" {
self.open_terminal() self.open_terminal()
} else if action == "Start" { } else if action == "Start" {
exec.Command("docker", "start", container_id).Run() dockerCommand("start", container_id).Run()
} else if action == "Stop" { } else if action == "Stop" {
exec.Command("docker", "stop", container_id).Run() dockerCommand("stop", container_id).Run()
} else if action == "Remove" { } else if action == "Remove" {
exec.Command("docker", "rm", "-f", container_id).Run() dockerCommand("rm", "-f", container_id).Run()
} }
} else if current_tab == NetworksTab { } else if current_tab == NetworksTab {
@ -474,7 +499,7 @@ func (self *DockerGUI) handle_action(action string) {
network_id := selected_items[0].Text(0) network_id := selected_items[0].Text(0)
if action == "Remove" { if action == "Remove" {
exec.Command("docker", "network", "rm", network_id).Run() dockerCommand("network", "rm", network_id).Run()
} }
} else if current_tab == VolumesTab { } else if current_tab == VolumesTab {
@ -485,7 +510,7 @@ func (self *DockerGUI) handle_action(action string) {
volume_name := selected_items[0].Text(0) volume_name := selected_items[0].Text(0)
if action == "Remove" { if action == "Remove" {
exec.Command("docker", "volume", "rm", volume_name).Run() dockerCommand("volume", "rm", volume_name).Run()
} }
} }
@ -504,7 +529,7 @@ func (self *DockerGUI) refresh_containers() {
selected_items := self.get_selected_items(self.containers_tree) selected_items := self.get_selected_items(self.containers_tree)
self.containers_tree.Clear() self.containers_tree.Clear()
output, err := exec.Command("docker", "ps", "-a", "--format", "{{.ID}}\\t{{.Names}}\\t{{.Image}}\\t{{.Status}}\\t{{.Ports}}").Output() output, err := dockerCommand("ps", "-a", "--format", "{{.ID}}\\t{{.Names}}\\t{{.Image}}\\t{{.Status}}\\t{{.Ports}}").Output()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -542,7 +567,7 @@ func (self *DockerGUI) refresh_images() {
selected_items := self.get_selected_items(self.images_tree) selected_items := self.get_selected_items(self.images_tree)
self.images_tree.Clear() self.images_tree.Clear()
output, err := exec.Command("docker", "images", "--format", "{{.ID}}\\t{{.Repository}}\\t{{.Tag}}\\t{{.Size}}").Output() output, err := dockerCommand("images", "--format", "{{.ID}}\\t{{.Repository}}\\t{{.Tag}}\\t{{.Size}}").Output()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -574,7 +599,7 @@ func (self *DockerGUI) refresh_networks() {
selected_items := self.get_selected_items(self.networks_tree) selected_items := self.get_selected_items(self.networks_tree)
self.networks_tree.Clear() self.networks_tree.Clear()
output, err := exec.Command("docker", "network", "ls", "--format", "{{.ID}}\\t{{.Name}}\\t{{.Driver}}").Output() output, err := dockerCommand("network", "ls", "--format", "{{.ID}}\\t{{.Name}}\\t{{.Driver}}").Output()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -604,7 +629,7 @@ func (self *DockerGUI) refresh_volumes() {
selected_items := self.get_selected_items(self.volumes_tree) selected_items := self.get_selected_items(self.volumes_tree)
self.volumes_tree.Clear() self.volumes_tree.Clear()
output, err := exec.Command("docker", "volume", "ls", "--format", "{{.Name}}\\t{{.Driver}}\\t{{.Mountpoint}}").Output() output, err := dockerCommand("volume", "ls", "--format", "{{.Name}}\\t{{.Driver}}\\t{{.Mountpoint}}").Output()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -666,7 +691,7 @@ func (self *DockerGUI) start_container() {
for _, item := range selected_items { for _, item := range selected_items {
container_id := item.Text(0) container_id := item.Text(0)
err := exec.Command("docker", "start", container_id).Wait() err := dockerCommand("start", container_id).Wait()
if err != nil { if err != nil {
qt.QMessageBox_Critical(self.QWidget, "Error", "Failed to start container "+container_id+": "+err.Error()) qt.QMessageBox_Critical(self.QWidget, "Error", "Failed to start container "+container_id+": "+err.Error())
} }
@ -685,7 +710,7 @@ func (self *DockerGUI) stop_container() {
for _, item := range selected_items { for _, item := range selected_items {
container_id := item.Text(0) container_id := item.Text(0)
err := exec.Command("docker", "stop", container_id).Run() err := dockerCommand("stop", container_id).Run()
if err != nil { if err != nil {
qt.QMessageBox_Critical(self.QWidget, "Error", "Failed to stop container "+container_id+": "+err.Error()) qt.QMessageBox_Critical(self.QWidget, "Error", "Failed to stop container "+container_id+": "+err.Error())
} }
@ -714,7 +739,7 @@ func (self *DockerGUI) remove_container() {
continue continue
} }
err := exec.Command("docker", "rm", "-f", container_id).Run() err := dockerCommand("rm", "-f", container_id).Run()
if err != nil { if err != nil {
qt.QMessageBox_Critical(self.QWidget, "Error", "Failed to remove container "+container_id+": "+err.Error()) qt.QMessageBox_Critical(self.QWidget, "Error", "Failed to remove container "+container_id+": "+err.Error())
} }
@ -729,7 +754,7 @@ func (self *DockerGUI) pull_image() {
return return
} }
err := exec.Command("docker", "pull", image_name).Run() err := dockerCommand("pull", image_name).Run()
if err == nil { if err == nil {
qt.QMessageBox_Information(self.QWidget, "Success", "Image \""+image_name+"\" pulled successfully.") qt.QMessageBox_Information(self.QWidget, "Success", "Image \""+image_name+"\" pulled successfully.")
} else { } else {
@ -757,7 +782,7 @@ func (self *DockerGUI) remove_image() {
continue continue
} }
err := exec.Command("docker", "rmi", image_id).Run() err := dockerCommand("rmi", image_id).Run()
if err == nil { if err == nil {
qt.QMessageBox_Information(self.QWidget, "Success", "Image \""+image_id+"\" removed successfully.") qt.QMessageBox_Information(self.QWidget, "Success", "Image \""+image_id+"\" removed successfully.")
} else { } else {
@ -775,7 +800,7 @@ func (self *DockerGUI) create_network() {
return return
} }
err := exec.Command("docker", "network", "create", network_name).Run() err := dockerCommand("network", "create", network_name).Run()
if err == nil { if err == nil {
qt.QMessageBox_Information(self.QWidget, "Success", "Network \""+network_name+"\" created successfully.") qt.QMessageBox_Information(self.QWidget, "Success", "Network \""+network_name+"\" created successfully.")
} else { } else {
@ -803,7 +828,7 @@ func (self *DockerGUI) remove_network() {
continue continue
} }
err := exec.Command("docker", "network", "rm", network_name).Run() err := dockerCommand("network", "rm", network_name).Run()
if err == nil { if err == nil {
qt.QMessageBox_Information(self.QWidget, "Success", "Network \""+network_name+"\" removed successfully.") qt.QMessageBox_Information(self.QWidget, "Success", "Network \""+network_name+"\" removed successfully.")
} else { } else {
@ -820,7 +845,7 @@ func (self *DockerGUI) create_volume() {
return return
} }
err := exec.Command("docker", "volume", "create", volume_name).Run() err := dockerCommand("volume", "create", volume_name).Run()
if err == nil { if err == nil {
qt.QMessageBox_Information(self.QWidget, "Success", "Volume \""+volume_name+"\" created successfully.") qt.QMessageBox_Information(self.QWidget, "Success", "Volume \""+volume_name+"\" created successfully.")
} else { } else {
@ -848,7 +873,7 @@ func (self *DockerGUI) remove_volume() {
continue continue
} }
err := exec.Command("docker", "volume", "rm", volume_name).Run() err := dockerCommand("volume", "rm", volume_name).Run()
if err == nil { if err == nil {
qt.QMessageBox_Information(self.QWidget, "Success", "Volume \""+volume_name+"\" removed successfully.") qt.QMessageBox_Information(self.QWidget, "Success", "Volume \""+volume_name+"\" removed successfully.")
} else { } else {
@ -892,6 +917,13 @@ func (self *DockerGUI) show_logs_error(error_message string) {
func main() { func main() {
qt.NewQApplication(os.Args) qt.NewQApplication(os.Args)
for _, arg := range os.Args {
if arg == `--sudo` {
dockerSudo = true
}
}
window := NewDockerGUI() window := NewDockerGUI()
window.Show() window.Show()
os.Exit(qt.QApplication_Exec()) os.Exit(qt.QApplication_Exec())