Skip to content

Commit 55481ce

Browse files
committed
allow to pass command via ssh
1 parent 51fdeeb commit 55481ce

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ ssh -l "alpine:latest" -o StrictHostKeychecking=no localhost -p 2222
4242

4343
# Executing scripts for CI testing
4444

45-
Currently it is not possible to specify a command for the ssh session, but the
46-
`cmd` option can be used to execute an specified command within the container
47-
for CI testing. Example:
45+
Execute pre-defined scripts by using the `cmd` option:
4846

4947
```
5048
cat /tmp/ci/test.sh
@@ -58,6 +56,16 @@ for CI testing. Example:
5856
1
5957
```
6058

59+
or execute a command via ssh call:
60+
61+
```
62+
ssh -l "debian:bookworm" -o StrictHostKeychecking=no localhost -p 2222 ls; echo $?
63+
bin dev home lib64 mnt proc run srv tmp var
64+
boot etc lib media opt root sbin sys usr
65+
0
66+
```
67+
68+
6169
or execute script on multiple containers:
6270

6371
```

main.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func main() {
6363
_, _, isTty := sess.Pty()
6464
cfg := &container.Config{
6565
Image: defaultImage,
66-
Cmd: sess.Command(),
6766
Env: sess.Environ(),
6867
Tty: isTty,
6968
OpenStdin: true,
@@ -165,19 +164,19 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
165164
status = 255
166165
cleanup = func() {}
167166
ctx := context.Background()
167+
useTty := true
168168

169169
cImage := cfg.Image
170170
InfoPrint("Image: %s", cImage)
171171
defaultCmd := []string{"/bin/sh", "-c", "/bin/bash || /bin/sh"}
172172

173+
if sess.RawCommand() != "" {
174+
defaultCmd = sess.Command()
175+
useTty = false
176+
}
173177
if cmd != "" {
174178
defaultCmd = strings.Fields(cmd)
175179
}
176-
177-
if sess.RawCommand() != "" {
178-
sess.Write([]byte("not implemented, use -cmd option\n"))
179-
return
180-
}
181180
InfoPrint("Executing command: %s", defaultCmd)
182181

183182
networkingConfig := network.NetworkingConfig{}
@@ -235,7 +234,7 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
235234
}
236235
InfoPrint("Attaching container: %s", resp.ID)
237236
stream, err := docker.ContainerExecAttach(ctx, execResp.ID, container.ExecStartOptions{
238-
Tty: true,
237+
Tty: useTty,
239238
})
240239
if err != nil {
241240
ErrorPrint("Error during container attach: [%v]", err.Error())

0 commit comments

Comments
 (0)