-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathgo-sum-to-src.sh
More file actions
executable file
·87 lines (82 loc) · 2.6 KB
/
go-sum-to-src.sh
File metadata and controls
executable file
·87 lines (82 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
#
# Copyright (c) 2021 Zededa, Inc.
# SPDX-License-Identifier: Apache-2.0
#
# Given lines from go.sum file(s) on stdin, create a list of tar files
# for each referenced package with the exact sha/commit.
# Skips lines with /go.mod in them since those are just hashes of the go.mod
# files
# Example: to use all go.sum files under current working directory:
# cat $(find . -name go.sum) | sort -u | ./go-sum-to-src.sh
outdir=/tmp/$$
verbose=
while getopts vo: o
do case "$o" in
v) verbose=1;;
o) outdir="$OPTARG";;
[?]) echo "Usage: $0 [-v] [-o <outdir>]"
exit 1;;
esac
done
shift $((OPTIND-1))
if [ $# -gt 0 ]; then
echo "Usage: $0 [-v] [-o <outdir>]"
exit 1
fi
if [ -f "$outdir" ]; then
echo "$outdir exists"
exit 1
fi
if [ -d "$outdir" ]; then
echo "$outdir exists"
exit 1
fi
mkdir -p "$outdir"
echo "outdir: $outdir"
count=0
while read -r line; do
# shellcheck disable=SC2086
set -- $line
if echo "$2" | grep -sq "/go.mod"; then
# echo "Skip $2"
continue
fi
pkgref="$1@$2"
[ -n "$verbose" ] && echo "pkgref: $pkgref"
go get -d "$pkgref"
src=$(go list -mod=readonly -m -json "$pkgref" | jq -r '.Dir')
# Remove leading $HOME from src
cleansrc=${src//$HOME//}
dst=$outdir/$cleansrc.tgz
destdir=$(dirname "$dst")
mkdir -p "$destdir"
[ -n "$verbose" ] && echo "Creating $dst"
tar -cz -f "$dst" "$src"
# Need more sane check to handle multiple LICENSE files/symlinks
if [ -f "$src"/LICENSE ]; then
cp -p "$src"/LICENSE "$outdir"/"$cleansrc".LICENSE
elif [ -f "$src"/LICENSE.txt ]; then
cp -p "$src"/LICENSE.txt "$outdir"/"$cleansrc".LICENSE
elif [ -f "$src"/LICENSE.md ]; then
cp -p "$src"/LICENSE.md "$outdir"/"$cleansrc".LICENSE
elif [ -f "$src"/LICENSE.rst ]; then
cp -p "$src"/LICENSE.rst "$outdir"/"$cleansrc".LICENSE
elif [ -f "$src"/License ]; then
cp -p "$src"/License "$outdir"/"$cleansrc".LICENSE
elif [ -f "$src"/COPYING ]; then
cp -p "$src"/COPYING "$outdir"/"$cleansrc".LICENSE-COPYING
else
# XXX debug
find "$src" | grep -i license
find "$src" | grep -i copying
touch "$outdir"/"$cleansrc".NO-LICENSE
fi
count=$((count +1))
done
# c2=$(find "$outdir" -type f|wc -l)
echo "Saved $count files $(du -sm "$outdir" | cut -f 1) Mbytes in $outdir"
lc=$(find "$outdir" -type f -name '*.LICENSE*' | wc -l)
echo "Saved $lc LICENSE files in $outdir"
nlc=$(find "$outdir" -type f -name '*.NO-LICENSE' | wc -l)
echo "$nlc packages without LICENSE file in $outdir"