-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (75 loc) · 2.15 KB
/
main.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
// Author: Zhang Yifa
// Email: yzhang3@linkernetworks.com
package main
import (
"fmt"
"log"
"os"
"runtime"
"github.com/fsouza/go-dockerclient"
"github.com/spf13/cobra"
"github.com/zyfdegh/go-dockerpty"
)
func main() {
//call console
if err := rootCmd.Execute(); err != nil {
log.Fatalf("execute command error: %v", err)
os.Exit(-1)
}
}
// RootCmd is root command of local-docker-exec
var rootCmd = &cobra.Command{
Use: "local-docker-exec [ContainerId]",
Short: "local-docker-exec can connect to docker container, and act as a docker exec.",
Long: "local-docker-exec can connect to docker container, and act as a docker exec.",
Example: "./local-docker-exec ffcede5a47cb",
Run: func(cmd *cobra.Command, args []string) {
if runtime.GOOS != "linux" {
log.Fatalln("Only linux is supported now.")
return
}
// example
// local-docker-exec c3598346f0d7
if cmd.Flags().NArg() == 1 {
containerId := cmd.Flags().Args()[0]
localDockerExec(containerId)
}
// others
cmd.SetArgs([]string{"--help"})
if err := cmd.Execute(); err != nil {
log.Fatalf("command arguments error: %v", err)
os.Exit(-1)
}
return
},
}
func localDockerExec(containerId string) {
fmt.Println("Welcome to local-docker-exec!")
endpoint := "unix:///var/run/docker.sock"
fmt.Printf("Connecting to %s, please wait...\n", endpoint)
client, err := docker.NewClient(endpoint)
if err != nil {
log.Fatalf("new client error: %v\n", err)
}
fmt.Printf("Connecting to container %s, please wait...\n", containerId)
// create exec
createOpts := docker.CreateExecOptions{}
createOpts.AttachStdin = true
createOpts.AttachStdout = true
createOpts.AttachStderr = true
createOpts.Tty = true
// select shell sequence
// bash -> sh -> zsh -> fish -> csh -> tcsh -> scsh -> ksh -> rc
createOpts.Cmd = []string{"sh", "-c", "bash || sh || zsh || fish || csh || tcsh || scsh || ksh || rc"}
createOpts.Container = containerId
exec, err := client.CreateExec(createOpts)
if err != nil {
log.Fatalf("create exec error: %v\n", err)
}
// start tty
err = dockerpty.StartExec(client, exec)
if err != nil {
log.Printf("start exec error: %v\n", err)
return
}
}