GetConfig loads and merges local and global Task configuration files
(dir string)
| 24 | |
| 25 | // GetConfig loads and merges local and global Task configuration files |
| 26 | func GetConfig(dir string) (*ast.TaskRC, error) { |
| 27 | var config *ast.TaskRC |
| 28 | reader := NewReader() |
| 29 | |
| 30 | // Read the XDG config file |
| 31 | if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" { |
| 32 | xdgConfigNode, err := NewNode("", filepath.Join(xdgConfigHome, "task"), defaultXDGTaskRCs) |
| 33 | if err == nil && xdgConfigNode != nil { |
| 34 | xdgConfig, err := reader.Read(xdgConfigNode) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | config = xdgConfig |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // If the current path does not contain $HOME |
| 43 | // If it does contain $HOME, then we will find this config later anyway |
| 44 | home, err := os.UserHomeDir() |
| 45 | if err == nil && !strings.Contains(home, dir) { |
| 46 | homeNode, err := NewNode("", home, defaultTaskRCs) |
| 47 | if err == nil && homeNode != nil { |
| 48 | homeConfig, err := reader.Read(homeNode) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | if config == nil { |
| 53 | config = homeConfig |
| 54 | } else { |
| 55 | config.Merge(homeConfig) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Find all the nodes from the given directory up to the users home directory |
| 61 | absDir, err := filepath.Abs(dir) |
| 62 | if err != nil { |
| 63 | return config, err |
| 64 | } |
| 65 | entrypoints, err := fsext.SearchAll("", absDir, defaultTaskRCs) |
| 66 | if errors.Is(err, os.ErrPermission) { |
| 67 | err = nil |
| 68 | } |
| 69 | if err != nil { |
| 70 | return config, err |
| 71 | } |
| 72 | |
| 73 | // Reverse the entrypoints since we want the child files to override parent ones |
| 74 | slices.Reverse(entrypoints) |
| 75 | |
| 76 | // Loop over the nodes, and merge them into the main config |
| 77 | for _, entrypoint := range entrypoints { |
| 78 | node, err := NewNode("", entrypoint, defaultTaskRCs) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | localConfig, err := reader.Read(node) |
| 83 | if err != nil { |
searching dependent graphs…