elvish/pkg/eval/builtin_fn_cmd.go
Kurtis Rader 9a576529a3 Replace ErrArgs with more specific errors
This also replaces the slightly awkward "arguments here" reason with
"argument count" as the "what" for a typical errs.ArityMismatch
exception. It also reformats most of the constructors so that the "what"
is on the same line. This makes `grep errs.ArityMismatch **.go` more
useful as a result.
2021-06-13 22:42:04 +01:00

124 lines
2.1 KiB
Go

package eval
import (
"fmt"
"os"
"os/exec"
"src.elv.sh/pkg/eval/errs"
)
// Command and process control.
// TODO(xiaq): Document "fg".
func init() {
addBuiltinFns(map[string]interface{}{
// Command resolution
"external": external,
"has-external": hasExternal,
"search-external": searchExternal,
// Process control
"fg": fg,
"exec": execFn,
"exit": exit,
})
}
//elvdoc:fn external
//
// ```elvish
// external $program
// ```
//
// Construct a callable value for the external program `$program`. Example:
//
// ```elvish-transcript
// ~> x = (external man)
// ~> $x ls # opens the manpage for ls
// ```
//
// @cf has-external search-external
func external(cmd string) Callable {
return NewExternalCmd(cmd)
}
//elvdoc:fn has-external
//
// ```elvish
// has-external $command
// ```
//
// Test whether `$command` names a valid external command. Examples (your output
// might differ):
//
// ```elvish-transcript
// ~> has-external cat
// ▶ $true
// ~> has-external lalala
// ▶ $false
// ```
//
// @cf external search-external
func hasExternal(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
//elvdoc:fn search-external
//
// ```elvish
// search-external $command
// ```
//
// Output the full path of the external `$command`. Throws an exception when not
// found. Example (your output might vary):
//
// ```elvish-transcript
// ~> search-external cat
// ▶ /bin/cat
// ```
//
// @cf external has-external
func searchExternal(cmd string) (string, error) {
return exec.LookPath(cmd)
}
//elvdoc:fn exit
//
// ```elvish
// exit $status?
// ```
//
// Exit the Elvish process with `$status` (defaulting to 0).
func exit(fm *Frame, codes ...int) error {
code := 0
switch len(codes) {
case 0:
case 1:
code = codes[0]
default:
return errs.ArityMismatch{What: "arguments", ValidLow: 0, ValidHigh: 1, Actual: len(codes)}
}
preExit(fm)
os.Exit(code)
// Does not return
panic("os.Exit returned")
}
func preExit(fm *Frame) {
daemon := fm.Evaler.DaemonClient()
if daemon != nil {
err := daemon.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}