Get rid of the cobra dependency

This commit is contained in:
Kovid Goyal
2022-09-25 12:23:52 +05:30
parent cbc569af64
commit 4396dede85
13 changed files with 154 additions and 493 deletions

View File

@@ -13,8 +13,6 @@ import (
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sys/unix"
"kitty"
@@ -30,13 +28,6 @@ import (
var ProtocolVersion [3]int = [3]int{0, 26, 0}
func add_bool_set(cmd *cobra.Command, name string, short string, usage string) *bool {
if short == "" {
return cmd.Flags().Bool(name, false, usage)
}
return cmd.Flags().BoolP(name, short, false, usage)
}
type GlobalOptions struct {
to_network, to_address, password string
to_address_is_from_env_var bool
@@ -142,7 +133,7 @@ type Response struct {
}
type rc_io_data struct {
cmd *cobra.Command
cmd *cli.Command
rc *utils.RemoteControlCmd
serializer serializer_func
on_key_event func(lp *loop.Loop, ke *loop.KeyEvent) error
@@ -294,82 +285,46 @@ func get_password(password string, password_file string, password_env string, us
return ans, nil
}
var all_commands map[string]func(*cobra.Command) *cobra.Command = make(map[string]func(*cobra.Command) *cobra.Command)
var command_objects map[string]*cobra.Command = make(map[string]*cobra.Command)
var all_commands map[string]func(*cli.Command) *cli.Command = make(map[string]func(*cli.Command) *cli.Command)
func add_global_options(fs *pflag.FlagSet) {
fs.String("to", "",
"An address for the kitty instance to control. Corresponds to the address given"+
" to the kitty instance via the :option:`kitty --listen-on` option or the :opt:`listen_on` setting in :file:`kitty.conf`. If not"+
" specified, the environment variable :envvar:`KITTY_LISTEN_ON` is checked. If that"+
" is also not found, messages are sent to the controlling terminal for this"+
" process, i.e. they will only work if this process is run within a kitty window.")
fs.String("password", "",
"A password to use when contacting kitty. This will cause kitty to ask the user"+
" for permission to perform the specified action, unless the password has been"+
" accepted before or is pre-configured in :file:`kitty.conf`.")
fs.String("password-file", "rc-pass",
"A file from which to read the password. Trailing whitespace is ignored. Relative"+
" paths are resolved from the kitty configuration directory. Use - to read from STDIN."+
" Used if no :option:`--password` is supplied. Defaults to checking for the"+
" :file:`rc-pass` file in the kitty configuration directory.")
fs.String("password-env", "KITTY_RC_PASSWORD",
"The name of an environment variable to read the password from."+
" Used if no :option:`--password-file` or :option:`--password` is supplied.")
cli.Choices(fs, "use-password", "If no password is available, kitty will usually just send the remote control command without a password. This option can be used to force it to always or never use the supplied password.", "if-available", "always", "never")
}
func setup_global_options(cmd *cobra.Command) (err error) {
var v = cli.FlagValGetter{Flags: cmd.Flags()}
to := v.String("to")
password := v.String("password")
password_file := v.String("password-file")
password_env := v.String("password-env")
use_password := v.String("use-password")
if v.Err != nil {
return v.Err
func setup_global_options(cmd *cli.Command) (err error) {
err = cmd.GetOptionValues(&rc_global_opts)
if err != nil {
return err
}
if to == "" {
to = os.Getenv("KITTY_LISTEN_ON")
if rc_global_opts.To == "" {
rc_global_opts.To = os.Getenv("KITTY_LISTEN_ON")
global_options.to_address_is_from_env_var = true
}
if to != "" {
network, address, err := utils.ParseSocketAddress(to)
if rc_global_opts.To != "" {
network, address, err := utils.ParseSocketAddress(rc_global_opts.To)
if err != nil {
return err
}
global_options.to_network = network
global_options.to_address = address
}
q, err := get_password(password, password_file, password_env, use_password)
q, err := get_password(rc_global_opts.Password, rc_global_opts.PasswordFile, rc_global_opts.PasswordEnv, rc_global_opts.UsePassword)
global_options.password = q
return err
}
func EntryPoint(tool_root *cobra.Command) *cobra.Command {
at_root_command := cli.CreateCommand(&cobra.Command{
Use: "@ [global options] command [command options] [command args]",
Short: "Control kitty remotely",
Long: "Control kitty by sending it commands. Set the allow_remote_control option in :file:`kitty.conf` or use a password, for this to work.",
})
at_root_command.Annotations["options_title"] = "Global options"
add_global_options(at_root_command.PersistentFlags())
func EntryPoint(tool_root *cli.Command) *cli.Command {
at_root_command := tool_root.AddSubCommand("", "@")
at_root_command.Usage = "[global options] [sub-command] [sub-command options] [sub-command args]"
at_root_command.ShortDescription = "Control kitty remotely"
at_root_command.HelpText = "Control kitty by sending it commands. Set the allow_remote_control option in :file:`kitty.conf` for this to work. When run without any sub-commands this will start an interactive shell to control kitty."
add_rc_global_opts(at_root_command)
for cmd_name, reg_func := range all_commands {
global_options_group := at_root_command.OptionGroups[0]
for _, reg_func := range all_commands {
c := reg_func(at_root_command)
at_root_command.AddCommand(c)
command_objects[cmd_name] = c
alias := *c
alias.Use = "@" + alias.Use
alias.Hidden = true
add_global_options(alias.Flags())
tool_root.AddCommand(&alias)
clone := tool_root.AddClone("", c)
clone.Name = "@" + c.Name
clone.Hidden = true
clone.OptionGroups = append(clone.OptionGroups, global_options_group.Clone(clone))
}
return at_root_command
}

View File

@@ -32,8 +32,8 @@ func parse_send_text(io_data *rc_io_data, args []string) error {
generators = append(generators, text_gen)
}
if options_send_text.from_file != "" {
f, err := os.Open(options_send_text.from_file)
if options_send_text.FromFile != "" {
f, err := os.Open(options_send_text.FromFile)
if err != nil {
return err
}
@@ -49,7 +49,7 @@ func parse_send_text(io_data *rc_io_data, args []string) error {
generators = append(generators, file_gen)
}
if options_send_text.stdin {
if options_send_text.Stdin {
pending_key_events := make([]string, 0, 1)
io_data.on_key_event = func(lp *loop.Loop, ke *loop.KeyEvent) error {

View File

@@ -2,7 +2,7 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
// this file is autogenerated by __FILE__ do not edit
// Code generated by gen-go-code.py; DO NOT EDIT.
package at
@@ -11,9 +11,6 @@ import (
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"kitty/tools/cli"
"kitty/tools/utils"
)
@@ -31,7 +28,7 @@ type CMD_NAME_json_type struct {
JSON_DECLARATION_CODE
}
func create_payload_CMD_NAME(io_data *rc_io_data, flags *pflag.FlagSet, args []string) (err error) {
func create_payload_CMD_NAME(io_data *rc_io_data, cmd *cli.Command, args []string) (err error) {
payload := CMD_NAME_json_type{}
JSON_INIT_CODE
io_data.rc.Payload = payload
@@ -62,19 +59,22 @@ func create_rc_CMD_NAME(args []string) (*utils.RemoteControlCmd, error) {
return &rc, nil
}
func run_CMD_NAME(cmd *cobra.Command, args []string) (err error) {
SET_OPTION_VALUES_CODE
func run_CMD_NAME(cmd *cli.Command, args []string) (return_code int, err error) {
err = cmd.GetOptionValues(&options_CMD_NAME)
if err != nil {
return
}
rc, err := create_rc_CMD_NAME(args)
if err != nil {
return err
return
}
nrv, err := cmd.Flags().GetBool("no-response")
nrv, err := cli.GetOptionValue[bool](cmd, "NoResponse")
if err == nil {
rc.NoResponse = nrv
}
var timeout float64 = WAIT_TIMEOUT
rt, err := cmd.Flags().GetFloat64("response-timeout")
rt, err := cli.GetOptionValue[float64](cmd, "ResponseTimeout")
if err == nil {
timeout = rt
}
@@ -84,31 +84,22 @@ func run_CMD_NAME(cmd *cobra.Command, args []string) (err error) {
timeout: time.Duration(timeout * float64(time.Second)),
string_response_is_err: STRING_RESPONSE_IS_ERROR,
}
err = create_payload_CMD_NAME(&io_data, cmd.Flags(), args)
err = create_payload_CMD_NAME(&io_data, cmd, args)
if err != nil {
return err
return
}
err = send_rc_command(&io_data)
return
}
func aliasNormalizeFunc_CMD_NAME(f *pflag.FlagSet, name string) pflag.NormalizedName {
name = cli.FlagNormalizer(name)
ALIAS_NORMALIZE_CODE
return pflag.NormalizedName(name)
}
func setup_CMD_NAME(root *cobra.Command) *cobra.Command {
ans := cli.CreateCommand(&cobra.Command{
Use: "CLI_NAME [options]" + "ARGSPEC",
Short: "SHORT_DESC",
Long: "LONG_DESC",
RunE: run_CMD_NAME,
})
func setup_CMD_NAME(parent *cli.Command) *cli.Command {
ans := parent.AddSubCommand("", "CMD_NAME")
ans.Usage = "ARGSPEC"
ans.ShortDescription = "SHORT_DESC"
ans.HelpText = "LONG_DESC"
ans.Run = run_CMD_NAME
ADD_FLAGS_CODE
ans.Flags().SetNormalizeFunc(aliasNormalizeFunc_CMD_NAME)
return ans
}

View File

@@ -4,7 +4,7 @@ package main
import (
"kitty/tools/cli"
_ "kitty/tools/cmd/at"
"kitty/tools/cmd/at"
"kitty/tools/completion"
)
@@ -12,8 +12,9 @@ func main() {
root := cli.NewRootCommand()
root.ShortDescription = "Fast, statically compiled implementations for various kitty command-line tools"
root.Usage = "command [command options] [command args]"
// root.AddCommand(at.EntryPoint(root))
at.EntryPoint(root)
completion.EntryPoint(root)
root.Exec()
}