From daa85ddda1c08f6e816443fef8c9164b7749d91a Mon Sep 17 00:00:00 2001 From: Maksym Bilan Date: Mon, 23 Dec 2024 18:45:02 +0200 Subject: [PATCH] Version command (#78) * New version file * script refactoring * New command * clean up * new env var * Fixing issues --------- Co-authored-by: Maksym Bilan <> --- .github/workflows/deploy.yml | 1 + VERSION | 1 + function.go | 3 ++- internal/bot/command.go | 1 + internal/bot/session.go | 2 ++ internal/bot/version.go | 8 ++++++++ scripts/bump_version.sh | 29 +++++++---------------------- scripts/create_tag.sh | 20 +++----------------- scripts/deploy_functions.sh | 6 +++++- scripts/get_version.sh | 31 ++++++++++++------------------- 10 files changed, 42 insertions(+), 60 deletions(-) create mode 100644 VERSION create mode 100644 internal/bot/version.go diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e1a1b8c..79449e3 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -29,6 +29,7 @@ jobs: - name: Run script file run: | + chmod +x ./scripts/get_version.sh chmod +x ./scripts/deploy_functions.sh ./scripts/deploy_functions.sh env: diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..ece61c6 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.0.6 \ No newline at end of file diff --git a/function.go b/function.go index 4b4554a..5ee8fb4 100644 --- a/function.go +++ b/function.go @@ -3,6 +3,7 @@ package capymind import ( "log" "net/http" + "os" "github.com/GoogleCloudPlatform/functions-framework-go/functions" "github.com/capymind/internal/bot" @@ -10,7 +11,7 @@ import ( ) func init() { - log.Printf("Version: %s", AppVersion) + log.Printf("Version: %s", os.Getenv("APP_VERSION")) functions.HTTP("handler", handler) functions.HTTP("schedule", schedule) diff --git a/internal/bot/command.go b/internal/bot/command.go index 6c94ee7..e0a0eba 100644 --- a/internal/bot/command.go +++ b/internal/bot/command.go @@ -14,6 +14,7 @@ const ( Language Command = "/language" Settings Command = "/settings" Help Command = "/help" + Version Command = "/version" // Hidden user commands Timezone Command = "/timezone" diff --git a/internal/bot/session.go b/internal/bot/session.go index 42c52be..ae1f996 100644 --- a/internal/bot/session.go +++ b/internal/bot/session.go @@ -75,6 +75,8 @@ func handleSession(session *Session) { startFeedback(session) case Help: handleHelp(session) + case Version: + handleVersion(session) case SleepAnalysis: handleSleepAnalysis(session) case WeeklyAnalysis: diff --git a/internal/bot/version.go b/internal/bot/version.go new file mode 100644 index 0000000..3a6060f --- /dev/null +++ b/internal/bot/version.go @@ -0,0 +1,8 @@ +package bot + +import "os" + +func handleVersion(session *Session) { + versionStr := "Version: " + os.Getenv("APP_VERSION") + setOutputText(versionStr, session) +} diff --git a/scripts/bump_version.sh b/scripts/bump_version.sh index 445c528..b8dc5a8 100755 --- a/scripts/bump_version.sh +++ b/scripts/bump_version.sh @@ -1,22 +1,8 @@ #!/bin/bash -# Get the directory of this script -SCRIPT_DIR=$(dirname "$0") - -# Path to the version.go file (adjusted for the relative path) -VERSION_FILE="$SCRIPT_DIR/../version.go" - -# Check if version.go exists -if [[ ! -f "$VERSION_FILE" ]]; then - echo "Error: $VERSION_FILE not found." - exit 1 -fi - -# Path to get_version.sh -GET_VERSION_SCRIPT="$SCRIPT_DIR/get_version.sh" - -# Import the current version using the get_version.sh script -CURRENT_VERSION=$("$GET_VERSION_SCRIPT") +# Get current version +source ./scripts/get_version.sh +CURRENT_VERSION=$(get_version) if [[ $? -ne 0 ]]; then echo "Error: Failed to read the current version." @@ -47,11 +33,10 @@ fi # Construct the new version NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}" -# Update the version in the file -sed -i.bak -E "s/(const AppVersion = \")[0-9]+\.[0-9]+\.[0-9]+(\".*)/\1${NEW_VERSION}\2/" "$VERSION_FILE" +# Write the new version to the VERSION file +SCRIPT_DIR=$(dirname "$0") +VERSION_FILE="$SCRIPT_DIR/../VERSION" +echo "$NEW_VERSION" > "$VERSION_FILE" # Print success message echo "Version bumped from $CURRENT_VERSION to $NEW_VERSION in $VERSION_FILE" - -# Optional: Remove the backup file created by sed -rm -f "${VERSION_FILE}.bak" diff --git a/scripts/create_tag.sh b/scripts/create_tag.sh index c337aed..b5fb5ba 100755 --- a/scripts/create_tag.sh +++ b/scripts/create_tag.sh @@ -1,22 +1,8 @@ #!/bin/bash -# Get the directory of this script -SCRIPT_DIR=$(dirname "$0") - -# Path to the version.go file (adjusted for the relative path) -VERSION_FILE="$SCRIPT_DIR/../version.go" - -# Check if version.go exists -if [[ ! -f "$VERSION_FILE" ]]; then - echo "Error: $VERSION_FILE not found." - exit 1 -fi - -# Path to get_version.sh -GET_VERSION_SCRIPT="$SCRIPT_DIR/get_version.sh" - -# Import the current version using the get_version.sh script -CURRENT_VERSION=$("$GET_VERSION_SCRIPT") +# Get current version +source ./scripts/get_version.sh +CURRENT_VERSION=$(get_version) # Create a new tag git tag -a "releases/$CURRENT_VERSION" -m "$CURRENT_VERSION" diff --git a/scripts/deploy_functions.sh b/scripts/deploy_functions.sh index 3669366..bbb4aa1 100755 --- a/scripts/deploy_functions.sh +++ b/scripts/deploy_functions.sh @@ -12,8 +12,12 @@ RUNTIME="go122" # Set the project ID PROJECT_ID=$CAPY_PROJECT_ID +# Get current version +source ./scripts/get_version.sh +APP_VERSION=$(get_version) + # Set environment variables -ENV_PARAMS=("CAPY_PROJECT_ID=$CAPY_PROJECT_ID" "CAPY_SERVER_REGION=$CAPY_SERVER_REGION" "CLOUD=true") +ENV_PARAMS=("CAPY_PROJECT_ID=$CAPY_PROJECT_ID" "CAPY_SERVER_REGION=$CAPY_SERVER_REGION" "CLOUD=true" "APP_VERSION=$APP_VERSION") ENV_VARS="" for PARAM in "${ENV_PARAMS[@]}"; do ENV_VARS+="$PARAM," diff --git a/scripts/get_version.sh b/scripts/get_version.sh index 01bc8e9..0262457 100755 --- a/scripts/get_version.sh +++ b/scripts/get_version.sh @@ -1,24 +1,17 @@ #!/bin/bash -# File containing the version constant (relative to the script location) -VERSION_FILE="./version.go" +get_version() { + local script_dir version_file + script_dir=$(dirname "$0") + version_file="$script_dir/../VERSION" -# Pattern to match the version line -VERSION_PATTERN='const AppVersion = "' + if [[ ! -f "$version_file" ]]; then + echo "Error: $version_file not found." >&2 + exit 1 + fi -# Check if version.go exists -if [[ ! -f "$VERSION_FILE" ]]; then - echo "Error: $VERSION_FILE not found." - exit 1 -fi + cat "$version_file" +} -# Extract the current version -CURRENT_VERSION=$(grep "$VERSION_PATTERN" "$VERSION_FILE" | sed -E 's/.*"([0-9]+\.[0-9]+\.[0-9]+)".*/\1/') - -if [[ -z "$CURRENT_VERSION" ]]; then - echo "Error: Could not find the current version in $VERSION_FILE" - exit 1 -fi - -# Output the current version -echo "$CURRENT_VERSION" \ No newline at end of file +CURRENT_VERSION=$(get_version) +echo "The current version is: $CURRENT_VERSION" \ No newline at end of file