luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

example_manifest_usage.lua (2269B)


      1 -- Example: How to use OrbitManager with manifest files and strings
      2 
      3 local OrbitManager = require("OrbitManager")
      4 
      5 print("=== OrbitManager Usage Examples ===\n")
      6 
      7 -- Example 1: Create from manifest string (embedded in code)
      8 print("Example 1: Creating from manifest string...")
      9 local manifestString = [[
     10 return {
     11     name = "myapp";
     12     dev = "devname";
     13     perms = {
     14         fs = true,
     15         os = false
     16     };
     17     allowedPaths = { "/tmp/*" };
     18     allowedDomains = { "api.myapp.io" }
     19 }
     20 ]]
     21 
     22 local sandbox1 = { io = { open = function() end }, os = { clock = function() end } }
     23 local orbit1 = OrbitManager.newFromManifest(sandbox1, manifestString)
     24 print("✓ Created from manifest string\n")
     25 
     26 -- Example 2: Create from manifest file
     27 print("Example 2: Creating from manifest file...")
     28 local manifestPath = "/home/b/Programming/LuajitOS/OS/apps/com.luajitos.testapp/settings/orbit_manifest.lua"
     29 
     30 -- Read the manifest file
     31 local file = io.open(manifestPath, "r")
     32 if file then
     33     local manifestContent = file:read("*all")
     34     file:close()
     35 
     36     local sandbox2 = { io = { open = function() end }, os = { clock = function() end } }
     37     local orbit2 = OrbitManager.newFromManifest(sandbox2, manifestContent)
     38 
     39     local metadata = orbit2:getManifestData()
     40     print("✓ Created from manifest file")
     41     print("  App:", metadata.name)
     42     print("  Developer:", metadata.dev)
     43     print("  Version:", metadata.version or "N/A")
     44 else
     45     print("✗ Could not open manifest file")
     46 end
     47 print()
     48 
     49 -- Example 3: Create from perms.lua file (original method)
     50 print("Example 3: Creating from perms.lua (traditional method)...")
     51 local sandbox3 = { io = { open = function() end }, os = { clock = function() end } }
     52 local orbit3 = OrbitManager.new(sandbox3, "perms.lua")
     53 print("✓ Created from perms.lua\n")
     54 
     55 -- Example 4: Using canRun to check permissions
     56 print("Example 4: Checking permissions with canRun...")
     57 local canRun, reason = orbit2:canRun("io.open", "/tmp/test.txt")
     58 print("  Can run io.open('/tmp/test.txt'):", canRun)
     59 if not canRun then
     60     print("  Reason:", reason)
     61 end
     62 
     63 canRun, reason = orbit2:canRun("io.open", "/etc/shadow")
     64 print("  Can run io.open('/etc/shadow'):", canRun)
     65 if not canRun then
     66     print("  Reason:", reason)
     67 end
     68 print()
     69 
     70 print("=== Examples Complete ===")