mirror of
https://github.com/go-task/task.git
synced 2025-06-25 00:47:04 +02:00
Replaced regex with easier code
This commit is contained in:
committed by
Andrey Nering
parent
478d1466d9
commit
fa936a54c0
@ -6,13 +6,10 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
@ -20,35 +17,20 @@ import (
|
|||||||
var (
|
var (
|
||||||
// TaskvarsFilePath file containing additional variables
|
// TaskvarsFilePath file containing additional variables
|
||||||
TaskvarsFilePath = "Taskvars"
|
TaskvarsFilePath = "Taskvars"
|
||||||
// DynamicVariablePattern is a pattern to test if a variable should get filled from running the content. It must contain a command group
|
|
||||||
DynamicVariablePattern = "^@(?P<command>.*)" // alternative proposal: ^$((?P<command>.*))$
|
|
||||||
// ErrCommandGroupNotFound returned when the command group is not present
|
|
||||||
ErrCommandGroupNotFound = fmt.Errorf("%s does not contain the command group", DynamicVariablePattern)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleDynamicVariableContent(value string) (string, error) {
|
func handleDynamicVariableContent(value string) (string, error) {
|
||||||
if value == "" {
|
if value == "" {
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
re := regexp.MustCompile(DynamicVariablePattern)
|
if value[0] != '@' {
|
||||||
if !re.MatchString(value) {
|
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
subExpressionIndex := 0
|
|
||||||
for index, value := range re.SubexpNames() {
|
|
||||||
if value == "command" {
|
|
||||||
subExpressionIndex = index
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if subExpressionIndex == 0 {
|
|
||||||
return "", ErrCommandGroupNotFound
|
|
||||||
}
|
|
||||||
var cmd *exec.Cmd
|
var cmd *exec.Cmd
|
||||||
if ShExists {
|
if ShExists {
|
||||||
cmd = exec.Command(ShPath, "-c", re.FindStringSubmatch(value)[subExpressionIndex])
|
cmd = exec.Command(ShPath, "-c", value[1:])
|
||||||
} else {
|
} else {
|
||||||
cmd = exec.Command("cmd", "/C", re.FindStringSubmatch(value)[subExpressionIndex])
|
cmd = exec.Command("cmd", "/C", value[1:])
|
||||||
}
|
}
|
||||||
cmd.Stdin = os.Stdin
|
cmd.Stdin = os.Stdin
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
|
Reference in New Issue
Block a user