Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue adding speakers with script #13876

Open
jerdog opened this issue Mar 27, 2024 · 4 comments
Open

Issue adding speakers with script #13876

jerdog opened this issue Mar 27, 2024 · 4 comments

Comments

@jerdog
Copy link
Contributor

jerdog commented Mar 27, 2024

I'm trying to add our speakers, and getting the following error:

utilities git:(jerdog/kc2024-AddProgram) ✗ ./add_speakers.sh                                 
Enter your event year (default: 2024): 2024
Enter your city name: Kansas City
sed: 1: "../content/events/2024- ...": invalid command code .

Anyone else seeing this? It will create the folder speakers but errors out I'm guessing here since it doesn't create the file

cp examples/templates/speakers.md $speakerspage

@yvovandoorn
Copy link
Contributor

Putting in my comments from Slack in here:

It looks like its coming out of sed .. specifically the BSD shipped version on macOS.

Specifically here:

+ escaped_rhs='Kansas City'
+ file=../content/events/2024-kansas-city/speakers.md
+ [[ -w ../content/events/2024-kansas-city/speakers.md ]]
+ sed -i 's/CITY/Kansas City/' ../content/events/2024-kansas-city/speakers.md
sed: 1: "../content/events/2024- ...": invalid command code .

The script calls function string_replace which is being set in

string_replace() {
# requires 3 arguments
if [[ $# -ne 3 ]] ; then
echo "string_replace requires 3 arguments: key to be replaced, string replacement, file"
exit 1
fi
# escape our left hand side, and right hand side of sed args
escaped_lhs=$(printf '%s\n' "$1" | sed 's:[][\\/.^$*]:\\&:g')
escaped_rhs=$(printf '%s\n' "$2" | sed 's:[\\/&]:\\&:g;$!s/$/\\/')
file=$3
if [[ -w "${file}" ]] ; then
# sed -i'' should work for macos and linux equally.
sed -i'' "s/${escaped_lhs}/${escaped_rhs}/" "${file}"
else
echo "WARNING: ${file} not writeable"
return 1
fi
}
.

The same file has some sed detection logic which is going unused.

function sedcmd() {
local osname=`uname`
local gnused=$(which sed)
if [[ $osname == 'Linux' ]]; then
sed -i "$@"
elif [[ $osname == 'Darwin' && $gnused == '/usr/local/bin/sed' ]]; then
sed -i "$@"
else
sed -i '' "$@"
fi
}

Using the sedcmd function to execute the command makes the script function.

diff --git a/utilities/common_code b/utilities/common_code
index 3903c0b46c..c8592c0b1d 100644
--- a/utilities/common_code
+++ b/utilities/common_code
@@ -33,7 +33,8 @@ string_replace() {

   if [[ -w "${file}" ]] ; then
     # sed -i'' should work for macos and linux equally.
-    sed -i'' "s/${escaped_lhs}/${escaped_rhs}/" "${file}"
+    # sed -i'' "s/${escaped_lhs}/${escaped_rhs}/" "${file}"
+    sedcmd "s/${escaped_lhs}/${escaped_rhs}/" "${file}"
   else
     echo "WARNING: ${file} not writeable"
     return 1

Output:

+ escaped_rhs='Kansas City'
+ file=../content/events/2024-kansas-city/speakers.md
+ [[ -w ../content/events/2024-kansas-city/speakers.md ]]
+ sedcmd 's/CITY/Kansas City/' ../content/events/2024-kansas-city/speakers.md
uname
++ uname
+ local osname=Darwin
which sed
++ which sed
+ local gnused=/usr/bin/sed
+ [[ Darwin == \L\i\n\u\x ]]
+ [[ Darwin == \D\a\r\w\i\n ]]
+ [[ /usr/bin/sed == \/\u\s\r\/\l\o\c\a\l\/\b\i\n\/\s\e\d ]]
+ sed -i '' 's/CITY/Kansas City/' ../content/events/2024-kansas-city/speakers.md

# Prompt for inputting speakers
while [ 1 ]
do
echo "Entering speakers; use CTRL+C to stop..."

Before I submit a PR, would be good to get context from @mattstratton and @rexroof, as this change was introduced in 3d92859.

@mattstratton
Copy link
Member

I don't know what the change was for, so I can't weigh in, but probably safe to revert it?

@rexroof
Copy link
Contributor

rexroof commented Mar 28, 2024

I added this general solution so that the script could support replacement strings that contained numerous special characters.

it is possible it was only tested on linux.

@jerdog
Copy link
Contributor Author

jerdog commented Apr 1, 2024

Yeah I am on MacOS so that could explain it @rexroof

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants