kitty @ shell: Fix global options being ignored

Also no need to exec a separate process for every command
This commit is contained in:
Kovid Goyal 2023-02-04 12:54:49 +05:30
parent 0cabc3e109
commit 9bdb647454
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 24 additions and 27 deletions

View File

@ -520,28 +520,24 @@ func (self *Command) GetOptionValues(pointer_to_options_struct any) error {
return nil return nil
} }
func (self *Command) Exec(args ...string) { func (self *Command) ExecArgs(args []string) (exit_code int) {
root := self root := self
for root.Parent != nil { for root.Parent != nil {
root = root.Parent root = root.Parent
} }
if len(args) == 0 {
args = os.Args
}
cmd, err := root.ParseArgs(args) cmd, err := root.ParseArgs(args)
if err != nil { if err != nil {
ShowError(err) ShowError(err)
os.Exit(1) return 1
} }
help_opt := cmd.option_map["Help"] help_opt := cmd.option_map["Help"]
version_opt := root.option_map["Version"] version_opt := root.option_map["Version"]
exit_code := 0
if help_opt != nil && help_opt.parsed_value().(bool) { if help_opt != nil && help_opt.parsed_value().(bool) {
cmd.ShowHelp() cmd.ShowHelp()
os.Exit(exit_code) return
} else if version_opt != nil && version_opt.parsed_value().(bool) { } else if version_opt != nil && version_opt.parsed_value().(bool) {
root.ShowVersion() root.ShowVersion()
os.Exit(exit_code) return
} else if cmd.Run != nil { } else if cmd.Run != nil {
exit_code, err = cmd.Run(cmd, cmd.Args) exit_code, err = cmd.Run(cmd, cmd.Args)
if err != nil { if err != nil {
@ -551,7 +547,14 @@ func (self *Command) Exec(args ...string) {
} }
} }
} }
os.Exit(exit_code) return
}
func (self *Command) Exec(args ...string) {
if len(args) == 0 {
args = os.Args
}
os.Exit(self.ExecArgs(args))
} }
func (self *Command) GetCompletions(argv []string, init_completions func(*Completions)) *Completions { func (self *Command) GetCompletions(argv []string, init_completions func(*Completions)) *Completions {

View File

@ -36,6 +36,7 @@ var ProtocolVersion [3]int = [3]int{0, 26, 0}
type GlobalOptions struct { type GlobalOptions struct {
to_network, to_address, password string to_network, to_address, password string
to_address_is_from_env_var bool to_address_is_from_env_var bool
already_setup bool
} }
var global_options GlobalOptions var global_options GlobalOptions
@ -367,6 +368,9 @@ func register_at_cmd(f func(*cli.Command) *cli.Command) {
} }
func setup_global_options(cmd *cli.Command) (err error) { func setup_global_options(cmd *cli.Command) (err error) {
if global_options.already_setup {
return nil
}
err = cmd.GetOptionValues(&rc_global_opts) err = cmd.GetOptionValues(&rc_global_opts)
if err != nil { if err != nil {
return err return err
@ -385,6 +389,7 @@ func setup_global_options(cmd *cli.Command) (err error) {
} }
q, err := get_password(rc_global_opts.Password, rc_global_opts.PasswordFile, rc_global_opts.PasswordEnv, rc_global_opts.UsePassword) q, err := get_password(rc_global_opts.Password, rc_global_opts.PasswordFile, rc_global_opts.PasswordEnv, rc_global_opts.UsePassword)
global_options.password = q global_options.password = q
global_options.already_setup = true
return err return err
} }

View File

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -161,26 +160,12 @@ func exec_command(at_root_command *cli.Command, rl *readline.Readline, cmdline s
fmt.Fprintln(os.Stderr, "No command named", formatter.BrightRed(parsed_cmdline[0])+". Type help for a list of commands") fmt.Fprintln(os.Stderr, "No command named", formatter.BrightRed(parsed_cmdline[0])+". Type help for a list of commands")
return true return true
} }
exe, err := os.Executable()
if err != nil {
exe, err = exec.LookPath("kitten")
if err != nil {
fmt.Fprintln(os.Stderr, "Could not find the kitten executable")
return false
}
}
cmdline := []string{"kitten", "@"} cmdline := []string{"kitten", "@"}
cmdline = append(cmdline, parsed_cmdline...) cmdline = append(cmdline, parsed_cmdline...)
cmd := exec.Cmd{Path: exe, Args: cmdline, Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr} root := cli.NewRootCommand()
err = cmd.Run() EntryPoint(root)
hi.ExitCode = root.ExecArgs(cmdline)
hi.Duration = time.Now().Sub(hi.Timestamp) hi.Duration = time.Now().Sub(hi.Timestamp)
hi.ExitCode = 0
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
hi.ExitCode = exitError.ExitCode()
}
fmt.Fprintln(os.Stderr, err)
}
rl.AddHistoryItem(hi) rl.AddHistoryItem(hi)
} }
return true return true
@ -214,6 +199,10 @@ func completions(before_cursor, after_cursor string) (ans *cli.Completions) {
} }
func shell_main(cmd *cli.Command, args []string) (int, error) { func shell_main(cmd *cli.Command, args []string) (int, error) {
err := setup_global_options(cmd)
if err != nil {
return 1, err
}
formatter = markup.New(true) formatter = markup.New(true)
fmt.Println("Welcome to the kitty shell!") fmt.Println("Welcome to the kitty shell!")
fmt.Println("Use", formatter.Green("help"), "for assistance or", formatter.Green("exit"), "to quit.") fmt.Println("Use", formatter.Green("help"), "for assistance or", formatter.Green("exit"), "to quit.")