diff options
| author | Miquel Sabaté Solà <msabate@suse.com> | 2016-05-31 12:29:41 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <msabate@suse.com> | 2016-05-31 12:29:41 +0200 |
| commit | 2cd03d15f8c8d8c29daef2efb4406825de8b0b12 (patch) | |
| tree | ffe0585ff717f38099c9468eb6cc5a878e5489e3 /bin | |
| parent | 30862ed08e26f4c1ee2c4cce8c419b8c9fbfe568 (diff) | |
| download | dotfiles-2cd03d15f8c8d8c29daef2efb4406825de8b0b12.tar.gz dotfiles-2cd03d15f8c8d8c29daef2efb4406825de8b0b12.zip | |
Added the `bin` directory and sorted out licensing
Everything is licensed under the MIT license, except what it's inside of the
`bin` directory, which is licensed under the GPLv3+ license.
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'bin')
| -rw-r--r-- | bin/db | 215 | ||||
| -rw-r--r-- | bin/examples/database.json | 27 | ||||
| -rwxr-xr-x | bin/git-fork | 64 | ||||
| -rwxr-xr-x | bin/license | 134 | ||||
| -rw-r--r-- | bin/sass | 78 | ||||
| -rw-r--r-- | bin/test/README.md | 7 | ||||
| -rw-r--r-- | bin/test/README.md.expected | 7 | ||||
| -rw-r--r-- | bin/test/main.cpp | 13 | ||||
| -rw-r--r-- | bin/test/main.cpp.expected | 13 | ||||
| -rwxr-xr-x | bin/test/test.sh | 49 | ||||
| -rwxr-xr-x | bin/toggle-keyboard-layout.sh | 27 |
11 files changed, 634 insertions, 0 deletions
@@ -0,0 +1,215 @@ +#!/usr/bin/perl -w +# Copyright (C) 2014-2016 Miquel Sabaté Solà <mikisabate@gmail.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# This script allows us to connect with psql properly for the given environment. +# +# The environments that we can use have to be specified in a JSON file. This +# JSON file has to be specified in the "$json" configurable value (by default +# is "db/database.json"). This JSON file has to specify the environments +# available and a set of values. Each environment has to specify the following +# values: +# +# * dbname: the name of the database. +# * host: the host to be used. +# * user: the user that is accessing the database. +# * password: the password for the user@host. +# +# More values can be specified, but they will be ignored by this script. You +# can find an example of this in the examples/database.json file. +# +# You can call this script with the following optional arguments: +# +# -e name +# This argument can be used to specify the environment that we want to +# connect to. If this argument has not been passed, it will take the +# value of the "$default" configurable value ("development" by default). +# Note that this argument can be used in combination with other +# arguments. +# +# -m, --migrate +# This argument can be used to migrate a set of .sql files into the +# database. The path to these files can be set with the "$migrate" +# configurable value (by default it is "db/migrate"). Keep in mind that +# migration files can have the comment "-- ignore." in the beginning of +# the file (other previous comments or blank lines will be ignored). This +# comment tells this script to ignore this migration file. +# +# -b, --backup +# Spit to the STDOUT a backup of the database. +# +# Therefore, this command has the following usage: +# +# $ db [-e name] [[-m | --migrate] | [-b | --backup]] +# + +use strict; +use File::Basename; +use Cwd 'abs_path', 'getcwd'; + + +## +# Config values. Ideally you should only modify these values to adapt this +# script to your project. +# +# NOTE: path values should *not* start and/or end with a slash. + + +# The base path that this script will use. By default it picks the current +# working directory. +my $base = abs_path(getcwd); + +# The relative path to the json file (including the name of the json file +# itself). +my $json = 'db/database.json'; + +# The default environment. This is the environment to be used when no +# environment has been given through the '-e' option. +my $default = 'development'; + +# The relative path with migration files. Migration files are plain .sql files +# that will be used with the '-m' option. +my $migrate = 'db/migrate'; + + +## +# And here starts the program itself. + + +# Show the usage string and exit. +sub usage { + print "Usage: db [-e name] [[-m | --migrate] | [-b | --backup]]\n"; + print " -e Specify an environment.\n"; + print " -m Migrate all the files from the db/migrate directory.\n"; + print " -b Backup the proper DB.\n"; + exit(1); +} + +# Check that a given value has been defined in the given hash. +sub check { + my ($ary, $value) = @_; + if (!defined($ary->{$value})) { + print "ERROR: you haven't defined the '$value' value.\n"; + exit(1); + } +} + +# Returns true if we should ignore the given migration. +sub should_ignore { + my ($path) = @_; + my $res = 0; + + if (open(MIGRATION, $path)) { + while (<MIGRATION>) { + if ($_ =~ /^--\s*ignore\.?/) { + # Ok boss, let's ignore it. + $res = 1; + last; + } elsif ($_ =~ /^\s*$/) { + # It's a blank line, next. + next; + } elsif ($_ =~ /^--/) { + # It's an SQL comment, next. + next; + } + last; + } + close(MIGRATION); + } + return $res; +} + +# Apply all the migrations if needed. +sub migrate { + my ($cmd, $path) = @_; + + print "Starting migration\n"; + opendir(DIR, $path) or die $!; + while (my $file = readdir(DIR)) { + next if ($file =~ m/^\./); + next if ($file !~ m/\.sql$/); + + if (should_ignore("$path/$file")) { + print "Ignoring migration: $file\n"; + } else { + print "Applying migration: $file\n"; + system("$cmd < $path/$file\n"); + } + } + closedir(DIR); +} + +# Parsing options. +my %opts = ('e', $default, 'm', 0, 'b', 0); +for (my $it = 0; $it < @ARGV; $it++) { + if ($ARGV[$it] eq '-e') { + usage() if (!$ARGV[$it + 1]); + $opts{e} = $ARGV[$it + 1]; + $it++; + } elsif ($ARGV[$it] eq '-m' || $ARGV[$it] eq '--migrate') { + $opts{m} = 1; + } elsif ($ARGV[$it] eq '-b' || $ARGV[$it] eq '--backup') { + $opts{b} = 1; + } else { + if ($ARGV[$it] ne '-h' && $ARGV[$it] ne '--help') { + print "Unknown option `$ARGV[$it]'\n\n"; + } + usage(); + } +} + +# Let's fetch the configuration of the environment. +my $config = "$base/$json"; +if (!-f $config) { + print "ERROR: the $config file does not exist!\n"; + exit(1) +} + +open(FILE, $config); +my $file = ''; +while (<FILE>) { + my $line = $_; + chomp($line); + $file .= " $line"; +} +close(FILE); + +# Fetch the values. +$file =~ m/"$opts{e}":\s?({(.*?)})/; +my $contents = $1; +if (!defined($contents)) { + print "The `$opts{e}' environment does not exist.\n"; + exit(1); +} + +# Setup the command. +my %ary = (); +$ary{$1} = $2 while ($contents =~ /"(\w+)":\s?"([\w\-]*)"/g); +foreach (qw{dbname user host password}) { + check(\%ary, $_); +} + +my $opts = "-d $ary{'dbname'} -U $ary{'user'} -h $ary{'host'}"; +my $cmd = "PGPASSWORD=$ary{'password'} psql $opts"; + +# Now execute the command properly. +if ($opts{m}) { + migrate($cmd, "$base/$migrate"); +} elsif ($opts{b}) { + my $opts = "-d $ary{'dbname'} -U $ary{'user'}"; + system("PGPASSWORD=$ary{'password'} pg_dump $opts"); +} else { + system("$cmd"); +} diff --git a/bin/examples/database.json b/bin/examples/database.json new file mode 100644 index 0000000..b58e56f --- /dev/null +++ b/bin/examples/database.json @@ -0,0 +1,27 @@ +{ + "production": { + "dbname": "productiondb", + "host": "localhost", + "user": "mssola", + "password": "1234" + }, + "development": { + "dbname": "devdb", + "host": "localhost", + "user": "mssola", + "password": "1234", + "sslmode": "disable" + }, + "staging": { + "dbname": "stagingdb", + "host": "localhost", + "user": "mssola", + "password": "1234" + }, + "test": { + "dbname": "testdb", + "host": "localhost", + "user": "mssola", + "password": "1234" + } +} diff --git a/bin/git-fork b/bin/git-fork new file mode 100755 index 0000000..a03b7a9 --- /dev/null +++ b/bin/git-fork @@ -0,0 +1,64 @@ +#!/bin/bash +# Copyright (C) 2015-2016 Miquel Sabaté Solà <mikisabate@gmail.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +set -e + +# This script adds the `fork` subcommand to git. This subcommand helps to keep +# a forked project updated with upstream. So, in a forked repository you would +# do: +# +# $ git fork +# +# The previous command will fetch the upstream remote and merge the master +# branch. You can change the remote and the branch to be picked like this: +# +# $ git fork branch +# $ git fork remote/branch +# +# Finally, if the given remote does not exist, the user will be prompted to +# provide the URL to the remote, so it can be created. + +# Configurable values: the name of the branch to be picked and the name of the +# upstream remote. +branch='master' +upstream='upstream' + +# You can modify the defaults by specifying either "branch" or "remote/branch". +if [ ! -z $1 ]; then + ifs=$IFS + IFS='/' + + read -ra results <<< "$1" + if [ ${#results[@]} -eq 2 ]; then + upstream=${results[0]} + branch=${results[1]} + else + branch=${results[0]} + fi + + IFS=$ifs +fi + +# Check whether the given upstream remote really exists. If it doesn't it will +# ask the user for a remote URL. +if [[ $(git remote show | xargs -0 | grep -Fxq "$upstream") -eq 1 ]]; then + echo "The given upstream '$upstream' remote does not exist" + echo -n "Provide one: " + read -ra url + git remote add $upstream $url +fi + +git fetch $upstream && git merge $upstream/$branch diff --git a/bin/license b/bin/license new file mode 100755 index 0000000..30e13a7 --- /dev/null +++ b/bin/license @@ -0,0 +1,134 @@ +#!/usr/bin/perl -w +# Copyright (C) 2014-2016 Miquel Sabaté Solà <mikisabate@gmail.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Yay! A new year! Oops, wait, now I have to update the dates from all my stuff +# right? Well, this script solves this very specific case. It basically updates +# all the copyright notices so they match the new year. So, for example +# (assuming that the current year is 2015): +# +# Copyright (c) John Smith -> Copyright (c) 2015 John Smith +# Copyright (c) 2014 John Smith -> Copyright (c) 2014-2015 John Smith +# Copyright (c) 2009-2014 John Smith -> Copyright (c) 2009-2015 John Smith +# +# The date to be picked is set in the `$current_year` configurable value. This +# script will apply this to all the files as given as command line arguments. +# So, for example: +# +# $ ls | xargs license +# +# The line above will update the license notice for all the files in the +# current directory. By default, if you don't pass any arguments, it will do +# the following: +# +# $ git grep -l 'Copyright' | xargs license +# +# This default behavior can be changed through the `$default_cmd` configurable +# value. +# +# Some notes: +# +# - This script will respect the (c) symbol, accepting the following formats: +# (c), (C) and © . More on the configurable value `$re`. +# - This script will leave intact anything that comes before the (c) symbol +# and anything that comes after the range of years. +# - This script needs an author, because we don't want to modify the ownership +# from other people. More on the configurable value `$name`. +# - Don't blindly trust this guy, since it might be quite destructive if +# something goes wrong. So I would check the results (e.g. `git status` and +# then `git diff`). + +use strict; + +## +# Config values. You will have to update for sure the `$name` (unless you're +# also called Miquel :P) and the `$current_year`. + +# The author name. We don't want to mess with copyright notices from other +# people. Note that just setting the name might be a bit weak, so it might be +# interesting to add last names, email, etc. (that is, anything that you +# usually put after the range of years). +my $name = 'Miquel'; + +# The current year. I previously thought to be clever and set it to `date +%Y`, +# but then I decided that I didn't want to be clever here to avoid problems and +# be more flexible. +my $current_year = '2015'; + +# The command that feeds this command if no command line argument was given. +my $default_cmd = "git grep -l 'Copyright' | xargs"; + +# The regular expression that matches copyright notices. I wouldn't change it, +# because if you do, you probably would have to change the `update_file` +# subroutine. +my $re = qr/ + ^(.*) # Save all the previous stuff. + Copyright # Our regexp starts with the `Copyright` word. + (\ \([Cc]\)|\ ©)? # An optional (c) or markdown symbol. + (\ (\d{4})(-\d{4})?)? # An optional range of dates. + \ $name # The author name. This word confirms the match. + (.*)$ # Save the rest of the line. +/x; + + +sub update_file +{ + my ($file) = @_; + + open my $in, '<', $file or die "Can't open file $file"; + my $perm = (stat $in)[2] & 07777; + open my $out, '>', "$file.new" or die "Can't open file $file.new"; + + while (<$in>) { + my $line = $_; + if ($line =~ $re) { + # Get the values from the regular expression safely. + my $prev = ($1) ? $1 : ''; + my $year = ($3) ? $3 : $current_year; + my $c = ($2) ? $2 : '(C)'; + my $next = ($6) ? $6 : ''; + $c =~ s/^\s//; + $year =~ s/^\s//; + + # Get the range of years straight. + my @years = split('-', $year); + if ($years[0] ne $current_year) { + $year = "$years[0]-$current_year"; + } + + # Behold! Our majestic line! + $line = "${prev}Copyright $c $year $name${next}\n"; + } + print $out "$line"; + } + close($in); + close($out); + + # And move the file while preserving the old permission bits. + `mv $file.new $file`; + open $in, '<', $file or die "Can't open file $file"; + chmod($perm | 0600, $in); + close($in); +} + + +## +# Main: get the files from the command line and update them. + +my @files = (@ARGV > 0) ? @ARGV : split(' ', `$default_cmd`); +foreach my $f (@files) { + update_file($f); +} + diff --git a/bin/sass b/bin/sass new file mode 100644 index 0000000..8e3fc10 --- /dev/null +++ b/bin/sass @@ -0,0 +1,78 @@ +#!/bin/sh +# Copyright (C) 2014-2016 Miquel Sabaté Solà <mikisabate@gmail.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Launch a background job that deals with SASS/SCSS. +# +# If no arguments were given, then it will try to kill a previous instance +# of the background job and launch another one. You can prevent the +# re-launching by passing the "stop" argument. This way, this script will +# efectively kill the currently running process and quit. Any other argument +# will be ignored. +# +# Therefore, the usage of this script is as follows: +# +# $ sass [stop] +# + + +## +# Config values. +# +# NOTE: path values should *not* start and/or end with a slash. + +# The base directory. +dir="$( cd "$( dirname "$0" )" && pwd )/.." + +# The absolute path to the directory where we can store temporary files. You +# don't really want to use this default since it can easily clash with other +# projects ;) +tmp="/tmp" + +# The absolute path to the file that will store the PID of the currently +# running background job. +pidfile="$tmp/assets.pid" + +# The absolute path to the SASS log. +csslog="$tmp/sass.log" + +# The absolute path of the directory containing the SASS files. +cssdir="$dir/public/stylesheets" + + +## +# And here starts the script itself. + + +# Get pidfile. +mkdir -p $tmp +touch $pidfile + +# Kill off old processes +while read pid; do + kill $pid > /dev/null 2>&1; +done < $pidfile + +# Truncate the PID file +> $pidfile + +# If the user just wanted to stop all old processes, then exit here. +if [[ $1 == "stop" ]]; then + exit 1 +fi + +# SASS +sass --watch "$cssdir" > $csslog 2>&1 & +echo $! >> $pidfile diff --git a/bin/test/README.md b/bin/test/README.md new file mode 100644 index 0000000..9defd7f --- /dev/null +++ b/bin/test/README.md @@ -0,0 +1,7 @@ + +# Lalala + +A readme file + +Copyright © 2014-2015 Miquel Sabaté Solà, released under the MIT License. + diff --git a/bin/test/README.md.expected b/bin/test/README.md.expected new file mode 100644 index 0000000..9defd7f --- /dev/null +++ b/bin/test/README.md.expected @@ -0,0 +1,7 @@ + +# Lalala + +A readme file + +Copyright © 2014-2015 Miquel Sabaté Solà, released under the MIT License. + diff --git a/bin/test/main.cpp b/bin/test/main.cpp new file mode 100644 index 0000000..27c570c --- /dev/null +++ b/bin/test/main.cpp @@ -0,0 +1,13 @@ +/* + * Copyright (C) 2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (c) 2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (C) 2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (C) 2013-2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (C) 2014-2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (C) 2015 Miquel Sabaté Solà <mikisabate@gmail.com> + */ + +int main(int argc, char *argv[]) +{ +} + diff --git a/bin/test/main.cpp.expected b/bin/test/main.cpp.expected new file mode 100644 index 0000000..27c570c --- /dev/null +++ b/bin/test/main.cpp.expected @@ -0,0 +1,13 @@ +/* + * Copyright (C) 2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (c) 2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (C) 2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (C) 2013-2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (C) 2014-2015 Miquel Sabaté Solà <mikisabate@gmail.com> + * Copyright (C) 2015 Miquel Sabaté Solà <mikisabate@gmail.com> + */ + +int main(int argc, char *argv[]) +{ +} + diff --git a/bin/test/test.sh b/bin/test/test.sh new file mode 100755 index 0000000..58a8530 --- /dev/null +++ b/bin/test/test.sh @@ -0,0 +1,49 @@ +#!/bin/sh +# Copyright (C) 2014-2016 Miquel Sabaté Solà <mikisabate@gmail.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Back up the current files and get paths straight. +dir="$( cd "$( dirname "$0" )" && pwd )" +license=$(realpath "$dir/../license") +cp "$dir/main.cpp" "$dir/main.cpp.old" +cp "$dir/README.md" "$dir/README.md.old" +status=0 + +# license main.cpp +$license $dir/main.cpp +d=$(diff "$dir/main.cpp" "$dir/main.cpp.expected") +if [ ! -z "$d" ]; then + echo "$d" + status=1 +fi + +# license README.md +$license $dir/README.md +d=$(diff "$dir/README.md" "$dir/README.md.expected") +if [ ! -z "$d" ]; then + echo "$d" + status=1 +fi + +# Tear down +mv "$dir/main.cpp.old" "$dir/main.cpp" +mv "$dir/README.md.old" "$dir/README.md" +if [ "$status" -eq "1" ]; then + echo "FAIL!" +else + echo "OK" +fi +exit $status + diff --git a/bin/toggle-keyboard-layout.sh b/bin/toggle-keyboard-layout.sh new file mode 100755 index 0000000..7606ba9 --- /dev/null +++ b/bin/toggle-keyboard-layout.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# Copyright (C) 2016 Miquel Sabaté Solà <mikisabate@gmail.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Idea taken from: https://github.com/ereslibre/dotfiles. All the credit goes +# to him. + +layout=$(setxkbmap -query | awk '/layout/{print $2}') +if [ $layout == 'us' ] +then + setxkbmap es +else + setxkbmap us +fi +killall -SIGUSR1 i3status |
