Skip to content

Commit

Permalink
ssh: parse config file as well as host file
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Jul 21, 2024
1 parent 26c7df4 commit 5e1a42b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Emojis struct {
type SSH struct {
GeneralModule `mapstructure:",squash"`
HostFile string `mapstructure:"host_file"`
ConfigFile string `mapstructure:"config_file"`
}

type Websearch struct {
Expand Down
70 changes: 56 additions & 14 deletions modules/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ func (s SSH) Placeholder() string {
return s.general.Placeholder
}

func (s SSH) Refresh() {}
func (s *SSH) Refresh() {
s.general.IsSetup = false
}

func (s SSH) Entries(ctx context.Context, term string) []Entry {
fields := strings.Fields(term)

cmd := "ssh"

for k, v := range s.entries {
if len(fields) > 1 {
cmd = fmt.Sprintf("ssh %s@%s", fields[1], v.Label)
}
if v.Sub == "SSH Host" {
if len(fields) > 1 {
cmd = fmt.Sprintf("ssh %s@%s", fields[1], v.Label)
}

s.entries[k].Exec = cmd
s.entries[k].Exec = cmd
}
}

return s.entries
Expand Down Expand Up @@ -76,20 +80,60 @@ func (s *SSH) SetupData(cfg *config.Config) {
return
}

sshCfg := filepath.Join(home, ".ssh", "config")
if cfg.Builtins.SSH.ConfigFile != "" {
sshCfg = cfg.Builtins.SSH.ConfigFile
}

hosts := filepath.Join(home, ".ssh", "known_hosts")
if cfg.Builtins.SSH.HostFile != "" {
hosts = cfg.Builtins.SSH.HostFile
}

if _, err := os.Stat(hosts); err != nil {
log.Println("SSH host file not found, disabling ssh module")
return
s.entries = append(s.entries, getHostFileEntries(hosts)...)
s.entries = append(s.entries, getConfigFileEntries(sshCfg)...)

s.general.IsSetup = true
}

func getConfigFileEntries(sshCfg string) []Entry {
entries := []Entry{}

file, err := os.Open(sshCfg)
if err != nil {
return []Entry{}
}

defer file.Close()
scanner := bufio.NewScanner(file)

for scanner.Scan() {
text := scanner.Text()

if strings.HasPrefix(text, "Host ") {
fields := strings.Fields(text)

entries = append(entries, Entry{
Label: fields[1],
Sub: "SSH Config",
Exec: fmt.Sprintf("ssh %s", fields[1]),
Searchable: fields[1],
Terminal: true,
Categories: []string{"ssh"},
Class: "ssh",
Matching: Fuzzy,
RecalculateScore: true,
})
}
}

return entries
}

func getHostFileEntries(hosts string) []Entry {
file, err := os.Open(hosts)
if err != nil {
log.Panicln(err)
return
return []Entry{}
}

defer file.Close()
Expand All @@ -107,7 +151,7 @@ func (s *SSH) SetupData(cfg *config.Config) {
for k := range hs {
entries = append(entries, Entry{
Label: k,
Sub: "SSH",
Sub: "SSH Host",
Exec: "ssh",
MatchFields: 1,
Searchable: k,
Expand All @@ -119,7 +163,5 @@ func (s *SSH) SetupData(cfg *config.Config) {
})
}

s.entries = entries

s.general.IsSetup = true
return entries
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.6-git
0.1.6

0 comments on commit 5e1a42b

Please sign in to comment.