-- env.lua -- Get an environment variable with a default value function env_get(key, default_value) if type(key) ~= "string" then error("env_get: key must be a string") end -- Check context for environment variables if __ctx and __ctx.env and __ctx.env[key] ~= nil then return __ctx.env[key] end return default_value end -- Set an environment variable function env_set(key, value) if type(key) ~= "string" then error("env_set: key must be a string") end -- Update context immediately for future reads if __ctx then __ctx.env = __ctx.env or {} __ctx.env[key] = value end -- Persist to Go backend return __env_set(key, value) end -- Get all environment variables as a table function env_get_all() -- Return context table directly if available if __ctx and __ctx.env then local copy = {} for k, v in pairs(__ctx.env) do copy[k] = v end return copy end -- Fallback to Go call return __env_get_all() end -- Check if an environment variable exists function env_exists(key) if type(key) ~= "string" then error("env_exists: key must be a string") end -- Check context first if __ctx and __ctx.env then return __ctx.env[key] ~= nil end return false end -- Set multiple environment variables from a table function env_set_many(vars) if type(vars) ~= "table" then error("env_set_many: vars must be a table") end if __ctx then __ctx.env = __ctx.env or {} end local success = true for key, value in pairs(vars) do if type(key) == "string" then -- Update context if __ctx and __ctx.env then __ctx.env[key] = value end -- Persist to Go if not __env_set(key, value) then success = false end else error("env_set_many: all keys must be strings") end end return success end