Skip to content

Commit

Permalink
Exec using API
Browse files Browse the repository at this point in the history
  • Loading branch information
fr05t1k committed Oct 13, 2018
1 parent e68f7ba commit 967a87a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 17 deletions.
76 changes: 68 additions & 8 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions connector/manager/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manager
import (
"fmt"
api "github.com/fsouza/go-dockerclient"
"os"
)

type Docker struct {
Expand All @@ -17,6 +18,28 @@ func NewDocker(client *api.Client, id string) *Docker {
}
}

func (dc *Docker) Exec(cmd []string) error {
execCmd, err := dc.client.CreateExec(api.CreateExecOptions{
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Cmd: cmd,
Container: dc.id,
Tty: true,
})

if err != nil {
return err
}

return dc.client.StartExec(execCmd.ID, api.StartExecOptions{
InputStream: os.Stdin,
OutputStream: os.Stdout,
ErrorStream: os.Stderr,
RawTerminal: true,
})
}

func (dc *Docker) Start() error {
c, err := dc.client.InspectContainer(dc.id)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions connector/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type Manager interface {
Pause() error
Unpause() error
Restart() error
Exec(cmd []string) error
}
4 changes: 4 additions & 0 deletions connector/manager/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func (m *Mock) Unpause() error {
func (m *Mock) Restart() error {
return nil
}

func (m *Mock) Exec(cmd []string) error {
return nil
}
4 changes: 4 additions & 0 deletions connector/manager/runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func (rc *Runc) Unpause() error {
func (rc *Runc) Restart() error {
return nil
}

func (rc *Runc) Exec(cmd []string) error {
return nil
}
4 changes: 4 additions & 0 deletions container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,7 @@ func (c *Container) Restart() {
}
}
}

func (c *Container) Exec(cmd []string) error {
return c.manager.Exec(cmd)
}
10 changes: 1 addition & 9 deletions menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package main

import (
"fmt"
"os"
"os/exec"
"time"

"github.com/bcicen/ctop/config"
Expand Down Expand Up @@ -225,13 +223,7 @@ func ExecSh() MenuFn {
ui.StopLoop()
defer ui.Loop()

// Reset colors && clear screen && run sh
cmdName := fmt.Sprintf("echo '\033[0m' && clear && docker exec -it %s sh", c.GetMeta("name"))
cmd := exec.Command("bash", "-c", cmdName)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Run()
c.Exec([]string{"sh", "-c", "echo '\033[0m' && clear && sh"})

return nil
}
Expand Down

0 comments on commit 967a87a

Please sign in to comment.