Skip to content

Commit

Permalink
find by name or alias, support setting multiple aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
lixvbnet committed Apr 11, 2023
1 parent b564fb8 commit 6b8cf8d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ nodes:
host: 192.168.1.10

- name: nodeB
alias: nodeB
alias: nodeB,secondNode,11
host: 192.168.1.11
user: root
password: Password
Expand All @@ -78,7 +78,7 @@ nodes:
sshw [options] [target] [command]
```

where `target` is of the form `[user[:pass]@]host`. Run `sshw -h` for a full list of available options.
where `target` is of the form `[user[:pass]@]host`. A `host` can be an FQDN, short hostname, configured node name or alias. Run `sshw -h` for a full list of available options.

---
This is a complete rewrite, except the UI interface, of [yinheli/sshw](https://github.com/yinheli/sshw) for extensibility. And the following features were added:
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
)

const Version = "1.3.1"
const Version = "1.3.2"
var Name = filepath.Base(os.Args[0])
var GitHash string

Expand Down
2 changes: 1 addition & 1 deletion sshlib/config_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ nodes:
host: 192.168.1.10

- name: nodeB
alias: nodeB
alias: nodeB,secondNode,11
host: 192.168.1.11
user: root
password: Password
17 changes: 11 additions & 6 deletions sshlib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func parseTarget(config *Config, target string) *Node {

var node *Node

// try as alias
// try as name or alias
if !strings.Contains(target, "@") {
node = findAlias(config.Nodes, target)
node = findByNameOrAlias(config.Nodes, target)
}
if node == nil {
// login by args
Expand All @@ -55,7 +55,7 @@ func parseTarget(config *Config, target string) *Node {
arr := strings.Split(target, "@")
node.Host = arr[1]
// try as alias
aliasNode := findAlias(config.Nodes, node.Host)
aliasNode := findByNameOrAlias(config.Nodes, node.Host)
if aliasNode != nil {
node = aliasNode
}
Expand All @@ -70,12 +70,17 @@ func parseTarget(config *Config, target string) *Node {
return node
}

func findAlias(nodes []*Node, nodeAlias string) *Node {
nodeAlias = strings.TrimSpace(nodeAlias)
func findByNameOrAlias(nodes []*Node, target string) *Node {
target = strings.TrimSpace(target)
for _, node := range nodes {
if node.Alias == nodeAlias {
if node.Name == target {
return node
}
for _, alias := range strings.Split(node.Alias, ",") {
if alias == target {
return node
}
}
}
return nil
}

0 comments on commit 6b8cf8d

Please sign in to comment.