Handle invalid args and passthrough
This commit is contained in:
parent
06bfa671d9
commit
3f829ccdde
@ -3,9 +3,15 @@
|
|||||||
package ssh
|
package ssh
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"kitty/tools/cli"
|
"kitty/tools/cli"
|
||||||
|
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = fmt.Print
|
var _ = fmt.Print
|
||||||
@ -20,6 +26,27 @@ func main(cmd *cli.Command, o *Options, args []string) (rc int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ssh_args, server_args, passthrough, found_extra_args, err := ParseSSHArgs(args, "--kitten")
|
||||||
|
if err != nil {
|
||||||
|
var invargs *ErrInvalidSSHArgs
|
||||||
|
switch {
|
||||||
|
case errors.As(err, &invargs):
|
||||||
|
if invargs.Msg != "" {
|
||||||
|
fmt.Fprintln(os.Stderr, invargs.Msg)
|
||||||
|
}
|
||||||
|
return 1, unix.Exec(ssh_exe(), []string{"ssh"}, os.Environ())
|
||||||
|
}
|
||||||
|
return 1, err
|
||||||
|
}
|
||||||
|
if passthrough {
|
||||||
|
if len(found_extra_args) > 0 {
|
||||||
|
return 1, fmt.Errorf("The SSH kitten cannot work with the options: %s", strings.Join(maps.Keys(PassthroughArgs()), " "))
|
||||||
|
}
|
||||||
|
return 1, unix.Exec(ssh_exe(), append([]string{"ssh"}, args...), os.Environ())
|
||||||
|
}
|
||||||
|
if false {
|
||||||
|
return len(ssh_args) + len(server_args), nil
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,18 @@ var _ = fmt.Print
|
|||||||
var ssh_options map[string]string
|
var ssh_options map[string]string
|
||||||
var query_ssh_for_options_once sync.Once
|
var query_ssh_for_options_once sync.Once
|
||||||
|
|
||||||
|
func ssh_exe() string {
|
||||||
|
ans := utils.Which("ssh")
|
||||||
|
if ans != "" {
|
||||||
|
return ans
|
||||||
|
}
|
||||||
|
ans = utils.Which("ssh", "/usr/local/bin", "/opt/bin", "/opt/homebrew/bin", "/usr/bin", "/bin")
|
||||||
|
if ans == "" {
|
||||||
|
ans = "ssh"
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
|
|
||||||
func get_ssh_options() {
|
func get_ssh_options() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if ssh_options == nil {
|
if ssh_options == nil {
|
||||||
@ -30,7 +42,7 @@ func get_ssh_options() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
cmd := exec.Command("ssh")
|
cmd := exec.Command(ssh_exe())
|
||||||
stderr, err := cmd.StderrPipe()
|
stderr, err := cmd.StderrPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -111,6 +123,10 @@ func (self *ErrInvalidSSHArgs) Error() string {
|
|||||||
return self.Msg
|
return self.Msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PassthroughArgs() map[string]bool {
|
||||||
|
return map[string]bool{"-N": true, "-n": true, "-f": true, "-G": true, "-T": true}
|
||||||
|
}
|
||||||
|
|
||||||
func ParseSSHArgs(args []string, extra_args ...string) (ssh_args []string, server_args []string, passthrough bool, found_extra_args []string, err error) {
|
func ParseSSHArgs(args []string, extra_args ...string) (ssh_args []string, server_args []string, passthrough bool, found_extra_args []string, err error) {
|
||||||
if extra_args == nil {
|
if extra_args == nil {
|
||||||
extra_args = []string{}
|
extra_args = []string{}
|
||||||
@ -119,7 +135,7 @@ func ParseSSHArgs(args []string, extra_args ...string) (ssh_args []string, serve
|
|||||||
passthrough = true
|
passthrough = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
passthrough_args := map[string]bool{"-N": true, "-n": true, "-f": true, "-G": true, "-T": true}
|
passthrough_args := PassthroughArgs()
|
||||||
boolean_ssh_args, other_ssh_args := GetSSHCLI()
|
boolean_ssh_args, other_ssh_args := GetSSHCLI()
|
||||||
ssh_args, server_args, found_extra_args = make([]string, 0, 16), make([]string, 0, 16), make([]string, 0, 16)
|
ssh_args, server_args, found_extra_args = make([]string, 0, 16), make([]string, 0, 16), make([]string, 0, 16)
|
||||||
expecting_option_val := false
|
expecting_option_val := false
|
||||||
@ -184,8 +200,8 @@ func ParseSSHArgs(args []string, extra_args ...string) (ssh_args []string, serve
|
|||||||
}
|
}
|
||||||
server_args = append(server_args, argument)
|
server_args = append(server_args, argument)
|
||||||
}
|
}
|
||||||
if len(server_args) == 0 {
|
if len(server_args) == 0 && !passthrough {
|
||||||
err = &ErrInvalidSSHArgs{Msg: "No server to connect to specified"}
|
err = &ErrInvalidSSHArgs{Msg: ""}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user