Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ node_modules/
/tests/playwright/.cache/
/.idea/
.DS_Store
/scripts/aliases

1 change: 1 addition & 0 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ EOT
fi

./scripts/update-hosts
./scripts/create-aliases.sh

if [[ $(uname -m) == 'arm64' ]]; then
echo "Setting custom containers for arm platform"
Expand Down
89 changes: 89 additions & 0 deletions docs/tools/aliases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Aliases for simpler development under Linux

To simplify the development and managment of the containers and various scripts, it is possible to define aliases for convinience.

The aliases depend on your local development setup.
Thus, there is a script in this repo that help with creating an aliases file.
To enable the aliases file, you have to include it in your shell (see below).

If installed in your users bash, the aliases are present in every folder.
You do not have to navigate to the development folder but can directly call the aliases.

## Aliases defined in the aliases file

The following aliases are defined by default:

### nc-docker - Access the docker compose

The `nc-docker` alias allows you to quickly issue commands to the Docker daemon (from the development environment).
For example, you can do the followin to start a container:

```
nc-docker up -d nextcloud
```

In fact the interface is the same as `docker compose` (or `docker-compose`).
So, any valid docker compose command can be used here.

### nc-cd - Switch to folders of containers

With `nc-cd` you can directly switch to the development environment from any location in your machine.
Without any parameter, it brings you to the `wordspace` folder of the development environment.
You can give it a relative path (like `nextcloud/apps-extra/fancy`) to get there as well.

### nc-occ - Call OCC quickly

It is convinient to call OCC of the developemnt environment.
This can be done with `nc-occ`.
Note however, that you have to provide the name of the instance unless you want to affect the `nextcloud` container.
To select the instance, it is the first parameter to `nc-occ`.
See also the `scripts/occ.sh` script.

You can use the `--help` command line parameter for more details.

### nc-mysql - Direct access to the MariaDB server

The alias `nc-mysql` is an alias to `scripts/mysql.sh`.
It allows you interact with the DB in a live session.

For more details, see the output of `nc-mysql --help`.

## Automatic alias file generation

By default, the aliases file is located in `scripts/aliases` in this repository.
The file is not checked in or exists by default and must be created.
To simplify this, you can use the script `scripts/create-aliases.sh`.

Please note, however, that the `bootstrap.sh` script already calls the `scripts/create-aliases.sh`.
After bootstrapping (as suggested by the README) the aliases file therefore is automatically generated with the default settings.

You can simply call the script in your shell.
This will create the aliases file for in the default location.
Note, that this script will (for security reasons) not install the alias file in your system.

To use/install the file, you have to make your bash to read (_source_) the alias file.
Typically, this is done by adding a section to your `.bashrc` in the home folder of your main development user.
The `create-aliases.sh` script outputs the section that could be inserted into your `.bashrc` file.

For example, such an entry could be

```shell
# NC docker aliases
if [ -f "/home/USER/Documents/nextcloud-docker-dev/scripts/aliases" ]
then
source "/home/USER/Documents/nextcloud-docker-dev/scripts/aliases"
fi
```

If you want to get rid of the aliases, you have to remove the entry again from the `.bashrc` file and restart the bash or terminal.

## Modifications of the aliases

By default, the implementation automatically checks if the aliases file is still up-to-date.
If there are changes to be made (as new aliases have been defined), your bash will issue a message upon starting a new shell.

In case you want to customize the aliases, this check will bail out and trigger this warning in each shell invocation.
To avoid this, you can disable the automatic update checks of the aliases file:
On creating the aliases file, you can call `create-aliases.sh --no-selftest`.
The `--no-selftest` parameter build a version of the alias file that will skip the selftest.
Note, however, that this implies that you are responsible to keep the file up-to-date.
156 changes: 156 additions & 0 deletions scripts/create-aliases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/bin/bash

set -e

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)

# localPath="${SCRIPT_DIR}/.."
basePath="${SCRIPT_DIR}/.."
aliasesFile="${SCRIPT_DIR}/aliases"

# Configuration from CLI -- here comes the defaults
selftest_alias_update=y
command=create

get_alias_content () {

if [ "$selftest_alias_update" = "y" ]
then
cat <<EOF
# Test if aliases file is up to date
if [ ! -f "$aliasesFile" ]
then
echo "The file $aliasesFile does not exist."
echo "This is strange as I (the apparent alias script) am running at the moment."
echo "Please check your setup and verify the correct alias file is configured."
fi

"${SCRIPT_DIR}/create-aliases.sh" --check-alias-file

EOF
fi

cat <<EOF
nc-docker() {
local DCC=\$(
source "${SCRIPT_DIR}/../.env"
source "${SCRIPT_DIR}/functions.sh"
echo "\$(get_docker_compose_command)"
)

if [ -z "\$DCC" ]
then
echo "❌ Install docker-compose before running this script"
return
fi

(cd "$basePath" && \$DCC "\$@")
}

nc-occ () {
"${SCRIPT_DIR}/occ.sh" "\$@"
}

nc-mysql () {
"${SCRIPT_DIR}/mysql.sh" "\$@"
}

nc-cd () {
if [ \$# -eq 1 ]
then
cd "$basePath/workspace/\$1"
else
cd "$basePath/workspace"
fi
}
EOF
}

check_alias_content () {
if ! diff "$aliasesFile" <(get_alias_content) > /dev/null
then
echo "The aliases file $aliasesFile of the NC docker development environment is not up to date."
echo "Please upgrade using ${SCRIPT_DIR}/create-aliases.sh"
fi
}

update_alias_file () {
echo "Writign new aliases file to ${aliasesFile}"
get_alias_content > "$aliasesFile"
cat <<EOF
The file $aliasesFile has been created.

You can now source this file in your shell:
source "$aliasesFile"
Then, the aliases will be available in your shell.

You can as well put this source command in your .bashrc. That way, the aliases are available in all shells.
To do so, the following snippet can be appended to your .bashrc:

# NC docker aliases
if [ -f "$aliasesFile" ]
then
source "$aliasesFile"
fi
EOF
}

print_help() {
cat <<EOF
Create aliases for the NC docker development environment.

Usage:
${SCRIPT_DIR}/create-aliases.sh [OPTIONS]

Options:
--no-selftest
Do not check if the aliases file is up to date while evaluating the aliases.
If you want to customize the aliases, this prevents the alias self-test from throwing errors at you.
If you want to change this setting, you have to recreate the aliases file.
--stdout
Print the aliases content to stdout instead of creating the aliases file.
--check-alias-file
Check if the aliases file is up to date.
This is only needed internally and should not be used by end users.
--help | -h
Print this help.
EOF
}

while [ $# -gt 0 ]
do
case "$1" in
-h|--help)
print_help
exit 0
;;
--check-alias-file)
command=check
;;
--no-selftest)
selftest_alias_update=n
;;
--stdout)
command=stdout
;;
*)
echo "Parameter $1 is not recognized."
echo "Cannot create aliases file."
exit 1
;;
esac
shift
done

case "$command" in
check)
check_alias_content
exit 0
;;
create)
update_alias_file
;;
stdout)
get_alias_content
;;
esac
Loading