diff options
-rw-r--r-- | acf/persistence/backends/augeas.lua | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/acf/persistence/backends/augeas.lua b/acf/persistence/backends/augeas.lua index 165cf8b..9d6b4cc 100644 --- a/acf/persistence/backends/augeas.lua +++ b/acf/persistence/backends/augeas.lua @@ -5,6 +5,7 @@ See LICENSE file for license details module(..., package.seeall) +local topology = require('acf.model.root').topology local pth = require('acf.path') local util = require('acf.util') @@ -97,12 +98,30 @@ function backend:set(mods) self.aug:rm(aug_path(path)..(delete and '' or '/*')) local apath, mvpath, index = self:find(path, type(value) ~= 'table') + local mpath = mvpath or apath + + if not delete then + if #self.aug:match(mpath) == 0 then + + local function order(path) + return topology('/augeas'..path).order + end + local ord = order(pth.join('/', unpack(path))) + + for _, sibling in ipairs(self.aug:match(pth.parent(mpath)..'/*')) do + if order(string.sub(strip_name(sibling), 7, -1)) > ord then + self.aug:insert(sibling, pth.name(mpath), true) + break + end + end + end - if not delete and mvpath then - local size = #self.aug:match(mvpath) - while size < index do - self.aug:insert(ipath(mvpath, size), pth.name(mvpath)) - size = size + 1 + if mvpath then + local size = #self.aug:match(mvpath) + while size < index do + self.aug:insert(ipath(mvpath, size), pth.name(mvpath)) + size = size + 1 + end end end @@ -110,7 +129,7 @@ function backend:set(mods) if not delete or mvpath then self.aug:set(apath, value) elseif apath > '/' then apath = pth.parent(apath) end - if delete or value == '' then gcpaths[mvpath or apath] = true end + if delete or value == '' then gcpaths[mpath] = true end end local function gc(path) |