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 custom init command to initialize hssh/config.yml file and config.hssh.d folder #9

Closed valentinocossar closed 3 years ago

valentinocossar commented 3 years ago

Add custom init command to initialize hssh/config.yml file and ~/.ssh/config.hssh.d folder only if one of them or both not exist. Use the init command as a fallback inside the root command to call the initialize process if needed.

Maybe this can improve/fix the issue #7.

CasvalDOT commented 3 years ago

@valentinocossar, I've started the creation of the init command. Do you need to show messages in the event that everything is in place or can we not generate any kind of output? Also consider that if you continue with this approach, using the same function also to initialize the other commands you will need to provide an attribute to indicate or not to show the output otherwise it risks being annoying to show redundant messages

CasvalDOT commented 3 years ago

@valentinocossar I've pushed the new command in the develop branch.

valentinocossar commented 3 years ago

@CasvalDOT Maybe the right approach is to make an option for init so when it's called from other commands show output messages only when the folder or the config file are created. On the other hand, when you run the init command directly from the CLI, we can show a message if the config file and folder already exist and otherwise a creation message for both.

What do you think?

CasvalDOT commented 3 years ago

It make sense

CasvalDOT commented 3 years ago

I've added the ability to suppress some output of the init command. This implementation has caused some refactory. Instead of having a single init command launched by the root command, now each command have a PreRun attribute declared like the code above and the Init function is called inside. Take a look:

var connectCmd = &cobra.Command{
    Use:     "connect",
    Aliases: []string{"c"},
    Short:   "Search and connect to host using fzf",
    PreRun: func(cmd *cobra.Command, args []string) {
        controllers.Init(false)
    },
    Run: func(cmd *cobra.Command, args []string) {
        var host string
        if len(args) > 0 {
            host = args[0]
        }

        controllers.Connect(host)
    },
}

This approach doesn't save time, but perhaps it is cleaner and clearer to understand.

Furthermore, I've removed the os.Exit(0) after the automatic Sync, this allows you to execute the command launched without having to relaunch it

CasvalDOT commented 3 years ago

Another additional included is the ability to connect using an argument provided instead of search through FZF. An example:

# If another argument after connect is provided, the FZF will be skipped
# and hssh try to connect to the host name provided
hssh c myhost
CasvalDOT commented 3 years ago

I've reported this feature in issue #11

valentinocossar commented 3 years ago

@CasvalDOT super! I agree with you with the cleaner approach of this refactor. I'll let you know more about this feature after some tests.