forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsh.go
136 lines (119 loc) · 2.93 KB
/
rsh.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"io"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"strings"
)
const (
DefaultShell = "/bin/sh"
)
type RshOptions struct {
CommonOptions
Container string
Namespace string
Executable string
stopCh chan struct{}
}
var (
rsh_long = templates.LongDesc(`
Opens a terminal or runs a command in a pods container
`)
rsh_example = templates.Examples(`
# Open a terminal in the first container of the foo deployment's latest pod
jx rsh foo
# Opens a terminal in the cheese container in the latest pod in the foo deployment
jx rsh -c cheese foo
`)
)
func NewCmdRsh(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &RshOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "rsh [deploymentOrPodName]",
Short: "Opens a terminal in a pod or runs a command in the pod",
Long: rsh_long,
Example: rsh_example,
Aliases: []string{"log"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdutil.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Container, "container", "c", "", "The name of the container to log")
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "the namespace to look for the Deployment. Defaults to the current namespace")
cmd.Flags().StringVarP(&options.Executable, "shell", "s", DefaultShell, "Path to the shell command")
return cmd
}
func (o *RshOptions) Run() error {
args := o.Args
client, curNs, err := o.Factory.CreateClient()
if err != nil {
return err
}
ns := o.Namespace
if ns == "" {
ns = curNs
}
filter := ""
names, err := kube.GetPodNames(client, ns, "")
if err != nil {
return err
}
if len(names) == 0 {
if filter == "" {
return fmt.Errorf("There are no Pods")
} else {
return fmt.Errorf("There are no Pods matching filter: " + filter)
}
}
name := ""
if len(args) == 0 {
n, err := util.PickName(names, "Pick Pod:")
if err != nil {
return err
}
name = n
} else {
name = args[0]
if util.StringArrayIndex(names, name) < 0 {
// lets try use the name as a filter
filteredNames := []string{}
for _, n := range names {
if strings.Contains(n, name) {
filteredNames = append(filteredNames, n)
}
}
n, err := util.PickName(filteredNames, "Pick Pod:")
if err != nil {
return err
}
name = n
}
}
if name == "" {
return fmt.Errorf("No pod found for namespace %s with name %s", ns, name)
}
a := []string{"exec", "-it", "-n", ns}
if o.Container != "" {
a = append(a, "-c", o.Container)
}
a = append(a, name)
if len(args) > 1 {
a = append(a, args[1:]...)
} else {
a = append(a, o.Executable)
}
return o.runCommandInteractive(true, "kubectl", a...)
}