Skip to content

Commit

Permalink
Merge pull request #1 from einar-lanfranco/patch-1
Browse files Browse the repository at this point in the history
* Fixes a bug on Linux when using a wrong parameter for `stat`
* Adds ability to extend the default, single configuration directory with multiple, user-provided ones. This allows for splitting config files in a broader level of abstraction and sharing them with different people, like for instance when you're working with different teams on projects completely unrelated one to the other.
  • Loading branch information
ncuesta authored Aug 18, 2016
2 parents ba236fc + a194885 commit 68da09c
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions sshok
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,22 @@ SSH_CONFIG_DIR=${SSH_CONFIG_DIR:-$SSH_DIR/config.d}
SSH_CONFIG_FILE=${SSH_CONFIG_FILE:-$SSH_DIR/config}
BACKUPS_DIR=${BACKUPS_DIR:-/tmp/sshok}
UNWANTED_FILES_PATTERN="# vim:"
# Sometimes you need to share configuration file with multiple idependent groups, so you need multiple
# config sources, for example 2 different git projects in folders job1 and job2.
# Example SSH_CONFIG_DIRS="$HOME/job1 $HOME/job2 ${SSH_CONFIG_DIR}"
# Default
SSH_CONFIG_DIRS="${SSH_CONFIG_DIR}"


# Get the filename with the most recently mtime from the path passed in the first argument. $1 is
# expected to exist.
newest_file_in() {
if [ -e $1 ]; then
stat=$(determine_stat_variation $1)
if [ $stat = "-c" ]; then
find $1 -type f -print0 | xargs -0 stat -c '%Y %N' | sort -rn | head -1 | cut -d' ' -f2
find $SSH_CONFIG_DIRS -type f -print0 | xargs -0 stat -c '%Y %n' | sort -rn | head -1 | cut -d' ' -f2
else
find $1 -type f -print0 | xargs -0 stat -f '%m %N' | sort -rn | head -1 | cut -d' ' -f2
find $SSH_CONFIG_DIRS -type f -print0 | xargs -0 stat -f '%m %N' | sort -rn | head -1 | cut -d' ' -f2
fi
fi
}
Expand Down Expand Up @@ -85,20 +91,28 @@ backup_config() {
# passed in the first argument.
generate_config() {
echo -e "#\n# This file was autogenerated from $1 on $(date +%FT%T)\n#\n"
for file in $1/*; do
echo "# From $file"
cat $file | grep -v "$UNWANTED_FILES_PATTERN"
done
for dir in $SSH_CONFIG_DIRS; do
for file in $dir/*; do
echo "# From $file"
cat $file | grep -v "$UNWANTED_FILES_PATTERN"
done
done
}

# First, make sure the $SSH_CONFIG_DIR directory exists and if it doesn't copy the current
# $SSH_CONFIG_FILE to it. By doing that we won't overwrite the existing configuration by accident
# on the first run.
if [ ! -d $SSH_CONFIG_DIR ]; then
mkdir -p $SSH_CONFIG_DIR && \
cp $SSH_CONFIG_FILE $SSH_CONFIG_DIR/original
mkdir -p $SSH_CONFIG_DIR
if [ -f $SSH_CONFIG_FILE ]; then
cp $SSH_CONFIG_FILE $SSH_CONFIG_DIR/original
else
echo -e "#\n# This file was autogenerated from $1 on $(date +%FT%T)\n#\n" > $SSH_CONFIG_FILE
fi
fi



if [ ! -d $BACKUPS_DIR ]; then
mkdir -p $BACKUPS_DIR
fi
Expand All @@ -109,7 +123,7 @@ if [ ! -e $SSH_CONFIG_FILE ] || [ $(newest_file_in $SSH_CONFIG_DIR) -nt $SSH_CON
# Backup current config...
backup_config $SSH_CONFIG_FILE $BACKUPS_DIR
# ...and then generate the new one
generate_config $SSH_CONFIG_DIR > $SSH_CONFIG_FILE
generate_config $SSH_CONFIG_DIRS > $SSH_CONFIG_FILE
fi

# Finally, run ssh with any arguments passed to this script
Expand Down

0 comments on commit 68da09c

Please sign in to comment.