mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-25 17:52:02 +02:00
Confirm and run tool
This commit is contained in:
@@ -40,10 +40,39 @@ func ask_for_permission(script_path string) (response string, err error) {
|
|||||||
return response, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ask_if_exe_allowed(exe_path string) (ok bool, err error) {
|
||||||
|
opts := &ask.Options{Type: "yesno", Default: "n"}
|
||||||
|
ctx := markup.New(true)
|
||||||
|
opts.Message = ctx.Prettify(fmt.Sprintf(
|
||||||
|
"Attempting to execute the program: :yellow:`%s`\nExecuting untrusted programs can be dangerous. Proceed anyway?", exe_path))
|
||||||
|
response, err := ask.GetChoices(opts)
|
||||||
|
return response == "y", err
|
||||||
|
}
|
||||||
|
|
||||||
func permission_denied(script_path string) error {
|
func permission_denied(script_path string) error {
|
||||||
return fmt.Errorf("Execution of %s was denied by user", script_path)
|
return fmt.Errorf("Execution of %s was denied by user", script_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func confirm_and_run_exe(args []string) (rc int, err error) {
|
||||||
|
exe := args[len(args)-1]
|
||||||
|
ok, err := ask_if_exe_allowed(exe)
|
||||||
|
if err != nil {
|
||||||
|
return 1, err
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
exe = utils.FindExe(args[0])
|
||||||
|
if exe == "" {
|
||||||
|
return 1, fmt.Errorf("Failed to find the script interpreter: %s", args[0])
|
||||||
|
}
|
||||||
|
if err = unix.Exec(exe, []string{exe}, os.Environ()); err != nil {
|
||||||
|
rc = 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return 1, permission_denied(exe)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func confirm_and_run_shebang(args []string, confirm_policy ConfirmPolicy) (rc int, err error) {
|
func confirm_and_run_shebang(args []string, confirm_policy ConfirmPolicy) (rc int, err error) {
|
||||||
script_path := args[len(args)-1]
|
script_path := args[len(args)-1]
|
||||||
do_confirm := true
|
do_confirm := true
|
||||||
|
|||||||
@@ -108,6 +108,16 @@ func KittyToolEntryPoints(root *cli.Command) {
|
|||||||
return run_shebang(args)
|
return run_shebang(args)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
// __confirm_and_run_exe__
|
||||||
|
root.AddSubCommand(&cli.Command{
|
||||||
|
Name: "__confirm_and_run_exe__",
|
||||||
|
Hidden: true,
|
||||||
|
OnlyArgsAllowed: true,
|
||||||
|
Run: func(cmd *cli.Command, args []string) (rc int, err error) {
|
||||||
|
return confirm_and_run_exe(args)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
// __convert_image__
|
// __convert_image__
|
||||||
images.ConvertEntryPoint(root)
|
images.ConvertEntryPoint(root)
|
||||||
// __atexit__
|
// __atexit__
|
||||||
|
|||||||
Reference in New Issue
Block a user