Skip to content

Commit

Permalink
Merge pull request #2 from mashiike/feature/change_shell
Browse files Browse the repository at this point in the history
Feature/change shell
  • Loading branch information
mashiike authored Feb 7, 2022
2 parents def1960 + 834e0a0 commit 9cde17a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
18 changes: 15 additions & 3 deletions executer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,28 @@ type ShellExecuter struct {
}

func NewShellExecuter() *ShellExecuter {
shell := "sh"
if s := os.Getenv("FLEXENTRY_SHELL"); s != "" {
shell = s
}
shellArgs := []string{"-c"}
if sArgs := os.Getenv("FLEXENTRY_SHELL_ARGS"); sArgs != "" {
shellArgs = strings.Split(sArgs, " ")
}
return &ShellExecuter{
shell: "sh",
shellArgs: []string{"-c"},
shell: shell,
shellArgs: shellArgs,
}
}

func (e *ShellExecuter) Execute(ctx context.Context, opt *ExecuteOption, commands ...string) error {
args := make([]string, 0, len(e.shellArgs)+len(commands))
args = append(args, e.shellArgs...)
args = append(args, strings.Join(commands, " "))
if os.Getenv("FLEXENTRY_QUOTE_COMMAND") != "" {
args = append(args, `"`+strings.Join(commands, " ")+`"`)
} else {
args = append(args, strings.Join(commands, " "))
}

log.Printf("[debug] $%s %s", e.shell, strings.Join(args, " "))
cmd := exec.CommandContext(ctx, e.shell, args...)
Expand Down
18 changes: 16 additions & 2 deletions flexentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,28 @@ func (e *Entrypoint) Execute(ctx context.Context, opt *ExecuteOption, commands .

func (e *Entrypoint) getHandler(args ...string) func(ctx context.Context, event Event) (interface{}, error) {
return func(ctx context.Context, event Event) (interface{}, error) {
log.Println("[debug] event:", event)
commands, err := e.DetectCommand(ctx, event)
if err != nil {
log.Println("[error] ", err)
return nil, err
}
environ, err := e.DetectEnviron(ctx, event)
if err != nil {
log.Println("[error] ", err)
return nil, err
}
if len(environ) > 0 {
for _, e := range environ {
log.Println("[debug] ", e)
}
} else {
environ = []string{}
}
opt := &ExecuteOption{
Stderr: os.Stderr,
Stdout: os.Stdout,
Stderr: os.Stderr,
Stdout: os.Stdout,
Environ: environ,
}
var bufInput, bufOutput bytes.Buffer
if err := json.NewEncoder(&bufInput).Encode(event); err != nil {
Expand Down

0 comments on commit 9cde17a

Please sign in to comment.