minamijoyo / tfupdate

Update version constraints in your Terraform configurations
MIT License
556 stars 23 forks source link

Support listing all modules/providers in the current directory #46

Open nitrocode opened 3 years ago

nitrocode commented 3 years ago

It would be nice to list all the modules/providers in the current directory

This would save us from having to ripgrep through the code to see which modules are used in order to see which modules can be updated.

Perhaps something like this

$ tfupdate release latest --current
module xyz 1.3.3
module abc 1.4.0
$ tfupdate release list --current
module xyz 1.3.1
module abc 1.2.1
minamijoyo commented 3 years ago

Sounds good. It's a missing feature.

nitrocode commented 2 years ago

Seems like this has been implemented by a separate tool

https://github.com/keilerkonzept/terraform-module-versions

awoimbee commented 11 months ago

This would save us from having to ripgrep through the code

For anyone else around here that wants maximum automation, I made a little script:

set -euo pipefail

arg=${1:-}

if [ "$arg" = "modules" ]; then
    # grep the module source, extract the unquoted name, sort and uniq
    modules="$(rg --no-heading "^  source *= \"[^.]" | awk -F '"| *' '{print $5}' | sort | uniq)"

    for m in $modules; do
        # some modules are submodules of other modules
        root_module="$(echo "$m" | awk -F'//' '{print $1}')"
        latest_version=$(curl "https://registry.terraform.io/v2/modules/$root_module?include=latest-version" | jq -r '.included[0].attributes.tag')
        latest_version=${latest_version#v}
        tfupdate module "$m" . -r -v "~> $latest_version"
    done
elif [ "$arg" = "providers" ]; then
    providers="$(rg --no-heading "^      source *= \"[^.]" | awk -F '"| *' '{print $5}' | sort | uniq)"

    for p in $providers; do
        latest_version=$(curl "https://registry.terraform.io/v2/providers/$p?include=latest-version" | jq -r '.included[0].attributes.tag')
        latest_version=${latest_version#v}
        tfupdate provider "$p" . -r -v "~> $latest_version"
    done
else
    echo "$0: automation script around tfupdate (https://github.com/minamijoyo/tfupdate)"
    echo ""
    echo "Usage: $0 [modules|providers]"
    echo "  modules: update all modules to latest version"
    echo "  providers: update all providers to latest version"
    exit 1
fi

Note that my ripgrep search strings (to differentiate modules from providers) are based on indentation (I use 2 spaces indentation).