using awk sed to parse update puppet file

可紊 提交于 2019-12-06 12:33:43

You can try something like:

awk -f m.awk puppetfile.pp  puppetfile.pp

where m.awk is:

NR==FNR {
    if (/^gitmod/) {
        gitrepo=getRepo()
        getline
        getline
        gitorg=getOrg()
        getline
        branch=getBranch()
        com[++i]=getNewCommit()
    }
    else if (/^\$[[:alnum:]]*=/) {
        vn=getVarName()
        val=getVarValue()
        var[vn]=val
    }
    next
}

/^gitmod/ {
    print
    getline
    sub(/".*"/,"\""com[++j]"\"")
}
{ print }

function getVarValue(a) {
    match($0,/=([[:alnum:]]+)[[:blank:]]*/,a)
    return a[1]
}

function getVarName(a) {
    match($0,/\$([[:alnum:]]+)=/,a)
    return "${"a[1]"}"
}

function getNewCommit(cmd,var) {
    cmd="ls-remote https://github.com/"gitorg"/"gitrepo".git refs/heads/"branch
    cmd |& getline var
    return var
}

function getBranch(a,br) {
    match($0,/"(.*)"/,a)
    br=a[1]
    if (br in var) br=var[br]
    return br
}

function getOrg(a,org) {
    match($0,/"(.*)"/,a)
    org=a[1]
    if (org in var) org=var[org]
    return org
}
function getRepo(a,rep) {
    match($0,/\{"(.*)":/,a)
    rep=a[1]
    if (rep in var) rep=var[rep]
    return rep
}

Here is a different take on your problem. If you store all this data in a YAML file :

gits:
  othergitcode:
    gitcommit: "b54123be540adrwer3b65872384e0101c5f94c926b81"
    gitorg:    "myreop",
    branch:    "mybranch",
  mygitcode:
    gitcommit: "b54123be540adrfer3b65872384e0101c5f94c926b81",
    gitorg:    'awesomerepo',
    branch:    "master"

Then you can have it on puppet with something like this:

create_resources('gitmod::pullstuff', hiera('gits'))

And this make it easy to manipulate in any scripting language (except awk, I suppose this answer falls short here ...).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!