When asking for permission to exec a shebang script also add options to view or edit the script
This commit is contained in:
parent
c101a6acb0
commit
491297ea1d
@ -5,35 +5,57 @@ package tool
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"kitty/kittens/ask"
|
"kitty/kittens/ask"
|
||||||
|
"kitty/tools/cli"
|
||||||
"kitty/tools/cli/markup"
|
"kitty/tools/cli/markup"
|
||||||
"kitty/tools/utils"
|
"kitty/tools/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = fmt.Print
|
var _ = fmt.Print
|
||||||
|
|
||||||
func ask_for_permission(script_path string) (allowed bool, err error) {
|
func ask_for_permission(script_path string) (response string, err error) {
|
||||||
opts := &ask.Options{Type: "yesno", Default: "n"}
|
opts := &ask.Options{Type: "choices", Default: "n", Choices: []string{"y;green:Yes", "n;red:No", "v;yellow:View", "e;magenta:Edit"}}
|
||||||
|
|
||||||
ctx := markup.New(true)
|
ctx := markup.New(true)
|
||||||
opts.Message = ctx.Prettify(fmt.Sprintf(
|
opts.Message = ctx.Prettify(fmt.Sprintf(
|
||||||
"Attempting to execute the script: :yellow:`%s`\nExecuting untrusted scripts can be dangerous. Proceed anyway?", script_path))
|
"Attempting to execute the script: :yellow:`%s`\nExecuting untrusted scripts can be dangerous. Proceed anyway?", script_path))
|
||||||
response, err := ask.GetChoices(opts)
|
response, err = ask.GetChoices(opts)
|
||||||
return response == "y", err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func confirm_and_run_shebang(args []string) (rc int, err error) {
|
func confirm_and_run_shebang(args []string) (rc int, err error) {
|
||||||
script_path := args[len(args)-1]
|
script_path := args[len(args)-1]
|
||||||
if unix.Access(script_path, unix.X_OK) != nil {
|
if unix.Access(script_path, unix.X_OK) != nil {
|
||||||
allowed, err := ask_for_permission(script_path)
|
response, err := ask_for_permission(script_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
if !allowed {
|
switch response {
|
||||||
|
default:
|
||||||
return 1, fmt.Errorf("Execution of %s was denied by user", script_path)
|
return 1, fmt.Errorf("Execution of %s was denied by user", script_path)
|
||||||
|
case "v":
|
||||||
|
raw, err := os.ReadFile(script_path)
|
||||||
|
if err != nil {
|
||||||
|
return 1, err
|
||||||
|
}
|
||||||
|
cli.ShowHelpInPager(utils.UnsafeBytesToString(raw))
|
||||||
|
return confirm_and_run_shebang(args)
|
||||||
|
case "e":
|
||||||
|
exe, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return 1, err
|
||||||
|
}
|
||||||
|
editor := exec.Command(exe, "edit-in-kitty", script_path)
|
||||||
|
editor.Stdin = os.Stdin
|
||||||
|
editor.Stdout = os.Stdout
|
||||||
|
editor.Stderr = os.Stderr
|
||||||
|
editor.Run()
|
||||||
|
return confirm_and_run_shebang(args)
|
||||||
|
case "y":
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exe := utils.FindExe(args[0])
|
exe := utils.FindExe(args[0])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user