File: which.lua

package info (click to toggle)
lua-penlight 1.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,708 kB
  • sloc: makefile: 2
file content (30 lines) | stat: -rw-r--r-- 800 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- a simple implementation of the which command. This looks for
-- the given file on the path. On windows, it will assume an extension
-- of .exe if no extension is given.
local List = require 'pl.List'
local path = require 'pl.path'
local app = require 'pl.app'

local pathl = List.split(os.getenv 'PATH',path.dirsep)

local function which (file)
    local res = pathl:map(path.join,file)
    res = res:filter(path.exists)
    if res then return res[1] end
end

local _,lua = app.lua()
local file = arg[1] or lua -- i.e. location of lua executable
local try

if not file then return print 'must provide a filename' end

if path.extension(file) == '' and path.is_windows then
    try = which(file..'.exe')
else
    try = which(file)
end

if try then print(try) else print 'cannot find on path' end