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
}
func (self *Command) Exec(args ...string) {
func (self *Command) ExecArgs(args []string) (exit_code int) {
root := self
for root.Parent != nil {
root = root.Parent
}
if len(args) == 0 {
args = os.Args
}
cmd, err := root.ParseArgs(args)
if err != nil {
ShowError(err)
os.Exit(1)
return 1
}
help_opt := cmd.option_map["Help"]
version_opt := root.option_map["Version"]
exit_code := 0
if help_opt != nil && help_opt.parsed_value().(bool) {
cmd.ShowHelp()
os.Exit(exit_code)
return
} else if version_opt != nil && version_opt.parsed_value().(bool) {
root.ShowVersion()
os.Exit(exit_code)
return
} else if cmd.Run != nil {
exit_code, err = cmd.Run(cmd, cmd.Args)
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 {

View File

@ -36,6 +36,7 @@ var ProtocolVersion [3]int = [3]int{0, 26, 0}
type GlobalOptions struct {
to_network, to_address, password string
to_address_is_from_env_var bool
already_setup bool
}
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) {
if global_options.already_setup {
return nil
}
err = cmd.GetOptionValues(&rc_global_opts)
if err != nil {
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)
global_options.password = q
global_options.already_setup = true
return err
}

View File

@ -7,7 +7,6 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"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")
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 = append(cmdline, parsed_cmdline...)
cmd := exec.Cmd{Path: exe, Args: cmdline, Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}
err = cmd.Run()
root := cli.NewRootCommand()
EntryPoint(root)
hi.ExitCode = root.ExecArgs(cmdline)
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)
}
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) {
err := setup_global_options(cmd)
if err != nil {
return 1, err
}
formatter = markup.New(true)
fmt.Println("Welcome to the kitty shell!")
fmt.Println("Use", formatter.Green("help"), "for assistance or", formatter.Green("exit"), "to quit.")