Skip to content

Commit

Permalink
prepare the script and the makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Oct 27, 2021
1 parent 1f66413 commit 7fbe68f
Show file tree
Hide file tree
Showing 10 changed files with 639 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var (
pEmbeddedSDK = regexp.MustCompile(`github.com/hashicorp/terraform-provider-azurerm/internal/services/\w+/sdk`)
)

var (
flagDir = flag.String("dir", ".", "The root directory used when loading the Go packages")
)

const usage = `Terraform resource to Azure API path mapping generator.
Usage: generate-provider-resource-mapping package...
Expand All @@ -30,7 +34,7 @@ func main() {
os.Exit(1)
}

pkgs, err := loadPackage(".", flag.Args())
pkgs, err := loadPackage(*flagDir, flag.Args())

if err != nil {
log.Fatal(err)
Expand Down
77 changes: 77 additions & 0 deletions .tools/generate-provider-resource-mapping/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

MYDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TOOLDIR="$MYDIR/generate-provider-resource-mapping"
MYNAME="$(basename "${BASH_SOURCE[0]}")"
ROOTDIR="$MYDIR/../.."

die() {
echo "$@" >&2
exit 1
}

usage() {
cat << EOF
Usage: ./run.sh [options]
Options:
-h|--help show this message
Arguments:
provider_dir The path to the AzureRM provider repo
EOF
}

main() {
while :; do
case $1 in
-h|--help)
usage
exit 1
;;
--)
shift
break
;;
*)
break
;;
esac
shift
done

local expect_n_arg
expect_n_arg=1
[[ $# = "$expect_n_arg" ]] || die "wrong arguments (expected: $expect_n_arg, got: $#)"

provider_dir=$1

[[ -d $provider_dir ]] || die "no such directory: $provider_dir"

pushd $provider_dir > /dev/null
pkgs=(./internal/sdk ./internal/services/*)
popd > /dev/null
out=$(go run "$TOOLDIR" -dir "$provider_dir" "${pkgs[@]}") || die "failed to run the tool"
cat << EOF > "$ROOTDIR/mapping/mapping.go"
// Code generated by generate-provider-resource-mapping/run.sh; DO NOT EDIT.
package schema
import (
"encoding/json"
"fmt"
"os"
)
var ProviderResourceMapping = map[string]string{}
func init() {
b := []byte(\`$out\`)
if err := json.Unmarshal(b, &ProviderResourceMapping); err != nil {
fmt.Fprintf(os.Stderr, "unmarshalling the provider resource mapping: %s", err)
os.Exit(1)
}
}
EOF
}

main "$@"
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ install:

gen:
@./.tools/generate-provider-schema/run.sh $(PROVIDER_DIR) $(PROVIDER_VERSION)
@./.tools/generate-provider-resource-mapping/run.sh $(PROVIDER_DIR)

depscheck:
@echo "==> Checking source code with go mod tidy..."
Expand Down
556 changes: 556 additions & 0 deletions mapping/mapping.go

Large diffs are not rendered by default.

0 comments on commit 7fbe68f

Please sign in to comment.