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

Function LoadHostInfos

internal/sshconfig/sshconfig.go:25–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23}
24
25func LoadHostInfos() ([]HostInfo, error) {
26 home, err := os.UserHomeDir()
27 if err != nil {
28 return nil, err
29 }
30 p := filepath.Join(home, ".ssh", "config")
31 f, err := os.Open(p)
32 if err != nil {
33 if os.IsNotExist(err) {
34 return []HostInfo{}, nil
35 }
36 return nil, err
37 }
38 defer f.Close()
39 infos := map[string]*HostInfo{}
40 current := []string{}
41 s := bufio.NewScanner(f)
42 for s.Scan() {
43 raw := s.Text()
44 line := strings.TrimSpace(raw)
45 if line == "" || strings.HasPrefix(line, "#") {
46 continue
47 }
48 lower := strings.ToLower(line)
49 if strings.HasPrefix(lower, "host ") {
50 rest := strings.TrimSpace(line[5:])
51 parts := strings.Fields(rest)
52 current = current[:0]
53 for _, h := range parts {
54 if h == "*" || strings.ContainsAny(h, "*?") {
55 continue
56 }
57 current = append(current, h)
58 if _, ok := infos[h]; !ok {
59 infos[h] = &HostInfo{Name: h}
60 }
61 }
62 continue
63 }
64 if len(current) == 0 {
65 continue
66 }
67 k, v := parseKV(line)
68 if k == "hostname" && v != "" {
69 for _, h := range current {
70 infos[h].Hostname = v
71 }
72 }
73 }
74 if err := s.Err(); err != nil {
75 return nil, err
76 }
77 out := make([]HostInfo, 0, len(infos))
78 for _, v := range infos {
79 out = append(out, *v)
80 }
81 sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
82 return out, nil

Callers 3

reloadHostsFunction · 0.92
runShellFunction · 0.92
LoadHostsFunction · 0.85

Calls 1

parseKVFunction · 0.85

Tested by

no test coverage detected