beliven-it / hssh

A CLI to easily list, search and connect to SSH hosts. Sync down hosts from providers in order to get a centralized hosts configuration.
MIT License
2 stars 1 forks source link

Add ~/.ssh/config custom hosts to connect and find commands #8

Closed valentinocossar closed 3 years ago

valentinocossar commented 3 years ago

Add ~/.ssh/config custom hosts to connect and find commands. Allow using custom hosts not added by the sync command but added manually to the ~/.ssh/config file.

CasvalDOT commented 3 years ago

Ok, I understand the request. Maybe we can follow the logic applied to my version of SSH. The file host to read match the pattern config. For example:

.ssh
  - config.hssh.d
    - host01
    - host02
  - config.agency.d
    - host_a
    - host_b
  - config
  - config.2

The system matches each file contained in a folder with name contains config and the same for the files. In this case the file to read will be:

- config.hssh.d/host01
- config.hssh.d/host02
- config.agency.d/host_a
- config.agency.d/host_b
- config
- config.2

Or maybe we can read a config file and take all files included.

# config

Include config.hssh.d/*
Include config.agency.d/*
Include config.2

This alternative maybe is more declarative and less prone to errors.

valentinocossar commented 3 years ago

@CasvalDOT yes, I prefer the first option, we can find valid hosts inside the .ssh folder with these 2 criteria:

  1. Files that match config as pattern in the name
  2. Files inside folders that match config as pattern in the name

But the CLI will only include config.hssh.d folder inside .ssh/config file automatically, other includes have to be added manually to the file.

What do you think?

CasvalDOT commented 3 years ago

I really like the first solution, but in my opinion the second is more robust as it would not generate errors due to the lack of the host

CasvalDOT commented 3 years ago

@valentinocossar, I've created the first possible implementation about this improvement. Now the list command (find and connect use the same logic) use this procedure:

  1. Read the config file and search for includes folder and files, furthermore the system take host configurations inside
  2. For each file included the system parse int and save in the connections array

I've also tried to optimize the read / parsing process adding goroutines. A nice to have will be discriminated unique host configurations.

When you have a moment try it to develop branch.

NOTE:

This is a first approach. An analysis must be performed to understand which patterns are allowed in the Include syntax. Currently, the system know a folder if the string have the * or / as final character, but what's happen if the included have this pattern?

# Using wildcard in the middle of the string
Include config*meta/*

It is allowed and must be managed? If so, the reading of files and folders should be managed differently.

valentinocossar commented 3 years ago

@CasvalDOT Super! As far as I know, the most used includes patterns are:

So at this time, we can only support these integrations.

But I discovered that in the Include declaration you can use the wildcard in each step of the path. More info in the Include description below:

Include
    Include the specified configuration file(s).  Mul‐
    tiple pathnames may be specified and each pathname
    may contain glob(3) wildcards and, for user config‐
    urations, shell-like “~” references to user home
    directories.  Files without absolute paths are
    assumed to be in ~/.ssh if included in a user con‐
    figuration file or /etc/ssh if included from the
    system configuration file.  Include directive may
    appear inside a Match or Host block to perform con‐
    ditional inclusion.

The support for the wildcard in each pathname can be a further improvement to consider in the next releases.

At this point, I'll test the integration as soon as possible to give you feedback about any improvement or bug.

CasvalDOT commented 3 years ago

Ok! I miss handling the ~ character.

CasvalDOT commented 3 years ago

@valentinocossar, I've push another version. I reduced the logic and using filepath.Glob(...) I can now resolve every wildcard. I've also add the ~ handler

valentinocossar commented 3 years ago

👍🏻 I'll test it along with all other features!