@@ -19,6 +19,7 @@ import (
19
19
"time"
20
20
21
21
"github.com/gliderlabs/ssh"
22
+ "github.com/kballard/go-shellquote"
22
23
"github.com/pkg/sftp"
23
24
"github.com/prometheus/client_golang/prometheus"
24
25
"github.com/spf13/afero"
@@ -515,8 +516,29 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string)
515
516
if runtime .GOOS == "windows" {
516
517
caller = "/c"
517
518
}
519
+ name := shell
518
520
args := []string {caller , script }
519
521
522
+ if strings .HasPrefix (strings .TrimSpace (script ), "#!" ) {
523
+ // If the script starts with a shebang, we should
524
+ // execute it directly. This is useful for running
525
+ // scripts that aren't executable.
526
+ shebang := strings .SplitN (script , "\n " , 2 )[0 ]
527
+ shebang = strings .TrimPrefix (shebang , "#!" )
528
+ shebang = strings .TrimSpace (shebang )
529
+ words , err := shellquote .Split (shebang )
530
+ if err != nil {
531
+ return nil , xerrors .Errorf ("split shebang: %w" , err )
532
+ }
533
+ name = words [0 ]
534
+ if len (words ) > 1 {
535
+ args = words [1 :]
536
+ } else {
537
+ args = []string {}
538
+ }
539
+ args = append (args , caller , script )
540
+ }
541
+
520
542
// gliderlabs/ssh returns a command slice of zero
521
543
// when a shell is requested.
522
544
if len (script ) == 0 {
@@ -528,7 +550,7 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string)
528
550
}
529
551
}
530
552
531
- cmd := pty .CommandContext (ctx , shell , args ... )
553
+ cmd := pty .CommandContext (ctx , name , args ... )
532
554
cmd .Dir = manifest .Directory
533
555
534
556
// If the metadata directory doesn't exist, we run the command
0 commit comments