MCPcopy Create free account
hub / github.com/Mister-leo/fssh / parseHostBlock

Function parseHostBlock

internal/sshconfig/writer.go:402–480  ·  view source on GitHub ↗

parseHostBlock parses a host block into HostConfig

(lines []string, start, end int)

Source from the content-addressed store, hash-verified

400
401// parseHostBlock parses a host block into HostConfig
402func parseHostBlock(lines []string, start, end int) (*HostConfig, error) {
403 if start >= len(lines) {
404 return nil, fmt.Errorf("invalid start index")
405 }
406
407 cfg := &HostConfig{LineNumber: start}
408
409 // Parse Host line
410 hostLine := strings.TrimSpace(lines[start])
411 if !strings.HasPrefix(strings.ToLower(hostLine), "host ") {
412 return nil, fmt.Errorf("invalid host block: expected 'Host' directive")
413 }
414 hostParts := strings.Fields(hostLine[5:])
415 if len(hostParts) == 0 {
416 return nil, fmt.Errorf("empty host name")
417 }
418 cfg.Name = hostParts[0]
419
420 // Mark global configs
421 if cfg.Name == "*" {
422 cfg.IsGlobal = true
423 }
424
425 // Parse configuration directives
426 for i := start + 1; i < end; i++ {
427 line := lines[i]
428 trimmed := strings.TrimSpace(line)
429
430 // Skip blank lines and comments
431 if trimmed == "" || strings.HasPrefix(trimmed, "#") {
432 continue
433 }
434
435 // Stop at next Host directive
436 if strings.HasPrefix(strings.ToLower(trimmed), "host ") {
437 break
438 }
439
440 key, value := parseKV(trimmed)
441 switch key {
442 case "hostname":
443 cfg.Hostname = value
444 case "user":
445 cfg.User = value
446 case "port":
447 cfg.Port = value
448 case "identityfile":
449 cfg.IdentityFile = append(cfg.IdentityFile, value)
450 case "identityagent":
451 cfg.IdentityAgent = value
452 case "proxycommand":
453 cfg.ProxyCommand = value
454 case "proxyjump":
455 cfg.ProxyJump = value
456 case "forwardagent":
457 cfg.ForwardAgent = value
458 case "serveraliveinterval":
459 cfg.ServerAliveInterval = value

Callers 3

LoadGlobalConfigFunction · 0.85
LoadHostConfigFunction · 0.85
LoadAllHostConfigsFunction · 0.85

Calls 1

parseKVFunction · 0.85

Tested by

no test coverage detected