Skip to content

Commit

Permalink
3.0.0 (breaking): #16: refactor protected rules and configuration fil…
Browse files Browse the repository at this point in the history
…es structure
  • Loading branch information
kaelzhang committed Dec 13, 2024
1 parent d6d8a4a commit 33934e8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 60 deletions.
18 changes: 5 additions & 13 deletions sample.safe-rm.conf → .safe-rm/config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# This is a sample configuration file for safe-rm.
# By default, you could copy this file to ~/.safe-rm.conf
# By default, you could copy this file to ~/.safe-rm/config

#
# If you want to use a trash directory other than the default system trash,
Expand All @@ -28,16 +28,8 @@
# export SAFE_RM_USE_APPLESCRIPT=no

#
# If you want to protect some files or directories from being removed by
# safe-rm:
# - Uncomment the line after divider `-----`
# - Create a file at the path specified by SAFE_RM_PROTECTED_RULES, such as
# `~/.safe-rm.protected`
# - Add the rules to the file, one rule per line, the rule follows the # .gitingore syntax (https://git-scm.com/docs/gitignore). If a file or
# directory ignores by the rules, it will not be removed by safe-rm.
# ATTENTION that
# - Before enabling this feature, `git` must be installed.
# - It is unnecessary to put `/` in the protected rules. And you also should
# NOT do so, or all files will be protected.
# If you want to change the configuration root directory of safe-rm, in order to
# - put the .gitignore file in a different directory other than `~/.safe-rm/`
# Uncomment the line after divider `-----`
# -----------------------------------------------
# export SAFE_RM_PROTECTED_RULES="$HOME/.safe-rm.protected"
# export SAFE_RM_CONFIG_ROOT=/path/to/config/root
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,33 @@ sudo sh uninstall.sh

## Configuration

Since 2.0.0, you could create a configuration file named `.safe-rm.conf` in your `$HOME` directory, to support
Since 3.0.0, you could create a configuration file located at `~/.safe-rm/config` in your `$HOME` directory, to support
- defining your custom trash directory
- allowing `safe-rm` to permanently delete files and directories that are already in the trash
- disallowing `safe-rm` to use [AppleScript][applescript]

For the description of each config, you could refer to the sample file [here](./sample.safe-rm.conf)
For the description of each config, you could refer to the sample file [here](./.safe-rm/config)

```sh
# You could
cp ./sample.safe-rm.conf ~/.safe-rm.conf
cp -r ./.safe-rm ~/
```

If you want to use a custom configuration file

```sh
alias="SAFE_RM_CONF=/path/to/safe-rm.conf /path/to/shell-safe-rm/bin/rm.sh"
alias="SAFE_RM_CONFIG=/path/to/safe-rm.conf /path/to/shell-safe-rm/bin/rm.sh"
```

Or if it is installed by npm:

```sh
alias="SAFE_RM_CONF=/path/to/safe-rm.conf safe-rm"
alias="SAFE_RM_CONFIG=/path/to/safe-rm.conf safe-rm"
```

### Disable `Put-back` Functionality on MacOS (MacOS only)

In `~/.safe-rm.conf`
In `~/.safe-rm/config`

```sh
export SAFE_RM_USE_APPLESCRIPT=no
Expand All @@ -161,18 +161,11 @@ export SAFE_RM_PERM_DEL_FILES_IN_TRASH=yes

### Protect Files And Directories From Deleting

If you want to protect some certain files or directories from deleting by mistake, you could first enable the configuration in your `~/.safe-rm.conf` by uncommenting the line below:

```diff
- # export SAFE_RM_PROTECTED_RULES="$HOME/.safe-rm.protected"
+ export SAFE_RM_PROTECTED_RULES="$HOME/.safe-rm.protected"
```

Then, a `~/.safe-rm.protected` file must be created, in the file, you could write [.gitignore rules](https://git-scm.com/docs/gitignore)
If you want to protect some certain files or directories from deleting by mistake, you could create a `.gitignore` under `~/.safe-rm/` in the file, you could write [.gitignore rules](https://git-scm.com/docs/gitignore)

If a path is matched by the rules that defined in `~/.safe-rm.protected`, the path will be protected and could not be deleted by `safe-rm`

For example, in the `~/.safe-rm.protected`
For example, in the `~/.safe-rm/config`

```.gitignore
/path/to/be/protected
Expand All @@ -196,11 +189,10 @@ $ safe-rm -rf /path/to

To keep the performance of `safe-rm` and avoid conducting unnecessary file system traversing, this would not prevent `/path/to/be/protected/foo` from removing.

Something that you might also need to know:
Pay **ATTENTION** that:
- Enabling this config requires `git` to be installed in your environment
- The protected rules file applies to the root directory (/), which This means that the patterns defined within it need to be relative to the root directory.
- Avoid adding `/` in the protected rules file, or everything will be protected
- `SAFE_RM_PROTECTED_RULES` could target to any pathname you want


[applescript]: https://en.wikipedia.org/wiki/AppleScript
Expand Down
63 changes: 42 additions & 21 deletions bin/rm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
# Basic configuration
# ------------------------------------------------------------------------------

DEFAULT_SAFE_RM_CONF="$HOME/.safe-rm.conf"
DEFAULT_SAFE_RM_CONFIG="$HOME/.safe-rm/config"

# You could modify the location of the configuration file by using
# ```sh
# $ export SAFE_RM_CONF=/path/to/safe-rm.conf
# $ export SAFE_RM_CONFIG=/path/to/safe-rm.conf
# ```
SAFE_RM_CONF=${SAFE_RM_CONF:="$DEFAULT_SAFE_RM_CONF"}
SAFE_RM_CONFIG=${SAFE_RM_CONFIG:="$DEFAULT_SAFE_RM_CONFIG"}

if [[ -f "$SAFE_RM_CONF" ]]; then
source "$SAFE_RM_CONF"
if [[ -f "$SAFE_RM_CONFIG" ]]; then
source "$SAFE_RM_CONFIG"
fi

SAFE_RM_CONFIG_ROOT=${SAFE_RM_CONFIG_ROOT:="$HOME/.safe-rm"}

# Print debug info or not
SAFE_RM_DEBUG=${SAFE_RM_DEBUG:=}
Expand Down Expand Up @@ -78,7 +79,7 @@ SAFE_RM_TRASH=${SAFE_RM_TRASH:="$DEFAULT_TRASH"}

if [[ "$OS_TYPE" == "MacOS" ]]; then
if command -v osascript &> /dev/null; then
# `SAFE_RM_USE_APPLESCRIPT=no` in your SAFE_RM_CONF file
# `SAFE_RM_USE_APPLESCRIPT=no` in your SAFE_RM_CONFIG file
# to disable AppleScript
if [[ "$SAFE_RM_USE_APPLESCRIPT" == "no" ]]; then
debug "$LINENO: applescript disabled by conf"
Expand Down Expand Up @@ -111,26 +112,36 @@ else
fi


if [[ -n "$SAFE_RM_PROTECTED_RULES" ]]; then
# But if it is not a file
if [[ ! -f "$SAFE_RM_PROTECTED_RULES" ]]; then
error "Protected rules file \"$SAFE_RM_PROTECTED_RULES\" does not exist"
error " please fix your configuration in \"$SAFE_RM_CONF\""
do_exit $LINENO 1
else
debug "$LINENO: protected rules enabled: $SAFE_RM_PROTECTED_RULES"
fi
SAFE_RM_PROTECTED_RULES="${SAFE_RM_CONFIG_ROOT}/.gitignore"

debug $SAFE_RM_PROTECTED_RULES

# But if it is not a file
if [[ -f "$SAFE_RM_PROTECTED_RULES" ]]; then
if command -v git &> /dev/null; then
:
debug "$LINENO: protected rules enabled: $SAFE_RM_PROTECTED_RULES"

if git -C "$SAFE_RM_CONFIG_ROOT" rev-parse --is-inside-work-tree &> /dev/null; then
:
else
error "[WARNING] safe-rm requires a git repository to use protected rules"
error "initializing a git repository in \"$SAFE_RM_CONFIG_ROOT\" ..."

git -C "$SAFE_RM_CONFIG_ROOT" init -q

error "success"
fi
else
error "safe-rm requires git installed to use protected rules"
error " please disable protected rules in \"$SAFE_RM_CONF\""
error " or install git"
do_exit $LINENO 1
error "[WARNING] safe-rm requires git installed to use protected rules"
error " please install git"
error " or remove the file \"$SAFE_RM_PROTECTED_RULES\""
SAFE_RM_PROTECTED_RULES=
fi
else
SAFE_RM_PROTECTED_RULES=
fi


# ------------------------------------------------------------------------------

# Simple basename: /bin/rm -> rm
Expand Down Expand Up @@ -321,6 +332,14 @@ if [[ ! -e $SAFE_RM_TRASH ]]; then
fi


check_return_status(){
local status=$?
if [[ $status -ne "0" ]]; then
debug "$LINENO: last command returned status $status"
EXIT_CODE=$status
fi
}

# try to remove a file or directory
remove(){
local file=$1
Expand Down Expand Up @@ -427,6 +446,8 @@ trash(){
else
do_trash "$target"
fi

check_return_status
}


Expand Down Expand Up @@ -473,7 +494,7 @@ is_protected(){
debug "$LINENO: check whether $rel_path is protected"
cat "$SAFE_RM_PROTECTED_RULES" >&2

local ignored=$(echo "$rel_path" | git check-ignore --no-index --stdin -v --exclude-from="$SAFE_RM_PROTECTED_RULES")
local ignored=$(git -C "$SAFE_RM_CONFIG_ROOT" check-ignore --no-index -v "$rel_path")

debug "$LINENO: git check-ignore result: $ignored"

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "safe-rm",
"version": "2.0.0",
"version": "3.0.0",
"description": "A much safer replacement of bash rm with nearly full functionalities and options of the rm command!",
"bin": {
"safe-rm": "./bin/rm.sh"
Expand Down Expand Up @@ -28,7 +28,7 @@
],
"ava": {
"files": [
"test/safe-rm.test.js"
"test/*.test.js"
]
},
"author": "kael",
Expand Down
20 changes: 13 additions & 7 deletions test/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,33 @@ module.exports = (
})

// Only test for safe-rm
!is_rm(type) && test.only(`protected rules`, async t => {
!is_rm(type) && test(`protected rules`, async t => {
const {
createDir,
createFile,
runRm,
pathExists
} = t.context

const dir = await createDir()
const protected_rule_file = await createFile({
name: '.safe-rm.protected',
content: `${dir}
`
const config_root = await createDir({
name: '.safe-rm'
})

const dir = await createDir()
const filepath = await createFile({
under: dir
})

await createFile({
name: '.gitignore',
under: config_root,
content: `${dir}
`
})

const result = await runRm([filepath], {
env: {
SAFE_RM_PROTECTED_RULES: protected_rule_file
SAFE_RM_CONFIG_ROOT: config_root
}
})

Expand Down

0 comments on commit 33934e8

Please sign in to comment.