Skip to content

Commit

Permalink
Support json on postresql
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki777 committed May 5, 2023
1 parent da1a4bb commit 699594a
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 26 deletions.
28 changes: 25 additions & 3 deletions postgresql/create-start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
#!/bin/bash

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
./create.sh $1 $2 $3
./create.sh -f "$format" $1 $2 $3 > /dev/null

set -eu
./start.sh $1
./start.sh -f "$format" $1
56 changes: 48 additions & 8 deletions postgresql/create.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
#!/bin/bash
set -eu

# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
Expand Down Expand Up @@ -38,10 +57,11 @@ extractFile $dir $optFileName
# install for linux
if [ ! -d $dir/basedir/bin ]; then
if [ $os = "linux" ]; then
echo "Installing..." 1>&2
cd $dir/basedir
./configure --prefix=$(pwd)
make
make install
./configure --prefix=$(pwd) 1>&2
make 1>&2
make install 1>&2
rm -fr config contrib doc src
fi
fi
Expand All @@ -51,11 +71,31 @@ $dir/basedir/bin/initdb \
--pgdata=$dir/datadir/$optName \
--username=postgres \
--encoding=UTF-8 \
--locale=en_US.UTF-8
echo "postgresql.conf is here. $dir/datadir/$optName/postgresql.conf"

--locale=en_US.UTF-8 1>&2
echo $optPort >$dir/datadir/$optName/postgresql.port.init

echo PostgreSQL Successfully created. $optName $optVersion $optPort
cd $currentDir
getCommands $optName $optVersion $optPort
commands=$(getCommands $optName $optVersion $optPort $format)

normalOutputs=""
normalOutputs="${normalOutputs}postgresql.conf is here. $dir/datadir/$optName/postgresql.conf\n"
normalOutputs="${normalOutputs}PostgreSQL Successfully created. $optName $optVersion $optPort\n"
normalOutputs="${normalOutputs}$commands\n"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"message\": \"PostgreSQL Successfully created.\",
\"name\": \"$optName\",
\"type\": \"postgresql\",
\"version\": \"$optVersion\",
\"port\": \"$optPort\",
\"dataDir\": \"$dir/datadir/$optName\",
\"confPath\": \"$dir/datadir/$optName/postgresql.conf\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi
48 changes: 45 additions & 3 deletions postgresql/delete.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
#!/bin/bash

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
. functions.sh

Expand All @@ -14,10 +36,30 @@ optPort=$(getPortByName "$optName" "$optVersion")

dir=$currentDir/versions/$optVersion

./stop.sh $optName $optVersion $optPort
./stop.sh -f "$format" $optName $optVersion $optPort > /dev/null

set -eu
exitIfNotExistDir $dir/datadir/$optName
exitIfRunningPort $optPort
[ -d "$dir/datadir/$optName" ] && rm -fr $dir/datadir/$optName
echo PostgreSQL Successfully deleted. $optName $optVersion $optPort

normalOutputs=""
normalOutputs="${normalOutputs}PostgreSQL Successfully deleted. $optName $optVersion $optPort"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"message\": \"PostgreSQL Successfully deleted.\",
\"name\": \"$optName\",
\"type\": \"postgresql\",
\"version\": \"$optVersion\",
\"port\": \"$optPort\",
\"dataDir\": \"$dir/datadir/$optName\",
\"confPath\": \"$dir/datadir/$optName/postgresql.conf\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi
39 changes: 37 additions & 2 deletions postgresql/port.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#!/bin/bash
set -eu

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
. functions.sh

Expand All @@ -21,4 +43,17 @@ optVersion=$(getVersionByName "$optName")
exitIfNotExistPortFile "$optName" "$optVersion"
optPort=$(getPortByName "$optName" "$optVersion")

echo "$optPort"
normalOutputs=""
normalOutputs="${normalOutputs}$optPort"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"port\": \"$optPort\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi
28 changes: 25 additions & 3 deletions postgresql/restart.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
#!/bin/bash

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
./stop.sh $1
./stop.sh -f "$format" $1 > /dev/null

set -eu
./start.sh $1
./start.sh -f "$format" $1
50 changes: 46 additions & 4 deletions postgresql/start.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#!/bin/bash
set -eu

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
. functions.sh

Expand All @@ -22,8 +44,28 @@ $dir/basedir/bin/pg_ctl \
--log $dir/datadir/$optName/postgres.log \
-w \
-o "-p $optPort" \
start
start 1>&2
head -1 $dir/datadir/$optName/postmaster.pid > $dir/datadir/$optName/postgresql.pid
echo $optPort > $dir/datadir/$optName/postgresql.port
echo "Your config file is located $dir/datadir/$optName/postgresql.conf"
echo PostgreSQL Successfully started. $optName $optVersion $optPort

normalOutputs=""
normalOutputs="${normalOutputs}PostgreSQL Successfully started. $optName $optVersion $optPort\n"
normalOutputs="${normalOutputs}Your config file is located $dir/datadir/$optName/postgresql.conf"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"message\": \"PostgreSQL Successfully started.\",
\"name\": \"$optName\",
\"type\": \"postgresql\",
\"version\": \"$optVersion\",
\"port\": \"$optPort\",
\"dataDir\": \"$dir/datadir/$optName\",
\"confPath\": \"$dir/datadir/$optName/postgresql.conf\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi
48 changes: 45 additions & 3 deletions postgresql/stop.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#!/bin/bash
set -eu

currentDir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))

currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd $currentDir
. functions.sh

Expand All @@ -22,8 +44,28 @@ $dir/basedir/bin/pg_ctl \
--log $dir/datadir/$optName/postgres.log \
-w \
-o "-p $optPort" \
stop
stop 1>&2
[ -f "$dir/datadir/$optName/postgresql.pid" ] && rm -f $dir/datadir/$optName/postgresql.pid
[ -f "$dir/datadir/$optName/postgresql.port" ] && cp $dir/datadir/$optName/postgresql.port $dir/datadir/$optName/postgresql.port.last
[ -f "$dir/datadir/$optName/postgresql.port" ] && rm -f $dir/datadir/$optName/postgresql.port
echo PostgreSQL Successfully stopped. $optName $optVersion $optPort

normalOutputs=""
normalOutputs="${normalOutputs}PostgreSQL Successfully stopped. $optName $optVersion $optPort"

jsonOutputs=""
jsonOutputs="$jsonOutputs{
\"message\": \"PostgreSQL Successfully stopped.\",
\"name\": \"$optName\",
\"type\": \"postgresql\",
\"version\": \"$optVersion\",
\"port\": \"$optPort\",
\"dataDir\": \"$dir/datadir/$optName\",
\"confPath\": \"$dir/datadir/$optName/postgresql.conf\"
}"

# Output
if [ "$format" = "json" ]; then
echo -e "${jsonOutputs}"
else
echo -e "${normalOutputs}"
fi

0 comments on commit 699594a

Please sign in to comment.