-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsf-rel
More file actions
executable file
·128 lines (115 loc) · 3.6 KB
/
sf-rel
File metadata and controls
executable file
·128 lines (115 loc) · 3.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash
#
# Given set of csv files (retrieved using 'sf-data'), extract all records related to a given object Id
#
# USAGE
#
# sf-rel ID # extract records related to ID
# sf-rel -s ID # no output (silent)
# sf-rel -r ID # restore original csv files
# sf-rel -R # explicitly restore original csv files and remove dot files
#
# EXAMPLES
#
# sf-data; cd .data; sf-rel 0018E000027VckFQAS # extract records related to the ID '0018E000027VckFQAS'
#
tmp=$(mktemp)
silent=false
restore=false
show_help() {
awk '/^[^ #]/{c=1}c==0{print $0}' $0 | sed -n '/^#/p' | sed 1d | sed 's/^#/ /g' \
| perl -pe "s/ #(.*)$/$(tput setaf 0)\1$(tput sgr 0)/" \
| perl -pe "s/(USAGE|EXAMPLES)/$(tput setaf 0)\1$(tput sgr 0)/" \
| perl -pe "s/\`(.+)\`/$(tput sgr 0 1)\1$(tput sgr 0)/"
exit 1
}
[[ -z "$1" ]] && show_help
while [ $# -gt 0 ]; do
case $1 in
-R)
fd -e orig -x mv {} {.}
fd -e dot -x rm {}
exit 0
;;
-r)
restore=true
;;
-s)
silent=true
;;
-h|--help)
show_help
;;
*)
id=$1
[ ! "${#id}" -eq 18 ] && show_help
;;
esac
shift
done
[ -z $id ] && show_help
# restore & backup 'object.csv' to 'object.csv.orig'
fd -e orig -x mv {} {.}
while read f; do
[ ! -f "${f}.orig" ] && cp "$f" "${f}.orig"
done < <(fd -I -e csv)
# clean up for proper line based bash operations:
# 1. remove some Id-columns we are not interested in
# 2. replace line breaks within quoted fields with white space
while read f; do
csvtk cut -m -f -OwnerId,-CreatedById,-LastModifiedById,-RecordTypeId,-ProfileId "$f" | csvtk replace -f '1-' -p '\n' -r ' ' > ${tmp}
mv ${tmp} "$f"
done < <(fd -I -e csv)
# extract related records (all records having the given Id in a column) from all available csv files
while read f; do
cat "$f" | awk -v id=$id '$0 ~ id || NR==1{print $0}' > ${tmp}
lines=$(wc -l ${tmp} | awk '{print $1}')
if [[ ${lines} == 1 ]]; then
rm "$f"
else
mv ${tmp} "$f"
[ $silent == false -a $restore == false ] && echo "extracted $(tput setaf 4)${f}$(tput sgr 0)"
fi
done < <(fd -I -e csv)
# draw diagram
if [ $silent == false ]; then
while read -r f; do
while read -r col; do
if [[ "$col" == "Id" ]]; then
sobj="${f%.*}"
break 2
fi
done < <(csvtk transpose "$f" | rg "$id" | csvtk cut -f 1)
done < <(fd -I -e csv)
dotfile="rel_${id}.dot"
echo "digraph {" > $dotfile
while read -r f; do
obj=${f%.*}
if [[ "$obj" == "$sobj" ]]; then
dot="${obj} [ label = \"@${obj}"
else
dot="${obj} [ label = \"#${obj}"
fi
if [[ ! "$obj" == "$sobj" ]]; then
dot="${dot} \\n Id:~$(csvtk transpose "$f" | rg --color=never "^Id," | csvtk cut -f 2)"
fi
while read -r col; do
dot="${dot} \\n $(echo ${col} | csvtk cut -f 1):~$(echo ${col} | csvtk cut -f 2)"
done < <(csvtk transpose "$f" | rg --color=never "$id")
echo "${dot}\" ]" >> $dotfile
if [[ ! "$obj" == "$sobj" ]]; then
echo "${obj} -- ${sobj}" >> $dotfile
fi
done < <(fd -I -e csv)
echo "}" >> $dotfile
[[ $DEBUG == "1" ]] && echo -e "\n$(tput setaf 0)$(cat $dotfile)$(tput sgr0)"
echo
cat $dotfile \
| graph-easy --from=dot --as=boxart \
| perl -pe "s/@(.+?) / $(tput setaf 1)\1$(tput sgr 0) /g" \
| perl -pe "s/#(.+?) / $(tput setaf 2)\1$(tput sgr 0) /g" \
| perl -pe "s/~(.+?) / $(tput setaf 0)\1$(tput sgr 0) /g"
fi
if [ $restore == true ]; then
fd -e orig -x mv {} {.}
fi