Skip to content

Commit dc9b764

Browse files
committed
maintenance: add simple host completion
1 parent 408fa67 commit dc9b764

8 files changed

Lines changed: 83 additions & 28 deletions

File tree

cmd/completion/completion.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ Licensed under the MIT license, see LICENSE in the project root for details.
55
package completion
66

77
import (
8+
"bufio"
9+
"errors"
10+
"fmt"
11+
"maps"
12+
"os"
13+
"os/user"
14+
"slices"
15+
816
"github.com/spf13/cobra"
917
"github.com/tschaefer/finchctl/internal/config"
1018
)
@@ -39,3 +47,43 @@ func CompleteDashboardRole(cmd *cobra.Command, args []string, toComplete string)
3947

4048
return []string{"viewer", "operator", "admin"}, cobra.ShellCompDirectiveNoFileComp
4149
}
50+
51+
func CompleteHostName(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
52+
if len(args) != 0 {
53+
return nil, cobra.ShellCompDirectiveNoFileComp
54+
}
55+
56+
curUser, err := user.Current()
57+
if err != nil {
58+
panic(err)
59+
}
60+
61+
knownHosts := curUser.HomeDir + "/.ssh/known_hosts"
62+
if _, err = os.Stat(knownHosts); errors.Is(err, os.ErrNotExist) {
63+
return nil, cobra.ShellCompDirectiveNoFileComp
64+
}
65+
66+
readFile, err := os.Open(knownHosts)
67+
if err != nil {
68+
return nil, cobra.ShellCompDirectiveNoFileComp
69+
}
70+
defer func() {
71+
_ = readFile.Close()
72+
}()
73+
74+
fileScanner := bufio.NewScanner(readFile)
75+
fileScanner.Split(bufio.ScanLines)
76+
77+
hosts := map[string]int{}
78+
for fileScanner.Scan() {
79+
var host string
80+
if _, err := fmt.Sscan(fileScanner.Text(), &host); err != nil {
81+
continue
82+
}
83+
84+
hosts[host] = 1
85+
}
86+
list := slices.Sorted(maps.Keys(hosts))
87+
88+
return list, cobra.ShellCompDirectiveNoFileComp
89+
}

cmd/service/deploy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import (
1919
)
2020

2121
var deployCmd = &cobra.Command{
22-
Use: "deploy [user@]host[:port]",
23-
Short: "Deploy Finch service to a remote host",
24-
Args: cobra.ExactArgs(1),
25-
Run: runDeployCmd,
22+
Use: "deploy [user@]host[:port]",
23+
Short: "Deploy Finch service to a remote host",
24+
Args: cobra.ExactArgs(1),
25+
Run: runDeployCmd,
26+
ValidArgsFunction: completion.CompleteHostName,
2627
}
2728

2829
func init() {

cmd/service/deregister.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
)
1616

1717
var deregisterCmd = &cobra.Command{
18-
Use: "deregister [user@]host[:port]",
19-
Short: "Deregister the client from a service on a remote host",
20-
Args: cobra.ExactArgs(1),
21-
Run: runDeregisterCmd,
18+
Use: "deregister [user@]host[:port]",
19+
Short: "Deregister the client from a service on a remote host",
20+
Args: cobra.ExactArgs(1),
21+
Run: runDeregisterCmd,
22+
ValidArgsFunction: completion.CompleteHostName,
2223
}
2324

2425
func init() {

cmd/service/register.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
)
1616

1717
var registerCmd = &cobra.Command{
18-
Use: "register [user@]host[:port]",
19-
Short: "Register the client with a service on a remote host",
20-
Args: cobra.ExactArgs(1),
21-
Run: runRegisterCmd,
18+
Use: "register [user@]host[:port]",
19+
Short: "Register the client with a service on a remote host",
20+
Args: cobra.ExactArgs(1),
21+
Run: runRegisterCmd,
22+
ValidArgsFunction: completion.CompleteHostName,
2223
}
2324

2425
func init() {

cmd/service/rotate_certificate.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
)
1616

1717
var rotateCertificateCmd = &cobra.Command{
18-
Use: "rotate-certificate [user@]host[:port]",
19-
Short: "Rotate mTLS certificates of a service on a remote host",
20-
Args: cobra.ExactArgs(1),
21-
Run: runRotateCertificateCmd,
18+
Use: "rotate-certificate [user@]host[:port]",
19+
Short: "Rotate mTLS certificates of a service on a remote host",
20+
Args: cobra.ExactArgs(1),
21+
Run: runRotateCertificateCmd,
22+
ValidArgsFunction: completion.CompleteHostName,
2223
}
2324

2425
func init() {

cmd/service/rotate_secret.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
)
1616

1717
var rotateSecretCmd = &cobra.Command{
18-
Use: "rotate-secret [user@]host[:port]",
19-
Short: "Rotate secret of a service on a remote host",
20-
Args: cobra.ExactArgs(1),
21-
Run: runRotateSecretCmd,
18+
Use: "rotate-secret [user@]host[:port]",
19+
Short: "Rotate secret of a service on a remote host",
20+
Args: cobra.ExactArgs(1),
21+
Run: runRotateSecretCmd,
22+
ValidArgsFunction: completion.CompleteHostName,
2223
}
2324

2425
func init() {

cmd/service/teardown.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import (
1919
)
2020

2121
var teardownCmd = &cobra.Command{
22-
Use: "teardown [user@]host[:port]",
23-
Short: "Tear down Finch service from a remote host",
24-
Args: cobra.ExactArgs(1),
25-
Run: runTeardownCmd,
22+
Use: "teardown [user@]host[:port]",
23+
Short: "Tear down Finch service from a remote host",
24+
Args: cobra.ExactArgs(1),
25+
Run: runTeardownCmd,
26+
ValidArgsFunction: completion.CompleteHostName,
2627
}
2728

2829
func init() {

cmd/service/update.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
)
1616

1717
var updateCmd = &cobra.Command{
18-
Use: "update [user@]host[:port]",
19-
Short: "Update service on a remote host",
20-
Args: cobra.ExactArgs(1),
21-
Run: runUpdateCmd,
18+
Use: "update [user@]host[:port]",
19+
Short: "Update service on a remote host",
20+
Args: cobra.ExactArgs(1),
21+
Run: runUpdateCmd,
22+
ValidArgsFunction: completion.CompleteHostName,
2223
}
2324

2425
func init() {

0 commit comments

Comments
 (0)