Search code examples
batch-fileantcommand-line-interfaceconfluence-rest-api

Ant task to automate running a cmd


I need to run a command like this:

atlassian confluence --action addLabels --labels labelName --space nameOfSpace --title "title name"

for a long list of titles. The command only accepts a single title argument and parameter.

Can I automate this using an Ant task that picks up a list of titles as input and puts each one in a command like the above and runs it?

I suppose I could just use replaceregexp to wrap each title in my list inside the above command, and put the whole thing into a bat file to run commands over and over, but isn't there a better way?

I've tried comma-separated lists of titles and other attempts to run a list in one command but it only seems to accept one at a time, and the documentation for the CLI says nothing about doing otherwise.

UPDATE:

I'd still be curious to know how to accomplish the above, but I finally found a way to do what I needed in one command. That is, to add or remove labels to pages in Confluence for all "child" pages of a given title by just entering the top page title:

confluence --action runFromPageList --space "SpaceName" --title "Parent Page Name" --descendents --common "--action addLabels --labels "New-Label-To-Add" --title ""@title@"" --space "SpaceName"" 

This is using the Confluence command line interface. The missing trick was to use -descendents (not --children, which is the way it's done in other confluence commands).

I had been extracting all child pages into a list-- that part I knew how to do -- and figured I'd go from there, but this way it automates the process using one command.

UPDATE II

As I mentioned above I found a way to add labels using one command in certain cases, but later I needed a way to add labels to a selected list of page titles (pages with no common parent). So I came back to this and used one of the answers here to devise a way to add labels by using for to loop over a list of titles in a .txt file. Here's what I ended up putting in my .bat file:

cmd /k for /F "usebackq delims=" %%A in (my.list.txt) DO (confluence --action addLabels --labels labelName --space spaceName --title "%%A")

One hitch was that the titles have spaces in the names, so I had to add "usebackq delims=" which I found in answer #2 here.


Solution

  • Using batch command it's easy with the for command. A simple example :

    for %%A in ("Title 1" "Title 2" "Title2") DO (
        atlassian confluence --action addLabels --labels labelName --space nameOfSpace --title %%A
    )
    

    The problem here is the readability if the list of titles is really large. A better solution is to have the process.cmd script and a file list.txt with all the titles. The final script will be:

    @echo off
    for /F %%A in (list.txt) DO (
       atlassian confluence --action addLabels --labels labelName --space nameOfSpace --title %%A
    
    )