· 8 years ago · Jan 04, 2018, 03:32 PM
1#!/bin/bash
2
3### MAIN block at the bottom
4
5function lookupData {
6 if [ ! -f .cache/$1.json ]; then
7 echo "Fetching data from maven.org for $1"
8 httpStatusCode=$( curl --silent -w '%{http_code}' -X GET 'https://search.maven.org/solrsearch/select?q=1%3A%22'"$1"'%22' -o '.cache/'"$1"'.json' )
9 if [ $? -ne 0 ]; then echo "curl did not exit with status 0"; exit 1; fi
10 else
11 echo "Found cached data for $1, not fetching them again"
12 fi
13}
14
15function addToLicenseManager {
16 scannedFileName=$2
17 sha1sumOfFile=$1
18 packageLicense=$( jq -r '.license_info' .cache/$1.packageinfo )
19 echo "Adding the jar $2 with the checksum $1 and the license $packageLicense to your license_finder"
20 license_finder dependencies add $scannedFileName $packageLicense
21}
22
23function lookupPackageInfo {
24 echo "Looking up package info for $1"
25 if [ ! -f .cache/$1.packageinfo ]; then
26 lang="Java"
27 group=$( jq -r '.response.docs [0].g' .cache/$1.json )
28 artifact=$( jq -r '.response.docs [0].a' .cache/$1.json )
29 prod_key="${group}/${artifact}"
30 prod_version=$( jq -r '.response.docs [0].v' .cache/$1.json )
31 unslashed_prod_key="${prod_key////:}"
32 undotted_prod_key="${unslashed_prod_key//./\~}"
33 echo $undotted_prod_key
34 httpStatusCode=$( curl --silent -w '%{http_code}' -X GET --header 'Accept: application/json' 'https://www.versioneye.com/api/v2/products/'"$lang"'/'"$undotted_prod_key"'?prod_version='"$prod_version"'&api_key='"$SECRET" -o '.cache/'"$1"'.packageinfo' )
35 if [ $? -ne 0 ]; then echo "curl did not exit with status 0"; exit 1; fi
36 else
37 echo "Package Info in cache, not re-requesting"
38 fi
39}
40
41function generateReport {
42 echo "Entering generateReport"
43 while read line; do
44 checksum=$(sha1sum ${line} | cut -d ' ' -f 1)
45 echo "sha1sum for $line is: $checksum"
46 lookupData ${checksum}
47 lookupPackageInfo ${checksum}
48 addToLicenseManager ${checksum} ${line} ${result}
49 echo "Waiting for 70 seconds"
50 sleep 70
51 done < .foundjars
52}
53
54function validateTooling {
55 for required_tool in ruby license_finder curl jq;
56 do
57 echo -n "Validating $required_tool is on your machine... "
58 command -v ${required_tool} >/dev/null 2>&1 || { echo >&2 "I require $required_tool but it's not installed. Aborting."; exit 1; }
59 echo "Found $required_tool"
60 done
61}
62
63function sanitizeEnv {
64 echo "Deleting old .foundjars file"
65 rm .foundjars
66 echo "Validating cache dir exists"
67 if [ ! -d .cache ]; then
68 echo "Cache dir created..."
69 mkdir .cache
70 fi
71}
72
73function generateJarLists {
74 echo "Finding *.jar files"
75 echo -n $( find -name *.jar | tee .foundjars | wc -l )
76 echo " .jar file(s) found"
77
78}
79
80#### MAIN Block starts here
81clear
82
83if [ -f secretkey ]; then
84 echo "Loading secret from file"
85 SECRET=$(<secretkey)
86
87else
88 echo "secretkey file not found, bailing out"
89 exit 1
90fi
91
92validateTooling
93
94sanitizeEnv
95
96generateJarLists
97
98license_finder project_name add $PWD
99
100generateReport