ui: support --sudo to run all docker subcommands with sudo
This commit is contained in:
parent
c23090739a
commit
6f8a984f3c
78
main.go
78
main.go
@ -19,6 +19,11 @@ const (
|
||||
NetworksTab int = 2
|
||||
VolumesTab int = 3
|
||||
)
|
||||
|
||||
var (
|
||||
dockerSudo bool
|
||||
)
|
||||
|
||||
func NewStatusDelegate(status string) *qt.QWidget {
|
||||
mw := qt.NewQWidget2()
|
||||
|
||||
@ -44,8 +49,32 @@ func NewStatusDelegate(status string) *qt.QWidget {
|
||||
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) {
|
||||
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) {
|
||||
@ -64,10 +93,6 @@ func popupCommand(command string) {
|
||||
}
|
||||
}
|
||||
|
||||
func openLogs(container_id string) {
|
||||
popupCommand("docker logs -f " + container_id)
|
||||
}
|
||||
|
||||
type DockerGUITab struct {
|
||||
*qt.QWidget
|
||||
|
||||
@ -459,11 +484,11 @@ func (self *DockerGUI) handle_action(action string) {
|
||||
if action == "Terminal" {
|
||||
self.open_terminal()
|
||||
} else if action == "Start" {
|
||||
exec.Command("docker", "start", container_id).Run()
|
||||
dockerCommand("start", container_id).Run()
|
||||
} else if action == "Stop" {
|
||||
exec.Command("docker", "stop", container_id).Run()
|
||||
dockerCommand("stop", container_id).Run()
|
||||
} else if action == "Remove" {
|
||||
exec.Command("docker", "rm", "-f", container_id).Run()
|
||||
dockerCommand("rm", "-f", container_id).Run()
|
||||
}
|
||||
|
||||
} else if current_tab == NetworksTab {
|
||||
@ -474,7 +499,7 @@ func (self *DockerGUI) handle_action(action string) {
|
||||
|
||||
network_id := selected_items[0].Text(0)
|
||||
if action == "Remove" {
|
||||
exec.Command("docker", "network", "rm", network_id).Run()
|
||||
dockerCommand("network", "rm", network_id).Run()
|
||||
}
|
||||
|
||||
} else if current_tab == VolumesTab {
|
||||
@ -485,7 +510,7 @@ func (self *DockerGUI) handle_action(action string) {
|
||||
|
||||
volume_name := selected_items[0].Text(0)
|
||||
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)
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
@ -542,7 +567,7 @@ func (self *DockerGUI) refresh_images() {
|
||||
selected_items := self.get_selected_items(self.images_tree)
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
@ -574,7 +599,7 @@ func (self *DockerGUI) refresh_networks() {
|
||||
selected_items := self.get_selected_items(self.networks_tree)
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
@ -604,7 +629,7 @@ func (self *DockerGUI) refresh_volumes() {
|
||||
selected_items := self.get_selected_items(self.volumes_tree)
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
@ -666,7 +691,7 @@ func (self *DockerGUI) start_container() {
|
||||
for _, item := range selected_items {
|
||||
container_id := item.Text(0)
|
||||
|
||||
err := exec.Command("docker", "start", container_id).Wait()
|
||||
err := dockerCommand("start", container_id).Wait()
|
||||
if err != nil {
|
||||
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 {
|
||||
container_id := item.Text(0)
|
||||
|
||||
err := exec.Command("docker", "stop", container_id).Run()
|
||||
err := dockerCommand("stop", container_id).Run()
|
||||
if err != nil {
|
||||
qt.QMessageBox_Critical(self.QWidget, "Error", "Failed to stop container "+container_id+": "+err.Error())
|
||||
}
|
||||
@ -714,7 +739,7 @@ func (self *DockerGUI) remove_container() {
|
||||
continue
|
||||
}
|
||||
|
||||
err := exec.Command("docker", "rm", "-f", container_id).Run()
|
||||
err := dockerCommand("rm", "-f", container_id).Run()
|
||||
if err != nil {
|
||||
qt.QMessageBox_Critical(self.QWidget, "Error", "Failed to remove container "+container_id+": "+err.Error())
|
||||
}
|
||||
@ -729,7 +754,7 @@ func (self *DockerGUI) pull_image() {
|
||||
return
|
||||
}
|
||||
|
||||
err := exec.Command("docker", "pull", image_name).Run()
|
||||
err := dockerCommand("pull", image_name).Run()
|
||||
if err == nil {
|
||||
qt.QMessageBox_Information(self.QWidget, "Success", "Image \""+image_name+"\" pulled successfully.")
|
||||
} else {
|
||||
@ -757,7 +782,7 @@ func (self *DockerGUI) remove_image() {
|
||||
continue
|
||||
}
|
||||
|
||||
err := exec.Command("docker", "rmi", image_id).Run()
|
||||
err := dockerCommand("rmi", image_id).Run()
|
||||
if err == nil {
|
||||
qt.QMessageBox_Information(self.QWidget, "Success", "Image \""+image_id+"\" removed successfully.")
|
||||
} else {
|
||||
@ -775,7 +800,7 @@ func (self *DockerGUI) create_network() {
|
||||
return
|
||||
}
|
||||
|
||||
err := exec.Command("docker", "network", "create", network_name).Run()
|
||||
err := dockerCommand("network", "create", network_name).Run()
|
||||
if err == nil {
|
||||
qt.QMessageBox_Information(self.QWidget, "Success", "Network \""+network_name+"\" created successfully.")
|
||||
} else {
|
||||
@ -803,7 +828,7 @@ func (self *DockerGUI) remove_network() {
|
||||
continue
|
||||
}
|
||||
|
||||
err := exec.Command("docker", "network", "rm", network_name).Run()
|
||||
err := dockerCommand("network", "rm", network_name).Run()
|
||||
if err == nil {
|
||||
qt.QMessageBox_Information(self.QWidget, "Success", "Network \""+network_name+"\" removed successfully.")
|
||||
} else {
|
||||
@ -820,7 +845,7 @@ func (self *DockerGUI) create_volume() {
|
||||
return
|
||||
}
|
||||
|
||||
err := exec.Command("docker", "volume", "create", volume_name).Run()
|
||||
err := dockerCommand("volume", "create", volume_name).Run()
|
||||
if err == nil {
|
||||
qt.QMessageBox_Information(self.QWidget, "Success", "Volume \""+volume_name+"\" created successfully.")
|
||||
} else {
|
||||
@ -848,7 +873,7 @@ func (self *DockerGUI) remove_volume() {
|
||||
continue
|
||||
}
|
||||
|
||||
err := exec.Command("docker", "volume", "rm", volume_name).Run()
|
||||
err := dockerCommand("volume", "rm", volume_name).Run()
|
||||
if err == nil {
|
||||
qt.QMessageBox_Information(self.QWidget, "Success", "Volume \""+volume_name+"\" removed successfully.")
|
||||
} else {
|
||||
@ -892,6 +917,13 @@ func (self *DockerGUI) show_logs_error(error_message string) {
|
||||
|
||||
func main() {
|
||||
qt.NewQApplication(os.Args)
|
||||
|
||||
for _, arg := range os.Args {
|
||||
if arg == `--sudo` {
|
||||
dockerSudo = true
|
||||
}
|
||||
}
|
||||
|
||||
window := NewDockerGUI()
|
||||
window.Show()
|
||||
os.Exit(qt.QApplication_Exec())
|
||||
|
Loading…
x
Reference in New Issue
Block a user