Detect latest op download package from script?

jgoguen
jgoguen
Community Member

I have a setup script I run on a fresh install to set things up and configure as much as I can. One thing I'm missing I'd like to have is the op CLI downloaded automatically. The packages are nicely named, but I don't see any way to get the current version or a URL to give me the latest version (e.g. instead of needing to find the current version is 0.10.0 to construct the URL https://cache.agilebits.com/dist/1P/op/pkg/v0.10.0/op_linux_amd64_v0.10.0.zip, I could fetch https://cache.agilebits.com/dist/1P/op/pkg/latest/op_linux_amd64_latest.zip and get whatever the latest version is at the time). The assumption is that the op binary is not available at the time so "op update" isn't an option.


1Password Version: Not Provided
Extension Version: Not Provided
OS Version: Not Provided
Sync Type: Not Provided

Comments

  • Hey @jgoguen, sorry for the delay in getting back to you here. I don't think there's any set way to do this at the moment. However, you could always use something like xmllint to parse the release notes and achieve something similar. This isn't guaranteed to work in the future, but it does work using the current release note format.

    OP_RELEASE_NOTES_URL="https://app-updates.agilebits.com/product_history/CLI"
    OP_PLATFORM="linux"
    OP_ARCH="amd64"
    
    op_release_notes=$(curl -sfL "${OP_RELEASE_NOTES_URL}")
    
    if [ $? -eq 0 ]; then
        op_url=$(echo "${op_release_notes}" | xmllint --html --xpath "string(//article[not(@class='beta')][1]/div[@class='cli-archs']/p[@class='system ${OP_PLATFORM}']/a[text()='${OP_ARCH}']/@href)" - 2>/dev/null)
        if [ -z "${op_url}" ]; then
            # If $op_url is empty (e.g. if the page layout has changed)
            echo "1Password CLI tool release notes were not in an expected format at: ${OP_RELEASE_NOTES_URL}"
        else
            # $op_url should be the URL for the latest download 
            echo "${op_url}"
        fi
    else
        # If the curl command failed (e.g. the page was not found)
        echo "1Password CLI tool release notes were not found at: ${OP_RELEASE_NOTES_URL}"
    fi
    

    Remove [not(@class='beta')] from --xpath if you'd like to grab the absolute latest version, regardless of whether it is marked as a beta version or not.

    For me xmllint didn't recognise HTML5 tags and would write to stderr to tell me this. I included 2>/dev/null to silence anything written to stderr, otherwise $op_url will end up with these errors too.

  • CamJN
    CamJN
    Community Member

    @Matthew_1P you can make this portable to macOS simply by setting OP_PLATFORM=$(uname | tr [A-Z] [a-z])

  • Absolutely, thanks for sharing that tip @CamJN!

    You can also automatically detect the architecture using uname -m, but you'll need to interpret the output into the standard formats we support. For example, i686, i586, and i486 should all be changed to i386, and x86_64 should be changed to amd64. Certainly not as nice as the command you gave for automatically setting OP_PLATFORM, but as far as I'm aware I don't think there's a better approach. :sweat:

This discussion has been closed.