Skip to content

Commit

Permalink
feat: support --list-hosts tools command (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
qcu266 committed Aug 14, 2024
1 parent 48da6cc commit b91a3c5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions tssh/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/'"`
Expand Down
2 changes: 1 addition & 1 deletion tssh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type sshHost struct {
ProxyJump string
RemoteCommand string
GroupLabels string
Selected bool
Selected bool `json:"-"`
}

type tsshConfig struct {
Expand Down
4 changes: 3 additions & 1 deletion tssh/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"time"

"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbletea"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

Expand Down Expand Up @@ -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
}
Expand Down
53 changes: 53 additions & 0 deletions tssh/tools_list_hosts.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b91a3c5

Please sign in to comment.