Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package laminate
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
Expand Down Expand Up @@ -77,21 +76,6 @@ func (cmd *Command) GetExt() string {
return "png"
}

// GetShell returns the shell to use for command execution
func (cmd *Command) GetShell() string {
if cmd.Shell != "" {
return cmd.Shell
}
if path, err := exec.LookPath("bash"); err == nil {
return path
}
// Fallback to sh
if path, err := exec.LookPath("sh"); err == nil {
return path
}
return "/bin/sh"
}

// LoadConfig loads the configuration from the config file
func LoadConfig() (*Config, error) {
configPath := getConfigPath()
Expand Down
37 changes: 35 additions & 2 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"

"github.com/k1LoW/exec"
Expand Down Expand Up @@ -51,8 +53,7 @@ func (e *Executor) getArgv() ([]string, error) {
if err != nil {
return nil, err
}
shell := e.cmd.GetShell()
return []string{shell, "-c", expanded}, nil
return e.cmd.buildCommand(expanded)
}

func (e *Executor) exceute(ctx context.Context, argv []string) ([]byte, error) {
Expand Down Expand Up @@ -114,3 +115,35 @@ func ExecuteWithCache(ctx context.Context, config *Config, lang, input string, o
_, err = output.Write(data)
return err
}

var standaloneCommandReg = regexp.MustCompile(`^[-_.+a-zA-Z0-9]+$`)

func (cmd *Command) buildCommand(c string) ([]string, error) {
if standaloneCommandReg.MatchString(c) {
return []string{c}, nil
}
sh, err := cmd.detectShell()
if err != nil {
return nil, err
}
return []string{sh, "-c", c}, nil
}

// detectShell returns the shell to use for command execution
func (cmd *Command) detectShell() (string, error) {
if cmd.Shell != "" {
return cmd.Shell, nil
}
if sh := os.Getenv("SHELL"); sh != "" {
return sh, nil
}
for _, sh := range []string{"bash", "sh"} {
if path, err := exec.LookPath(sh); err == nil {
return path, nil
}
}
if runtime.GOOS == "windows" {
return "cmd", nil
}
return "", fmt.Errorf("no suitable shell found")
}
Loading