Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

MSR: replace grep -P with sed #1301

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/plugins/conditionals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ sudo kdb mount main.ini /examples/conditionals ni
sudo kdb mount sub.ini /examples/conditionals/sub ini

# mount conditionals as global plugin
sudo kdb global-mount conditionals
sudo kdb global-mount conditionals || $(exit 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added #1306 to revert this later


# create testfiles
cat > `kdb file /examples/conditionals` << EOF \
Expand All @@ -113,9 +113,9 @@ EOF
echo "key = false" > `kdb file /examples/conditionals/sub`

# should fail and yield an error
kdb export /examples/conditionals ini
#> sub/key = false
kdb export /examples/conditionals simpleini
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid simpleini, it currently does not work on macOS. (In particular such a change is strange in a PR attempting to do macOS fixes 😆) Any reason why you used it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, didn't know that.
i changed it to simpleini because it's easier to read in tutorial after ini started to support metakeys (the metakey would be printed to stdout too)
i could add a nometa flag to ini though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sanssecours said he will add a simpleini replacement for MacOS. #701

We should not make ini even more complicated.

#> key1 = val1
#> sub/key = false
# ERRORS:135
# Error (#135) occurred!
# Description: Validation failed
Expand All @@ -129,9 +129,9 @@ kdb export /examples/conditionals ini
kdb set /examples/conditionals/sub/key true

# should succeed
kdb export /examples/conditionals ini
#> sub/key = true
kdb export /examples/conditionals simpleini
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again

#> key1 = val1
#> sub/key = true

# cleanup
kdb rm -r /examples/conditionals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
INFILE="$1"


BLOCKS=$(grep -oPz '(?s)```sh.*?```\n' "$1")
BLOCKS=$(sed -n '/```sh/,/```\n/p' "$1")
BUF=

COMMAND=
Expand Down Expand Up @@ -110,17 +110,17 @@ translate()
grep -Eq "^(\s)*#>" <<< "$line"
if [ -z "$OUTBUF" ];
then
tmp=$(grep -Po "(?<=#\> ).*" <<<"$line")
tmp=$(sed -n 's/\(\s\)*#> \(.*\)/\2/p' <<<"$line")
OUTBUF="$tmp"
else
tmp=$(grep -Po "(?<=#\> ).*" <<< "$line")
tmp=$(sed -n 's/\(\s\)*#> \(.*\)/\2/p' <<<"$line")
OUTBUF=$(echo -en "${OUTBUF}\n${tmp}")
fi

grep -Eq "^(\s*)#" <<< "$line"
if [ "$?" -eq 0 ];
then
tmp=$(grep -Po "(?<=\# )(.*)" <<< "$line")
tmp=$(sed -n 's/\(\s\)*# \(.*\)/\2/p' <<<"$line")
cmd=$(cut -d ':' -f1 <<< "$tmp")
arg=$(cut -d ':' -f2- <<< "$tmp")

Expand Down Expand Up @@ -165,7 +165,7 @@ translate()
COMMAND=$(sed "s/\`[[:blank:]]*sudo\ /\`/" <<< "$COMMAND")
if [ "${line: -1}" == "\\" ];
then
COMMAND="${COMMAND::-1}"
COMMAND="${COMMAND%?}"
fi
while [ "${line: -1}" == "\\" ];
do
Expand All @@ -174,7 +174,7 @@ translate()
line=$(sed "s/\`[[:blank:]]*sudo\ /\`/" <<< "$line")
if [ "${line: -1}" == "\\" ];
then
COMMAND=$(printf "%s\\\n%s" "$COMMAND" "${line::-1}")
COMMAND=$(printf "%s\\\n%s" "$COMMAND" "${line%?}")
else
COMMAND=$(printf "%s\\\n%s\\\n" "$COMMAND" "$line")
fi
Expand All @@ -184,19 +184,28 @@ translate()
done <<<"$BUF"
writeBlock "$TMPFILE"
../shell_recorder.sh "$TMPFILE"
# rm "$TMPFILE"
rm "$TMPFILE"
}

INBLOCK=0
IFS=''

MOUNTPOINTS_BACKUP=$(kdb mount)

while read -r line;
do
grep -Eq '(\s)*```sh$' <<<"$line"
if [ "$?" -eq 0 ];
then
INBLOCK=1
continue;
fi
grep -Eq '(\s)*```$' <<<"$line"
if [ "$?" -eq 0 ];
then
INBLOCK=0
continue
fi
if [ $INBLOCK -eq 0 ];
then
continue
fi
Expand All @@ -210,3 +219,16 @@ done <<<"$BLOCKS"

translate

MOUNTPOINTS=$(kdb mount)

if [ "$MOUNTPOINTS_BACKUP" != "$MOUNTPOINT" ];
then
IFS='
'
TOUMOUNT=$(diff <(echo "$MOUNTPOINTS_BACKUP") <(echo "$MOUNTPOINTS") | grep -Eo "^>.*")
for line in $TOUMOUNT;
do
mp=$(sed -n 's/\(.*\)with name \(.*\)/\2/p' <<< "$line")
kdb umount "$mp"
done
fi