mirror of
https://github.com/go-task/task.git
synced 2025-08-08 22:36:57 +02:00
wip
This commit is contained in:
@@ -34,16 +34,7 @@ func Parse(dir string) {
|
||||
// Read any .env files
|
||||
readDotEnv(dir)
|
||||
|
||||
home, _ := os.UserHomeDir()
|
||||
fmt.Printf("home : %#v\n", home)
|
||||
// Create a node for the Task config reader
|
||||
node, _ := taskrc.NewNode("", dir)
|
||||
fmt.Printf("node : %#v\n", node)
|
||||
node2, _ := taskrc.NewNode("", home)
|
||||
fmt.Printf("node2 : %#v\n", node2)
|
||||
// Read the Task config file
|
||||
reader := taskrc.NewReader()
|
||||
config, _ := reader.Read(node)
|
||||
config, _ := taskrc.GetConfig(dir)
|
||||
|
||||
// Initialize the experiments
|
||||
GentleForce = New("GENTLE_FORCE", config, 1)
|
||||
|
@@ -6,3 +6,20 @@ type TaskRC struct {
|
||||
Version *semver.Version `yaml:"version"`
|
||||
Experiments map[string]int `yaml:"experiments"`
|
||||
}
|
||||
|
||||
// Merge combines the current TaskRC with another TaskRC, prioritizing non-nil fields from the other TaskRC.
|
||||
func (t *TaskRC) Merge(other *TaskRC) {
|
||||
if other == nil {
|
||||
return
|
||||
}
|
||||
if t.Version == nil && other.Version != nil {
|
||||
t.Version = other.Version
|
||||
}
|
||||
if t.Experiments == nil && other.Experiments != nil {
|
||||
t.Experiments = other.Experiments
|
||||
} else if t.Experiments != nil && other.Experiments != nil {
|
||||
for k, v := range other.Experiments {
|
||||
t.Experiments[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,41 @@
|
||||
package taskrc
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/go-task/task/v3/taskrc/ast"
|
||||
)
|
||||
|
||||
var defaultTaskRCs = []string{
|
||||
".taskrc.yml",
|
||||
".taskrc.yaml",
|
||||
}
|
||||
|
||||
// GetConfig loads and merges local and global Task configuration files
|
||||
func GetConfig(dir string) (*ast.TaskRC, error) {
|
||||
reader := NewReader()
|
||||
|
||||
// LocalNode is the node for the local Task configuration file
|
||||
localNode, _ := NewNode("", dir)
|
||||
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// GlobalNode is the node for the global Task configuration file (~/.taskrc.yml)
|
||||
globalNode, _ := NewNode("", home)
|
||||
|
||||
localConfig, _ := reader.Read(localNode)
|
||||
|
||||
globalConfig, _ := reader.Read(globalNode)
|
||||
|
||||
if globalConfig == nil {
|
||||
return localConfig, nil
|
||||
}
|
||||
|
||||
// Merge the global configuration into the local configuration
|
||||
globalConfig.Merge(localConfig)
|
||||
|
||||
return globalConfig, nil
|
||||
}
|
||||
|
89
taskrc/taskrc_test.go
Normal file
89
taskrc/taskrc_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package taskrc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/go-task/task/v3/taskrc/ast"
|
||||
)
|
||||
|
||||
const (
|
||||
localConfigYAML = `
|
||||
experiments:
|
||||
GENTLE_FORCE: 1
|
||||
ENV_PRECEDENCE: 0
|
||||
`
|
||||
|
||||
globalConfigYAML = `
|
||||
experiments:
|
||||
GENTLE_FORCE: 0
|
||||
REMOTE_TASKFILES: 1
|
||||
ENV_PRECEDENCE: 1
|
||||
|
||||
`
|
||||
)
|
||||
|
||||
func setupDirs(t *testing.T) (localDir, globalDir string) {
|
||||
t.Helper()
|
||||
localDir = t.TempDir()
|
||||
globalDir = t.TempDir()
|
||||
|
||||
t.Setenv("HOME", globalDir)
|
||||
|
||||
return localDir, globalDir
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, dir, filename, content string) {
|
||||
t.Helper()
|
||||
err := os.WriteFile(filepath.Join(dir, filename), []byte(content), 0o644)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetConfig_MergesGlobalAndLocal(t *testing.T) { //nolint:paralleltest // cannot run in parallel
|
||||
localDir, globalDir := setupDirs(t)
|
||||
|
||||
// Write local config
|
||||
writeFile(t, localDir, ".taskrc.yml", localConfigYAML)
|
||||
|
||||
// Write global config
|
||||
writeFile(t, globalDir, ".taskrc.yml", globalConfigYAML)
|
||||
|
||||
cfg, err := GetConfig(localDir)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, cfg)
|
||||
fmt.Printf("cfg : %#v\n", cfg)
|
||||
assert.Equal(t, &ast.TaskRC{Version: nil, Experiments: map[string]int{"GENTLE_FORCE": 1, "ENV_PRECEDENCE": 0, "REMOTE_TASKFILES": 1}}, cfg)
|
||||
}
|
||||
|
||||
func TestGetConfig_NoConfigFiles(t *testing.T) { //nolint:paralleltest // cannot run in parallel
|
||||
localDir, _ := setupDirs(t)
|
||||
|
||||
cfg, err := GetConfig(localDir)
|
||||
fmt.Printf("cfg : %#v\n", cfg)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, cfg)
|
||||
}
|
||||
|
||||
func TestGetConfig_OnlyGlobal(t *testing.T) { //nolint:paralleltest // cannot run in parallel
|
||||
localDir, globalDir := setupDirs(t)
|
||||
|
||||
writeFile(t, globalDir, ".taskrc.yml", globalConfigYAML)
|
||||
|
||||
cfg, err := GetConfig(localDir)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, &ast.TaskRC{Version: nil, Experiments: map[string]int{"GENTLE_FORCE": 0, "ENV_PRECEDENCE": 1, "REMOTE_TASKFILES": 1}}, cfg)
|
||||
}
|
||||
|
||||
func TestGetConfig_OnlyLocal(t *testing.T) { //nolint:paralleltest // cannot run in parallel
|
||||
localDir, _ := setupDirs(t)
|
||||
|
||||
writeFile(t, localDir, ".taskrc.yml", localConfigYAML)
|
||||
|
||||
cfg, err := GetConfig(localDir)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, &ast.TaskRC{Version: nil, Experiments: map[string]int{"GENTLE_FORCE": 1, "ENV_PRECEDENCE": 0}}, cfg)
|
||||
}
|
Reference in New Issue
Block a user