Search code examples
linuxwindowsunixherokustrapi

Any way to convert this code to work in Powershell? Needed for Strapi to deploy to Heroku


I'm trying to deploy a Strapi backend to Heroku, on their docs, they use this line of code:

heroku config:set MY_HEROKU_URL=$(heroku info -s | grep web_url | cut -d= -f2)

Of course this (grep and cut) don't work in powershell. Does anyone know an alternative way to enter this into powershell? Or if there is any docs relevant?


Solution

  • You can use capturing feature of -replace PowerShell operand.

    I don't know the specific format of heroku command, so I will only give a simple example.

    > $web_url = "mysite.com"
    > "mysite.com some information" -replace "$web_url (.+)", '$1'
    some information
    

    In the code above, (.+) captures the rest of the input string after the link and a space. $1 references the captured string.

    Pay attention that I used double-quotes for the first right-hand-side operand to expand $web_url into the regexp. On the other hand, single-quotes in the last operand to prevent powershell interpretting $1 as a variable reference.

    You can have more information about -replace operand and rexexp capturing from the following links:

    Note: Above code will apply the process to every line of input string:

    > "mysite.com some information`nmysite.com some other information" -replace "$web_url (.+)", '$1'
    some information
    some other information