From b91a3c535c69f67cbebf46a7ada132eec97116f9 Mon Sep 17 00:00:00 2001 From: QCU Date: Wed, 14 Aug 2024 18:19:35 +0800 Subject: [PATCH] feat: support --list-hosts tools command (#134) --- tssh/args.go | 1 + tssh/config.go | 2 +- tssh/tools.go | 4 ++- tssh/tools_list_hosts.go | 53 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tssh/tools_list_hosts.go diff --git a/tssh/args.go b/tssh/args.go index 2c73930..c3b2043 100644 --- a/tssh/args.go +++ b/tssh/args.go @@ -85,6 +85,7 @@ type sshArgs struct { TsshdPath string `arg:"--tsshd-path" placeholder:"path" help:"[udp] tsshd absolute path on the server"` NewHost bool `arg:"--new-host" help:"[tools] add new host to configuration"` EncSecret bool `arg:"--enc-secret" help:"[tools] encode secret for configuration"` + ListHosts bool `arg:"--list-hosts" help:"[tools] list all hosts in configuration"` InstallTrzsz bool `arg:"--install-trzsz" help:"[tools] install trzsz to the remote server"` InstallTsshd bool `arg:"--install-tsshd" help:"[tools] install tsshd to the remote server"` InstallPath string `arg:"--install-path" placeholder:"path" help:"[tools] install path, default: '~/.local/bin/'"` diff --git a/tssh/config.go b/tssh/config.go index 192bdc4..287b224 100644 --- a/tssh/config.go +++ b/tssh/config.go @@ -64,7 +64,7 @@ type sshHost struct { ProxyJump string RemoteCommand string GroupLabels string - Selected bool + Selected bool `json:"-"` } type tsshConfig struct { diff --git a/tssh/tools.go b/tssh/tools.go index 35cc225..c5c778e 100644 --- a/tssh/tools.go +++ b/tssh/tools.go @@ -33,7 +33,7 @@ import ( "time" "github.com/charmbracelet/bubbles/textinput" - "github.com/charmbracelet/bubbletea" + tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" ) @@ -459,6 +459,8 @@ func execLocalTools(argv []string, args *sshArgs) (int, bool) { return execEncodeSecret() case args.NewHost || len(argv) == 0 && isFileNotExistOrEmpty(userConfig.configPath): return execNewHost(args) + case args.ListHosts: + return execListHosts() default: return 0, false } diff --git a/tssh/tools_list_hosts.go b/tssh/tools_list_hosts.go new file mode 100644 index 0000000..f6fb1eb --- /dev/null +++ b/tssh/tools_list_hosts.go @@ -0,0 +1,53 @@ +/* +MIT License + +Copyright (c) 2023-2024 The Trzsz SSH Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package tssh + +import ( + "encoding/json" + "fmt" + "runtime" + "strings" +) + +func execListHosts() (int, bool) { + hosts := getAllHosts() + + if hosts == nil { + hosts = []*sshHost{} + } + result, err := json.MarshalIndent(hosts, "", " ") + if err != nil { + warning("json marshal indent failed: %v", err) + return 1, true + } + + hostsJson := string(result) + if runtime.GOOS == "windows" { + hostsJson = strings.ReplaceAll(hostsJson, "\n", "\r\n") + } + fmt.Println(hostsJson) + + return 0, true +}