summaryrefslogtreecommitdiffstats
path: root/acf2/persistence/backends/service.lua
blob: 216318478a298263cfd5ac99a0fd481c32d8afa5 (plain)
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
31
32
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]

local rc = require('rc')
local stringy = require('stringy')


local backend = require('acf2.object').class()

function backend:get(path, top)
   if #path == 1 then return {'enabled', 'status'} end
   assert(#path == 2)
   local status = rc.service_status(path[1])
   if path[2] == 'status' then return status end
   if path[2] == 'enabled' then return stringy.startswith(status, 'start') end
end

function backend:set(mods)
   for _, mod in ipairs(mods) do
      local path, value = unpack(mod)
      assert(#path == 2 and path[2] == 'enabled')

      local name = path[1]
      if value then rc.service_add(name)
      else rc.service_delete(name) end
      os.execute('rc-service '..name..' '..(value and 'start' or 'stop'))
   end
end

return backend