-
Notifications
You must be signed in to change notification settings - Fork 901
feat: add shebang support to scripts #10134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"time" | ||
|
||
"github.com/gliderlabs/ssh" | ||
"github.com/kballard/go-shellquote" | ||
"github.com/pkg/sftp" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/spf13/afero" | ||
|
@@ -515,8 +516,29 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string) | |
if runtime.GOOS == "windows" { | ||
caller = "/c" | ||
} | ||
name := shell | ||
args := []string{caller, script} | ||
|
||
if strings.HasPrefix(strings.TrimSpace(script), "#!") { | ||
// If the script starts with a shebang, we should | ||
// execute it directly. This is useful for running | ||
// scripts that aren't executable. | ||
shebang := strings.SplitN(script, "\n", 2)[0] | ||
shebang = strings.TrimPrefix(shebang, "#!") | ||
shebang = strings.TrimSpace(shebang) | ||
words, err := shellquote.Split(shebang) | ||
if err != nil { | ||
return nil, xerrors.Errorf("split shebang: %w", err) | ||
} | ||
name = words[0] | ||
if len(words) > 1 { | ||
args = words[1:] | ||
} else { | ||
args = []string{} | ||
} | ||
args = append(args, caller, script) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I guess this still assumes that the shebang is for a shell that supports What if this was the contents:
This works on a typical Unix system, but here it would trigger the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this gets a bit weird but we totally can. Should we just temporarily write it then defer a cleanup? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, a bit weird, but it'd be awesome to support the use-case fully. I think temp write + cleanup sounds good (and no need to make it executable since we're handling the shebang parsing part)! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Going to fix! |
||
} | ||
|
||
// gliderlabs/ssh returns a command slice of zero | ||
// when a shell is requested. | ||
if len(script) == 0 { | ||
|
@@ -528,7 +550,7 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string) | |
} | ||
} | ||
|
||
cmd := pty.CommandContext(ctx, shell, args...) | ||
cmd := pty.CommandContext(ctx, name, args...) | ||
cmd.Dir = manifest.Directory | ||
|
||
// If the metadata directory doesn't exist, we run the command | ||
|
Uh oh!
There was an error while loading. Please reload this page.