langgenius / dify-docs

The open-source repo for docs.dify.ai
Creative Commons Attribution 4.0 International
197 stars 176 forks source link

correct instruction to generate and replace SECRET_KEY value in .env file #292

Closed bowenliang123 closed 2 months ago

bowenliang123 commented 2 months ago

Before this PR: Failed to replace value in .env file with sed on Mac.

Pasted Graphic

(dify-api-py3.10) bw@bwdeMacBook-Pro-2 api % sed -i 's/SECRET_KEY=.*/SECRET_KEY=QDyaHv8M5kuJ6XpNxEHXZ0OD6B029cKNrOt+VPxky70ixTGAwRBNtULn/' .env sed: 1: ".env": invalid command code .

With this PR:

To solve this problem,

Possible approach 1 with sed :

On Mac:
sed -i '' "s/^SECRET_KEY=.*/SECRET_KEY=$(printf '%s' "$(openssl rand -base64 42)" | sed 's/[\/&]/\\&/g')/" .env

On Linux:
sed -i "s/^SECRET_KEY=.*/SECRET_KEY=$(printf '%s' "$(openssl rand -base64 42)" | sed 's/[\/&]/\\&/g')/" .env

Possible approach 2 with awk (recommended and used in this PR as it has clear syntax without escaping characters like sed):

 awk -v key="$(openssl rand -base64 42)" '/^SECRET_KEY=/ {sub(/=.*/, "=" key)} 1' .env > temp_env && mv temp_env .env
bowenliang123 commented 2 months ago

cc @crazywoola