(path, host, port string)
| 194 | } |
| 195 | |
| 196 | func addHost(path, host, port string) (string, error) { |
| 197 | var reader io.Reader |
| 198 | f, err := os.Open(path) |
| 199 | if err != nil { |
| 200 | if !os.IsNotExist(err) { |
| 201 | return "", err |
| 202 | } |
| 203 | |
| 204 | reader = strings.NewReader("") |
| 205 | } else { |
| 206 | reader = f |
| 207 | defer f.Close() |
| 208 | } |
| 209 | |
| 210 | configScanner := scanner.NewScanner(reader) |
| 211 | newLines := []string{} |
| 212 | inSection := false |
| 213 | startMarker := "# DevSpace Start " + host |
| 214 | endMarker := "# DevSpace End " + host |
| 215 | for configScanner.Scan() { |
| 216 | text := configScanner.Text() |
| 217 | if strings.HasPrefix(text, startMarker) { |
| 218 | inSection = true |
| 219 | } else if strings.HasPrefix(text, endMarker) { |
| 220 | inSection = false |
| 221 | } else if !inSection { |
| 222 | newLines = append(newLines, text) |
| 223 | } |
| 224 | } |
| 225 | if configScanner.Err() != nil { |
| 226 | return "", errors.Wrap(err, "parse ssh config") |
| 227 | } |
| 228 | |
| 229 | // add new section |
| 230 | newLines = append(newLines, startMarker) |
| 231 | newLines = append(newLines, "Host "+host) |
| 232 | newLines = append(newLines, " HostName localhost") |
| 233 | newLines = append(newLines, " LogLevel error") |
| 234 | newLines = append(newLines, " Port "+port) |
| 235 | newLines = append(newLines, " IdentityFile \""+DevSpaceSSHPrivateKeyFile+"\"") |
| 236 | newLines = append(newLines, " StrictHostKeyChecking no") |
| 237 | newLines = append(newLines, " UserKnownHostsFile /dev/null") |
| 238 | newLines = append(newLines, " User devspace") |
| 239 | newLines = append(newLines, endMarker) |
| 240 | return strings.Join(newLines, "\n"), nil |
| 241 | } |
no test coverage detected