Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support --list-hosts tools command #134

Merged
merged 3 commits into from
Aug 14, 2024

Conversation

qcu266
Copy link
Contributor

@qcu266 qcu266 commented Aug 12, 2024

support list all tssh hosts.

in my case, I want to use wezterm to integrate tssh for ssh session management.

-- tssh.lua
local wezterm = require 'wezterm'

local M = {}

local function host_list()
    local hosts = {}
    local success, stdout, stderr = wezterm.run_child_process {
        'tssh',
        '--list-hosts',
    }
    hosts = wezterm.json_parse(stdout)

    return hosts
end

local function make_tssh_label_func(host)
    return wezterm.format {
        { Foreground = { AnsiColor = 'Green' } },
        { Text = "groups:" .. host["GroupLabels"] },
    }
end

local function make_tssh_fixup_func(alias)
    return function(cmd)
        local wrapped = {
            "tssh",
            alias,
        }
        cmd.args = wrapped
        -- wezterm.log_error(cmd)

        return cmd
    end
end

function M.compute_exec_domains()
    local exec_domains = {}
    for _, host in pairs(host_list()) do
        local alias = host["Alias"]
        local fixup = make_tssh_fixup_func(alias)
        local label_func = make_tssh_label_func(host)
        table.insert(
            exec_domains,
            wezterm.exec_domain(
                'tssh:' .. alias,
                fixup,
                label_func
            )
        )
    end
    return exec_domains
end

return M
-- wezterm.lua
local wezterm = require 'wezterm'
local tssh = require('tssh')

local config = {}
if wezterm.config_builder then
  config = wezterm.config_builder()
end

config.exec_domains = tssh.compute_exec_domains()

@lonnywong
Copy link
Member

@qcu266 Thanks for the PR.

Can you also write a simple user guide for it? Consider putting it on the wiki. It can be Chinese or English.

It is best to describe the advantages of the integration, or record a screen to show it.

@qcu266 qcu266 requested a review from lonnywong August 14, 2024 03:55
@qcu266 qcu266 force-pushed the feat/list-hosts branch 2 times, most recently from 99d5c79 to 08794ad Compare August 14, 2024 04:02
@qcu266 qcu266 requested a review from lonnywong August 14, 2024 09:56
@lonnywong lonnywong merged commit b91a3c5 into trzsz:main Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants