Skip to content

Commit

Permalink
Add command to create a symbolic link
Browse files Browse the repository at this point in the history
  • Loading branch information
norm committed Sep 12, 2021
1 parent 3e2a7aa commit 5d2ab78
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions documentation/kitfile.markdown
Expand Up @@ -112,3 +112,7 @@ Available commands are:
immediately. Useful for showing manual actions needed (eg. allowing an
application to use Accessibility features) without them being buried
among the entire output of a run.

* symlink _SOURCE_ _DESTINATION_

Creates a symbolic link at _DESTINATION_ that points to _SOURCE_.
14 changes: 14 additions & 0 deletions kitout.sh
Expand Up @@ -133,6 +133,7 @@ function process_kitfile {
clone) clone_repository $argument ;;
brewfile) brewfile "$argument" ;;
install) install_file $argument ;;
symlink) symlink $argument ;;
remind) remind "$argument" ;;

cron_entry) add_to_crontab "$argument" ;;
Expand Down Expand Up @@ -255,6 +256,19 @@ function install_file {
fi
}

function symlink {
local source="$1"
local target="$2"

action "symbolic linking '$target' to '$source'"
if [ -e "$target" -a ! -L "$target" ]; then
error "cannot create symlink: '$target' already exists"
else
rm -f "$target"
ln -s "$source" "$target"
fi
}

function add_to_crontab {
local line="$*"
local tab="$(mktemp '/tmp/kitout.crontab.XXXXX')"
Expand Down
32 changes: 32 additions & 0 deletions tests/symlink.bats
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

bold=$'\e'[1m
yellow=$'\e'[33m
magenta=$'\e'[35m
reset=$'\e'[0m

@test symlink {
[ ! -f /tmp/symlink.kitfile ]
[ ! -f /tmp/exists ]

run ./kitout.sh tests/symlink.kitfile
echo "$output"

[ $status -eq 1 ]

[ -f /tmp/symlink.kitfile ]
[ -L /tmp/symlink-to-symlink.kitfile ]
[ "${lines[2]}" == "${yellow}=== symbolic linking '/tmp/symlink-to-symlink.kitfile' to '/tmp/symlink.kitfile'${reset}" ]

[ -e /tmp/exists -a ! -L /tmp/exists ]
[ "${lines[3]}" == "${yellow}=== symbolic linking '/tmp/exists' to '/tmp/symlink.kitfile'${reset}" ]
[ "${lines[4]}" == "${bold}${magenta}*** cannot create symlink: '/tmp/exists' already exists${reset}" ]
}

function setup_file {
teardown
}

function teardown {
rm -f /tmp/symlink.kitfile /tmp/exists
}
9 changes: 9 additions & 0 deletions tests/symlink.kitfile
@@ -0,0 +1,9 @@
# make sure file exists
install tests/symlink.kitfile /tmp/symlink.kitfile
install tests/symlink.kitfile /tmp/exists

# link to it
symlink /tmp/symlink.kitfile /tmp/symlink-to-symlink.kitfile

# should not work, cannot replace files
symlink /tmp/symlink.kitfile /tmp/exists

0 comments on commit 5d2ab78

Please sign in to comment.