github.com/openshift-online/ocm-sdk-go@v0.1.473/hack/verify-model-version.sh (about)

     1  #!/bin/bash -e
     2  
     3  # This script ensures that all the api-model submodules used across the project are all the same version.
     4  
     5  # Group 1: Shared modules to verify across root and examples
     6  COMMON_MODULES=(
     7    "github.com/openshift-online/ocm-api-model/model"
     8    "github.com/openshift-online/ocm-api-model/clientapi"
     9  )
    10  
    11  COMMON_DIRS=(
    12    "."
    13    "examples"
    14  )
    15  
    16  # Group 2: metamodel_generator module to only be checked in metamodel_generator
    17  METAMODEL_MODULE="github.com/openshift-online/ocm-api-model/metamodel_generator"
    18  METAMODEL_DIR="metamodel_generator"
    19  
    20  # Function to robustly extract module version from go.mod
    21  extract_version() {
    22    local module=$1
    23    local mod_file=$2
    24  
    25    awk -v mod="$module" '
    26      $1 == "require" && $2 == mod { print $3; exit }
    27      $1 == mod { print $2; exit }
    28    ' "$mod_file"
    29  }
    30  
    31  echo "Verifying shared module versions across ./ and ./examples..."
    32  
    33  status=0
    34  
    35  # Step 1: Check shared modules in . and examples
    36  for module in "${COMMON_MODULES[@]}"; do
    37    echo "Checking module: $module"
    38    ref_version=""
    39    consistent=true
    40  
    41    for dir in "${COMMON_DIRS[@]}"; do
    42      mod_file="$dir/go.mod"
    43      version=$(extract_version "$module" "$mod_file")
    44  
    45      if [ -z "$version" ]; then
    46        echo "  ❌ $module not found in $mod_file"
    47        consistent=false
    48        status=1
    49        continue
    50      fi
    51  
    52      echo "  ✔ $dir/go.mod: $version"
    53  
    54      if [ -z "$ref_version" ]; then
    55        ref_version="$version"
    56      elif [ "$version" != "$ref_version" ]; then
    57        echo "  ❌ Version mismatch in $mod_file: $version (expected: $ref_version)"
    58        consistent=false
    59        status=1
    60      fi
    61    done
    62  
    63    if $consistent; then
    64      echo "✅ All versions match for $module: $ref_version"
    65    else
    66      echo "❌ Version mismatch found for $module"
    67    fi
    68  
    69    echo
    70  done
    71  
    72  # Step 2: Check metamodel_generator module in metamodel_generator/go.mod
    73  echo "Checking metamodel module in $METAMODEL_DIR: $METAMODEL_MODULE"
    74  
    75  mod_file="$METAMODEL_DIR/go.mod"
    76  metamodel_version=$(extract_version "$METAMODEL_MODULE" "$mod_file")
    77  
    78  if [ -z "$metamodel_version" ]; then
    79    echo "❌ $METAMODEL_MODULE not found in $mod_file"
    80    status=1
    81  elif [ "$metamodel_version" != "$ref_version" ]; then
    82    echo "  ❌ Version mismatch in $mod_file: $metamodel_version (expected: $ref_version)"
    83    consistent=false
    84    status=1
    85  fi
    86  
    87  if $consistent; then
    88    echo "✅ All versions match for $METAMODEL_MODULE: $ref_version"
    89  else
    90    echo "❌ Version mismatch found for $METAMODEL_MODULE"
    91  fi
    92  
    93  exit $status