Getting current path of the executing Lua script?

5,451 views
Skip to first unread message

Aapo Talvensaari

unread,
Jan 18, 2016, 12:08:16 PM1/18/16
to openresty-en
Is there a way to get current path of the executing Lua script in OpenResty?

E.g.

content_by_lua_file 'test.lua';

And in test.lua:

local currentPath = -- something here?

Because otherwise everything seem to be relative to nginx binary path.

Lord Nynex

unread,
Jan 18, 2016, 1:12:45 PM1/18/16
to openre...@googlegroups.com
Hello,

I use debug.getinfo(1). The returned table will have a 'source' key. I see the following idiom used quite a bit 

local current_path = string.sub(debug.getinfo(1).source, 2, string.len("/scriptname.lua") * -1).

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aapo Talvensaari

unread,
Jan 18, 2016, 4:56:41 PM1/18/16
to openresty-en
On Monday, 18 January 2016 20:12:45 UTC+2, Lord Nynex wrote:
Hello,

I use debug.getinfo(1). The returned table will have a 'source' key. I see the following idiom used quite a bit 

local current_path = string.sub(debug.getinfo(1).source, 2, string.len("/scriptname.lua") * -1)

Is there any other ways than using debug-library? I'm not sure I want to add dependency to debug library in any of my libs. But thanks anyway, this is one possibility.

Lord Nynex

unread,
Jan 18, 2016, 5:33:15 PM1/18/16
to openre...@googlegroups.com
Unfortunately I have not seen another way. If you find a better way please let me know. I do not like this approach either. 

--
Reply all
Reply to author
Forward
0 new messages