mirror of
				https://github.com/go-task/task.git
				synced 2025-10-30 23:58:01 +02:00 
			
		
		
		
	First implementation of dynamic variables
This commit is contained in:
		
				
					committed by
					
						 Andrey Nering
						Andrey Nering
					
				
			
			
				
	
			
			
			
						parent
						
							2f317aa32c
						
					
				
				
					commit
					478d1466d9
				
			| @@ -5,10 +5,14 @@ import ( | |||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"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" | ||||||
| ) | ) | ||||||
| @@ -16,22 +20,71 @@ 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) { | ||||||
|  | 	if value == "" { | ||||||
|  | 		return value, nil | ||||||
|  | 	} | ||||||
|  | 	re := regexp.MustCompile(DynamicVariablePattern) | ||||||
|  | 	if !re.MatchString(value) { | ||||||
|  | 		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 | ||||||
|  | 	if ShExists { | ||||||
|  | 		cmd = exec.Command(ShPath, "-c", re.FindStringSubmatch(value)[subExpressionIndex]) | ||||||
|  | 	} else { | ||||||
|  | 		cmd = exec.Command("cmd", "/C", re.FindStringSubmatch(value)[subExpressionIndex]) | ||||||
|  | 	} | ||||||
|  | 	cmd.Stdin = os.Stdin | ||||||
|  | 	cmd.Stderr = os.Stderr | ||||||
|  | 	bytes, err := cmd.Output() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  | 	return strings.TrimSpace(string(bytes)), nil | ||||||
|  | } | ||||||
|  |  | ||||||
| func (t Task) handleVariables() (map[string]string, error) { | func (t Task) handleVariables() (map[string]string, error) { | ||||||
| 	localVariables := make(map[string]string) | 	localVariables := make(map[string]string) | ||||||
| 	for key, value := range t.Vars { | 	for key, value := range t.Vars { | ||||||
| 		localVariables[key] = value | 		val, err := handleDynamicVariableContent(value) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return nil, err | ||||||
|  | 		} | ||||||
|  | 		localVariables[key] = val | ||||||
| 	} | 	} | ||||||
| 	if fileVariables, err := readTaskvarsFile(); err == nil { | 	if fileVariables, err := readTaskvarsFile(); err == nil { | ||||||
| 		for key, value := range fileVariables { | 		for key, value := range fileVariables { | ||||||
| 			localVariables[key] = value | 			val, err := handleDynamicVariableContent(value) | ||||||
|  | 			if err != nil { | ||||||
|  | 				return nil, err | ||||||
|  | 			} | ||||||
|  | 			localVariables[key] = val | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	for key, value := range getEnvironmentVariables() { | 	for key, value := range getEnvironmentVariables() { | ||||||
| 		localVariables[key] = value | 		val, err := handleDynamicVariableContent(value) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return nil, err | ||||||
|  | 		} | ||||||
|  | 		localVariables[key] = val | ||||||
| 	} | 	} | ||||||
| 	return localVariables, nil | 	return localVariables, nil | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user