1 #!/usr/bin/env nix-shell
2 #! nix-shell -i bash -p coreutils curl git jq moreutils nix nurl
3 # shellcheck shell=bash
6 # Update a terraform provider to the latest version advertised at the
7 # provider source address.
10 shopt -s inherit_errexit
14 Usage: ./update-provider [--force] [--no-build] [<owner>/]<provider>
16 Update a single provider in the providers.json inventory file.
18 For example to update 'terraform-providers.aws' run:
22 If the provider is not in the list already, use the form '<owner>/<provider>'
23 to add the provider to the list:
25 ./update-provider hetznercloud/hcloud
29 * --force: Force the update even if the version matches.
30 * --no-build: Don't build provider
39 while [[ $# -gt 0 ]]; do
58 if [[ -n ${provider} ]]; then
59 echo "ERROR: provider name was passed two times: '${provider}' and '$1'"
60 echo "Use --help for more info"
69 if [[ -z ${provider} ]]; then
70 echo "ERROR: No providers specified!"
76 # Usage: read_attr <key>
78 jq -r ".\"${provider}\".\"$1\"" providers.json
81 # Usage: update_attr <key> <value>
83 if [[ $2 == "null" ]]; then
84 jq -S ".\"${provider}\".\"$1\" = null" providers.json | sponge providers.json
86 jq -S ".\"${provider}\".\"$1\" = \"$2\"" providers.json | sponge providers.json
90 repo_root=$(git rev-parse --show-toplevel)
93 nurl --expr "(import ${repo_root} {}).terraform-providers.${provider}.$1"
97 echo "== terraform-providers.${provider}: $* =="
100 pushd "$(dirname "$0")" >/dev/null
102 if [[ ${provider} =~ ^[^/]+/[^/]+$ ]]; then
103 homepage="https://registry.terraform.io/providers/${provider}"
104 provider=$(basename "${provider}")
106 update_attr homepage "${homepage}"
107 # create empty stings so nix-prefetch works
109 update_attr vendorHash ""
113 homepage="$(read_attr homepage)"
115 registry_response=$(curl -s "${homepage//providers/v1/providers}")
117 old_rev="$(read_attr rev)"
118 rev="$(jq -r '.tag' <<<"${registry_response}")"
119 if [[ ${force} != 1 ]]; then
120 if [[ ${old_rev} == "${rev}" ]]; then
121 echo_provider "already at version ${rev}"
124 if [[ ${rev//v/} =~ [[:alpha:]] ]]; then
125 echo_provider "not updating to unstable version ${rev}"
129 echo_provider "updating from ${old_rev} to ${rev}"
130 update_attr rev "${rev}"
132 provider_source_url="$(jq -r '.source' <<<"${registry_response}")"
134 org="$(echo "${provider_source_url}" | cut -d '/' -f 4)"
135 update_attr owner "${org}"
136 repo="$(echo "${provider_source_url}" | cut -d '/' -f 5)"
137 update_attr repo "${repo}"
139 if [[ ${spdx} == 1 ]]; then
140 old_spdx="$(read_attr spdx)"
141 if [[ ${old_spdx} != null ]]; then
142 spdx="$(curl -L -s ${GITHUB_TOKEN:+-u ":${GITHUB_TOKEN}"} "https://api.github.com/repos/${org}/${repo}/license" | jq -r '.license.spdx_id')"
143 update_attr spdx "${spdx}"
147 echo_provider "calculating hash"
148 hash=$(generate_hash src)
149 update_attr hash "${hash}"
151 old_vendor_hash="$(read_attr vendorHash)"
152 if [[ ${old_vendor_hash} != null ]]; then
153 echo_provider "calculating vendorHash"
154 vendorHash=$(generate_hash goModules)
155 update_attr vendorHash "${vendorHash}"
158 # Check that the provider builds
159 if [[ ${build} == 1 ]]; then
160 echo_provider "building"
161 nix-build --no-out-link "${repo_root}" -A "terraform-providers.${provider}"