mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.1k stars 336 forks source link

interp: allow to register go functions #872

Closed zimbatm closed 2 years ago

zimbatm commented 2 years ago

As part of the long-running branch in https://github.com/direnv/direnv/tree/gosh, there are a number of bash functions that could be sped up by implementing them in Go instead.

This PR is a bit of a brute-force attempt to see if I could add the capability to this project. It seems to work but I don't know if a solution like that would be acceptable. I'm looking for an initial review to validate the overall approach before continuing on the PR.

mvdan commented 2 years ago

Have you seen https://pkg.go.dev/mvdan.cc/sh/v3/interp#ExecHandler? That already exists as a way to add commands implemented in Go. See the added example; if you want to add multiple commands, you can imagine a string switch in that function.

zimbatm commented 2 years ago

Hmm right, I missed that. I guess ExecHandler is not going to make them visible for introspection but I can live with that.

zimbatm commented 2 years ago

thanks! :)

mvdan commented 2 years ago

Yeah, it is opaque to the shell code, but the advantage is that you can have arbitrary logic - like implementing any command based on a prefix :)