Skip to content

Commit 4d65418

Browse files
authored
Merge pull request #7 from sqids/check-multiple-bash-versions
Test multiple bash versions
2 parents b39eeac + 1e787c0 commit 4d65418

File tree

5 files changed

+58
-17
lines changed

5 files changed

+58
-17
lines changed

.github/workflows/tests.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,29 @@ on:
77
pull_request:
88
branches:
99
- main
10+
schedule:
11+
- cron: '0 0 1 * *' # Run on the first day of every month
12+
workflow_dispatch:
1013

1114
jobs:
1215
test:
16+
strategy:
17+
matrix:
18+
version: ['4.0', '4.1', '4.2', '4.3', '4.4', '5.0', '5.1', 'latest']
19+
fail-fast: false
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v4
23+
24+
- name: Create docker image and run container
25+
run: |
26+
docker build --build-arg VERSION=${{ matrix.version }} -t "sqids-bash/test:bash-${{ matrix.version }}" .
27+
docker run -id --name "bash-${{ matrix.version }}" "sqids-bash/test:bash-${{ matrix.version }}"
28+
29+
- name: Run tests
30+
run: docker container exec "bash-${{ matrix.version }}" bats ./tests
31+
32+
lint:
1333
runs-on: ubuntu-latest
1434
steps:
1535
- uses: actions/checkout@v4
@@ -22,12 +42,3 @@ jobs:
2242

2343
- name: Run ShellCheck
2444
uses: ludeeus/action-shellcheck@master
25-
26-
- name: Set up Bats
27-
run: |
28-
git clone https://github.com/bats-core/bats-core.git
29-
cd bats-core
30-
sudo ./install.sh /usr/local
31-
32-
- name: Run tests
33-
run: bats -T ./tests

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# CHANGELOG
22

3-
## [1.0.1] - 2024-01-28
3+
## [1.0.1] - 2024-01-29
44

55
### Fixed
66

77
- Fix encoding numbers with leading zeros throws an error. (#6)
8+
- Works with bash version 4.0 or higher (#7)
89

910
## [1.0.0] - 2024-01-28
1011

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ARG VERSION
2+
3+
FROM bash:${VERSION}
4+
5+
COPY ./src /var/tmp/src
6+
COPY ./tests /var/tmp/tests
7+
8+
WORKDIR /var/tmp
9+
10+
# install git and bats
11+
RUN apk update \
12+
&& apk add --no-cache git \
13+
&& git clone https://github.com/bats-core/bats-core.git \
14+
&& cd bats-core \
15+
&& ./install.sh /usr/local

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# [Sqids Bash](https://sqids.org/bash)
22

3+
![GitHub Release](https://img.shields.io/github/v/release/sqids/sqids-bash)
34
[![Tests](https://github.com/sqids/sqids-bash/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/sqids/sqids-bash/actions/workflows/tests.yml)
5+
![GitHub Downloads (all assets, all releases)](https://img.shields.io/github/downloads/sqids/sqids-bash/total)
46

57
[Sqids](https://sqids.org/bash) (*pronounced "squids"*) is a small library that lets you **generate unique IDs from numbers**. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups.
68

@@ -29,12 +31,18 @@ Not good for:
2931

3032
## 🚀 Getting started
3133

34+
If your bash version is not 4.0 or higher, please upgrade your bash. (You can check with `$bash --version`.)
35+
36+
Once you have verified the version of bash, run
37+
3238
```bash
3339
git clone https://github.com/sqids/sqids-bash.git
3440
chmod +x sqids-bash/src/sqids
3541
cp sqids-bash/src/sqids /usr/local/bin
3642
```
3743

44+
You may need to add `sudo` before the command to run the commands as root.
45+
3846
## 👩‍💻 Examples
3947

4048
Simple encode & decode:

src/sqids

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ to_id() {
641641
local id_chars=()
642642

643643
while true; do
644-
id_chars=("${alphabet:$((num % ${#alphabet})):1}" "${id_chars[@]}")
644+
id_chars=("${alphabet:$((num % ${#alphabet})):1}" "${id_chars[@]+"${id_chars[@]}"}") # it can be "${id_chars[@]}" without `set -u`, workaround for bash 4.0
645645
num=$((num / ${#alphabet}))
646646
if [[ $num -eq 0 ]]; then
647647
break
@@ -978,7 +978,7 @@ EOS
978978
}
979979

980980
version() {
981-
echo "sqids-bash version 1.0.0"
981+
echo "sqids-bash version 1.0.1"
982982
exit 0
983983
}
984984

@@ -1048,10 +1048,16 @@ main() {
10481048
exit 1
10491049
fi
10501050

1051-
if [[ -n $(echo "$alphabet" | fold -s1 | sort | uniq -d) ]]; then
1052-
echo "ERROR: Alphabet cannot contain duplicate characters" >&2
1053-
exit 1
1054-
fi
1051+
declare -A alphabet_dict
1052+
splitstr "$alphabet"
1053+
for char in $__RETURN; do
1054+
if ${alphabet_dict[$char]:-false}; then
1055+
echo "ERROR: Alphabet cannot contain duplicate characters" >&2
1056+
exit 1
1057+
else
1058+
alphabet_dict[$char]=true
1059+
fi
1060+
done
10551061

10561062
MIN_LENGTH_LIMIT=255
10571063
if [[ $min_length -lt 0 || $min_length -gt $MIN_LENGTH_LIMIT ]]; then
@@ -1090,7 +1096,7 @@ main() {
10901096
case "$mode" in
10911097
"encode")
10921098
if $flag_a || $flag_b; then
1093-
encode -a "$alphabet" -b "${filtered_blocklist[*]}" -l "$min_length" "$@"
1099+
encode -a "$alphabet" -b "${filtered_blocklist[*]+"${filtered_blocklist[*]}"}" -l "$min_length" "$@" # it can be "${filtered_blocklist[*]}" without `set -u`, workaround for bash 4.0
10941100
else
10951101
encode -a "$alphabet" -b "${block_list[*]}" -l "$min_length" "$@"
10961102
fi

0 commit comments

Comments
 (0)