(srcPath, includePath string)
| 126 | } |
| 127 | |
| 128 | func appendConfigInclude(srcPath, includePath string) (appended bool, err error) { |
| 129 | nixConf, err := os.OpenFile(srcPath, os.O_RDWR, 0) |
| 130 | if err != nil { |
| 131 | return false, err |
| 132 | } |
| 133 | defer nixConf.Close() |
| 134 | |
| 135 | confb, err := io.ReadAll(nixConf) |
| 136 | if err != nil { |
| 137 | return false, err |
| 138 | } |
| 139 | for _, line := range strings.Split(string(confb), "\n") { |
| 140 | line = strings.TrimSpace(line) |
| 141 | if line == "" { |
| 142 | // <whitespace> |
| 143 | continue |
| 144 | } |
| 145 | if strings.HasPrefix(line, "#") { |
| 146 | // # comment |
| 147 | continue |
| 148 | } |
| 149 | |
| 150 | path := strings.TrimSpace(strings.TrimPrefix(line, "include")) |
| 151 | if path == includePath { |
| 152 | // include devbox-nix.conf |
| 153 | return false, nil |
| 154 | } |
| 155 | path = strings.TrimSpace(strings.TrimPrefix(line, "!include")) |
| 156 | if path == includePath { |
| 157 | // !include devbox-nix.conf |
| 158 | return false, nil |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | include := "\ninclude " + includePath + "\n" |
| 163 | if _, err := nixConf.WriteString(include); err != nil { |
| 164 | return false, redact.Errorf("append %q to %s: %v", redact.Safe(include), srcPath, err) |
| 165 | } |
| 166 | if err := nixConf.Close(); err != nil { |
| 167 | return false, redact.Errorf("append %q to %s: %v", redact.Safe(include), srcPath, err) |
| 168 | } |
| 169 | return true, nil |
| 170 | } |
no test coverage detected