|
/** |
|
* Usage: Run the script: groovy compare-properties.groovy |
|
* You will be prompted for application name (finance-app), source environment (eg. uat3) and target environment (prod) |
|
*/ |
|
|
|
// Script configuration |
|
|
|
def propertiesFileName = 'application.properties' |
|
def basePath = 'src/main/resources' |
|
|
|
// End of script configuration - no more config below this point |
|
|
|
def applicationName, src, target, diffMode |
|
|
|
def allowedDiffModes = ['1', '2', '3'] |
|
|
|
if (args.length == 0) { |
|
diffMode = System.console().readLine 'Select mode: \n ' + |
|
'Enter 1 to evaluate missing properties. \n ' + |
|
'Enter 2 to evaluate value mismatches.\n ' + |
|
'Enter 3 to find properties where values are duplicating. \n ' + |
|
'Choice: ' |
|
|
|
if (!allowedDiffModes.contains(diffMode)) { |
|
println 'Invalid option selected' |
|
System.exit(0) |
|
} |
|
|
|
|
|
applicationName = System.console().readLine 'Enter application name: ' |
|
src = System.console().readLine 'Enter Source Environment: ' |
|
|
|
if (['1', '2'].contains(diffMode)) |
|
target = System.console().readLine 'Enter Target Environment: ' |
|
|
|
} else { |
|
diffMode = args[0] |
|
applicationName = args[1] |
|
src = args[2] |
|
target = args[3] |
|
} |
|
|
|
Properties srcProperties = new Properties() |
|
Properties targetProperties = new Properties() |
|
|
|
File srcPropertiesFile = new File("${basePath}/${applicationName}/${src}/${propertiesFileName}") |
|
File targetPropertiesFile = new File("${basePath}/${applicationName}/${target}/${propertiesFileName}") |
|
|
|
Closure loadPropertiesFromFile = { |
|
File propertiesFile, Properties loadIntoProps, String env, String appName -> |
|
if (env) { |
|
try { |
|
propertiesFile.withInputStream { |
|
loadIntoProps.load(it) |
|
} |
|
} |
|
catch (FileNotFoundException e) { |
|
println "Environment ${env} not defined for the application ${appName}. \nExiting script. \n" |
|
System.exit(0) |
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
loadPropertiesFromFile(srcPropertiesFile, srcProperties, src, applicationName) |
|
loadPropertiesFromFile(targetPropertiesFile, targetProperties, target, applicationName) |
|
|
|
// Only the keys |
|
List srcPropListKeys = srcProperties.collect { return it.key } |
|
List targetPropListKeys = targetProperties.collect { return it.key } |
|
|
|
List srcPropList = srcProperties.collect { return ["${it.key}": it.value] } |
|
List targetPropList = targetProperties.collect { return ["${it.key}": it.value] } |
|
|
|
|
|
Closure evaluateMissingProps = { |
|
println "Properties in ${src} that are not in ${target}: " |
|
(srcPropListKeys - targetPropListKeys).size == 0 ? println(' None') : (srcPropListKeys - targetPropListKeys).each { |
|
println " ${it} \n" |
|
} |
|
|
|
println "\n" |
|
|
|
println "Properties in ${target} that not in ${src}:" |
|
(targetPropListKeys - srcPropListKeys).size == 0 ? println(' None') : (targetPropListKeys - srcPropListKeys).each { |
|
println " ${it} \n" |
|
} |
|
|
|
} |
|
|
|
Closure evaluateValueMismatches = { |
|
List diff = (srcPropList - targetPropList) |
|
println "${diff.size} properties in ${src} are different in ${target}: " |
|
|
|
diff.size == 0 ? println('None') : diff.each { |
|
it.each { k, v -> |
|
|
|
def targetProp = targetProperties.find { it.key == k } |
|
println "-----" |
|
println " $k " |
|
println "\t${src}\t: $v " |
|
println "\t${target}\t: ${targetProp?.value ? targetProp?.value : " ** DOES NOT EXIST IN: ${target}. ** "}" |
|
} |
|
} |
|
} |
|
|
|
Closure findDuplicatingProperties = { |
|
|
|
srcProperties.groupBy { it.value }.findAll { it.value.size() > 1 }?.each { |
|
it.value.each { println "${it.key}=${it.value}" } |
|
} |
|
} |
|
|
|
|
|
println '*********** START ***********' |
|
|
|
println "Number of properties in ${applicationName}/${src}: ${srcPropListKeys.size}\n" |
|
|
|
if (target) println "Number of properties in ${applicationName}/${target}: ${targetPropListKeys.size}\n" |
|
|
|
|
|
if (diffMode == '1') |
|
evaluateMissingProps() |
|
else if (diffMode == '2') |
|
evaluateValueMismatches() |
|
else if (diffMode == '3') |
|
findDuplicatingProperties() |
|
else |
|
throw new RuntimeException('Invalid mode selected...') |
|
|
|
|
|
println "\n *********** END *********** \n" |