Commit 8280a9fe authored by Felix Hamann's avatar Felix Hamann

rewrote aliases

parent 9235c618
......@@ -44,13 +44,12 @@ For the server configuration use `(defvar ktz/is-server t)`.
| `e <args>` | `emacs -nw <args>` |
| `e -gui <args>` | `emacs <args>` |
| `s <args>` | `sudo fish -c "<args>"` |
| `q <host> <user>`| `ssh user@host` |
#### c - systemctl and journalctl ####
| Command | |
|-------------|-----------------------------|
| `a` | `systemctl` |
| `c` | `systemctl` |
| `c dis [0]` | `systemctl disable [0]` |
| `c dr` | `systemctl daemon-reload |
| `c en [0]` | `systemctl enable [0]` |
......@@ -67,49 +66,51 @@ For the server configuration use `(defvar ktz/is-server t)`.
#### a - apt-* shortcuts ####
| Command | |
|------------------|------------------------------|
| `a ar` | `apt autoremove` |
| `a c` | `apt-get clean` |
| `a dg` | `apt full-upgrade` |
| `a i [0]` | `apt install [0]` |
| `a pg [0]` | `apt purge [0]` |
| `a r [0]` | `apt remove [0]` |
| `a ug` | `apt upgrade` |
| `a up` | `apt update` |
| `a d [0]` | `apt-get download [0]` |
| `a s [0]` | `apt-cache search [0]` |
| `a fs [0]` | `apt-file search [0]` |
| `a sh [0]` | `apt show [0]` |
| Command | |
|------------|------------------------|
| `a` | `apt` |
| `a ar` | `apt autoremove` |
| `a c` | `apt-get clean` |
| `a dg` | `apt full-upgrade` |
| `a i [0]` | `apt install [0]` |
| `a pg [0]` | `apt purge [0]` |
| `a r [0]` | `apt remove [0]` |
| `a ug` | `apt upgrade` |
| `a up` | `apt update` |
| `a d [0]` | `apt-get download [0]` |
| `a s [0]` | `apt-cache search [0]` |
| `a fs [0]` | `apt-file search [0]` |
| `a sh [0]` | `apt show [0]` |
#### g - Git shortcuts ####
Shortcuts for `git`. `args` may be optional.
| Command | Equivalent | Description |
|----------------|-------------------------|-----------------------|
| `g a <args>` | `git add <args>` | `git help add` |
| `g b <args>` | `git branch <args>` | `git help branch` |
| `g c <args>` | `git commit <args>` | `git help commit` |
| `g f <args>` | `git fetch <args>` | `git help fetch` |
| `g cl <args>` | `git clone <args>` | `git help clone` |
| `g co <args>` | `git checkout <args>` | `git help checkout` |
| `g d <args>` | `git diff <args>` | `git help diff` |
| `g gpl <args>` | - | pull from all remotes |
| `g gps <args>` | - | push to all remotes |
| `g h <args>` | `git help <args>` | `git help` |
| `g l <args>` | `git log <args>` | `git help log` |
| `g mg <args>` | `git merge <args>` | `git help merge` |
| `g mv <args>` | `git mv <args>` | `git help mv` |
| `g ps <args>` | `git push <args> <args>`| `git help push <args>`|
| `g pl <args>` | `git pull <args>` | `git help pull` |
| `g plr <args>` | `git pull --rebase <a>` | `git help pull` |
| `g rm <args>` | `git rm <args>` | `git help rm` |
| `g rt <args>` | `git remote <args>` | `git help remote` |
| `g s <args>` | `git status <args>` | `git help status` |
| `g st <args>` | `git stash <args>` | `git help stash` |
| `g u <args>` | `git reset HEAD <args>` | unstage file |
| `g v` | `git version` | - |
| Command | Equivalent | Description |
|----------------|--------------------------|------------------------|
| `g ` | `git` | `git` |
| `g a <args>` | `git add <args>` | `git help add` |
| `g b <args>` | `git branch <args>` | `git help branch` |
| `g c <args>` | `git commit <args>` | `git help commit` |
| `g f <args>` | `git fetch <args>` | `git help fetch` |
| `g cl <args>` | `git clone <args>` | `git help clone` |
| `g co <args>` | `git checkout <args>` | `git help checkout` |
| `g d <args>` | `git diff <args>` | `git help diff` |
| `g gpl <args>` | - | pull from all remotes |
| `g gps <args>` | - | push to all remotes |
| `g h <args>` | `git help <args>` | `git help` |
| `g l <args>` | `git log <args>` | `git help log` |
| `g mg <args>` | `git merge <args>` | `git help merge` |
| `g mv <args>` | `git mv <args>` | `git help mv` |
| `g ps <args>` | `git push <args> <args>` | `git help push <args>` |
| `g pl <args>` | `git pull <args>` | `git help pull` |
| `g plr <args>` | `git pull --rebase <a>` | `git help pull` |
| `g rm <args>` | `git rm <args>` | `git help rm` |
| `g rt <args>` | `git remote <args>` | `git help remote` |
| `g s <args>` | `git status <args>` | `git help status` |
| `g st <args>` | `git stash <args>` | `git help stash` |
| `g u <args>` | `git reset HEAD <args>` | unstage file |
| `g v` | `git version` | - |
#### t - tmux shortcuts ####
......@@ -123,33 +124,6 @@ Shortcuts for `tmux`. `args` may be optional.
| `t n [<session-name>] [<args>]` | `tmux new-session <args> -s <session-name>` | Create new sessions |
### crypt.fish - Encrypt files ###
Small and rudimentary shortcut to easily encrypt and decrypt single
files. Uses openssl's 256bit aes encryption.
```
crypt -(d|e|q|z) input [output]
switches:
-d decrypt
-e encrypt
-q quiet mode
-z delete source file
-d and -e exclude each other
input: Input file
output: (Optional) output file
```
If no output file is given the program assigns the output file a name
automatically based on the operation. It adds .aes256 to the encrypted
file and removes this suffix for decrypted files.
`crypt -e foo.tar.gz` produces `foo.tar.gz.aes256` while
`crypt -d foo.tar.gz.aes256` produces `foo.tar.gz`.
## Notes ##
__Some handy commands__
......
# -*- coding: utf-8 -*-
#
# exports all alias functions that serve as
# proxy functions for ./lib/alias
#
# Aliases work by parsing the ./alias/*.alias files
# and translating them into their proper commands.
# The translated command is then executed by fish.
function __alias_head -a head
echo $head
end
set __CC ~/.config/fish/functions/lib/alias
function __alias_tail
set -l tail
if [ 1 -lt (count $argv) ]
set tail $argv[2..-1]
end
echo $tail
function __alias_resolve
echo (eval "$__CC" $argv)
end
function __alias_exec
~/.config/fish/functions/lib/alias $argv
end
#
# proxy functions
#
function a -d "Aliases for apt-*"
__alias_exec a $argv
eval (__alias_resolve $argv)
end
function c -d "Aliases for systemctl, journalctl"
__alias_exec c $argv
end
function ll --description 'Alias for ls -lAp'
ls -lhAp --group-directories-first $argv
end
function s -d "Alias for sudo"
sudo fish -c "$argv"
# APT
# --------------------
function a -d 'apt aliases'
__alias_exec a $argv
end
function t -d "Aliases for tmux"
__alias_exec t $argv
# SYSTEMD
# --------------------
function c -d 'systemd aliases'
__alias_exec c $argv
end
#
# emacs shortcut
# if you want to open a file called "-gui"
# in terminal mode, you must type
# `e -gui -nw -gui`:)
#
function e -d "Alias for emacs [-nw]"
if [ (count $argv) -gt 0 -a "$argv[1]" = "-gui" ]
emacs (__alias_tail $argv)
else
emacs -nw $argv
end
end
#
# git shortcuts
#
# GIT
# --------------------
function __g_gps
for rt in (g rt)
echo "pushing to $rt"
g ps $rt (__alias_tail $argv)
g ps $rt $argv[2..-1]
end
end
function __g_gpl
for rt in (g rt)
echo "pulling from $rt"
g pl $rt (__alias_tail $argv)
end
for rt in (g rt)
echo "pulling from $rt"
g pl $rt $argv[2..-1]
end
end
function g -d "Aliases for git"
switch (__alias_head $argv)
case gps
__g_gps $argv
case gpl
__g_gpl $argv
case '*'
__alias_exec g $argv
end
switch ($argv[1])
case gps
__g_gps $argv
case gpl
__g_gpl $argv
case '*'
__alias_exec g $argv
end
end
# TMUX
# --------------------
function t -d 'git aliases'
__alias_exec t $argv
end
# SUDO
# --------------------
function s -d "sudo prefix"
if [ (count $argv) -gt 0 ]
eval sudo (__alias_resolve $argv)
else
eval sudo
end
end
......@@ -2,22 +2,19 @@
# files used by ../lib/alias
# aliases for all things apt
#
# when using sudo, the effective
# user must have set fish set as
# the default shell and these
# dotfiles installed
ar : apt autoremove
c : apt-get clean
d [0] : apt-get download [0]
dg : apt full-upgrade
fs [0] : apt-file search [0]
i [0] : apt install [0]
l [0] : dpkg -l | grep [0]
pg [0] : apt purge [0]
r [0] : apt remove [0]
s [0] : apt-cache search [0]
sh [0] : apt show [0]
ug : apt upgrade
up : apt update
: apt
- : apt *
ar : apt autoremove
c : apt-get clean
d : apt-get download *
dg : apt full-upgrade
fs : apt-file search *
i : apt install *
l : dpkg -l | grep *
pg : apt purge *
r : apt remove *
s : apt-cache search *
sh : apt show *
ug : apt upgrade
up : apt update
......@@ -3,16 +3,17 @@
# aliases for all systemctl and journalctl
#
a : systemctl
dis [0] : systemctl disable [0]
dr : systemctl daemon-reload
en [0] : systemctl enable [0]
j [0] : journalctl -u [0] -b
jf [0] : journalctl -u [0] -b -f
m [0] : systemctl mask [0]
rl [0] : systemctl reload [0]
rs [0] : systemctl restart [0]
sa [0] : systemctl start [0]
so [0] : systemctl stop [0]
st [0] : systemctl status [0]
u [0] : systemctl unmask [0]
: systemctl
- : systemctl *
dis : systemctl disable *
dr : systemctl daemon-reload
en : systemctl enable *
j : journalctl -u * -b
jf : journalctl -u * -b -f
m : systemctl mask *
rl : systemctl reload *
rs : systemctl restart *
sa : systemctl start *
so : systemctl stop *
st : systemctl status *
u : systemctl unmask *
......@@ -3,26 +3,27 @@
# aliases for `git`
#
a [0] : git add [0]
b [0] : git branch [0]
c [0] : git commit [0]
f [0] : git fetch [0]
cl [0] : git clone [0]
co [0] : git checkout [0]
d [0] : git diff [0]
h [0] : git help [0]
l [0] : git log [0]
mg [0] : git merge [0]
mv [0] : git mv [0]
ps [0] : git push [0]
pl [0] : git pull [0]
plr [0] : git pull --rebase [0]
rm [0] : git rm [0]
rt [0] : git remote [0]
s [0] : git status [0]
st [0] : git stash [0]
u [0] : git reset HEAD [0]
v : git version
rbc : git rebase --continue
rbs : git rebase --skip
rba : git rebase --abort
: git
a : git add *
b : git branch *
c : git commit *
f : git fetch *
cl : git clone *
co : git checkout *
d : git diff *
h : git help *
l : git log *
mg : git merge *
mv : git mv *
ps : git push *
pl : git pull *
plr : git pull --rebase *
rm : git rm *
rt : git remote *
s : git status *
st : git stash *
u : git reset HEAD *
v : git version
rbc : git rebase --continue
rbs : git rebase --skip
rba : git rebase --abort
......@@ -3,6 +3,7 @@
# aliases for `tmux`
#
ls : tmux list-sessions
a [0] [1] : tmux attach-session [1] -t [0]
n [0] [1] : tmux new-session [1] -s [0]
: tmux
ls : tmux list-sessions
a : tmux attach-session -t *
n : tmux new-session -s *
#
# files used by ../lib/alias
# aliases for the openvz family
#
sp [0] : vzctl stop [0]
st [0] : vzctl start [0]
d [0] : vzctl destroy [0]
s h [0] [1] : vzctl set [0] --hostname [1] --save
s ip a [0] [1] : vzctl set [0] --ipadd [1] --save
s ip d [0] [1] : vzctl set [0] --ipdel [1] --save
s if a [0] : vzctl set [0] --netif_add eth0,,,,vmbr0 --save
function be
if [ $argv[1] = "r" ]
set -l args
if [ (count $argv) -gt 1 ]
set args $argv[2..-1]
end
if [ -x bin/rake ]
./bin/rake $args
else
bundle exec rake $args
end
else
bundle exec $argv
end
end
#
# TODO
# remove redundancy from the -d and -e code
# handle non-existant files [ ! -d $input ]
# document the different options
#
function out -a q msg
if [ $q -ne 0 ]
echo "$msg"
end
end
function is_in -a needle haystack
if echo "$needle" | grep -qE "$haystack"
return 0
end
return 1
end
function crypt \
-d "crypt -(d|e|q|z) input [output]" \
-a op input output \
set -l op (echo "\"$op\"" | tr -d "-" | xargs echo)
set -l input "$input"
set -l output "$output"
set -l q 1
if is_in "q" $op
set q 0
end
if is_in $op "de|ed"
out $q "please specify either d or e"
return 1
end
if [ -z $input ]
out $q "please specify an input file"
return 1
end
if is_in $op "d"
if [ -z $output ]
set output (echo $input | sed 's/\(.*\)\.aes256$/\1/')
end
out $q "decrypting $input -> $output"
openssl aes-256-cbc -d -salt -in $input -out $output
if [ $status -ne 0 ]
out $q "something went wrong, aborting"
return 1
end
if is_in $op "z"
out $q "removing original file $input"
rm $input
end
out $q "done, exiting"
return $status
end
if is_in $op "$e"
if [ -z $output ]
set output $input".aes256"
end
out $q "encrypting $input -> $output"
openssl aes-256-cbc -salt -in $input -out $output
if [ $status -ne 0 ]
out $q "something went wrong, aborting"
end
if is_in $op "z"
out $q "removing original file $input"
rm $input
end
out $q "done, exiting"
return $status
end
out $q "please specify either d or e"
return 1
end
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import sys
from os import path
from subprocess import call
import pathlib
from typing import List
class Rule (object):
"""
Wrapper class for rules. Saves a command template
and handles parsing, transformation and execution
of said template.
"""
@property
def cmd_template (self):
return self._cmd_template
def fail(msg: str):
print(msg)
sys.exit(2)
def _parse (self, args):
"""
The basic idea is to replace all {\d+} (positional) and all [\d+]
(optional) afterwards. All remaining arguments are concatenated
and put in to the last optional place found.
"""
cmd = self._cmd_template.split ()
args.reverse ()
# positional
mark = lambda i: '{%d}' % i
incr = 0
try:
while mark (incr) in cmd:
index = cmd.index (mark (incr))
cmd[index] = args.pop ()
incr += 1
def translate(
*argv: List[str],
caller: str = None,
alias: str = None,
cmd: str = None,
):
root = pathlib.Path(caller).parents[1]
with (root / "alias" / f"{alias}.alias").open(mode="r") as fd:
# raised by [].pop ()
except IndexError:
raise Exception ("Not enough positional arguments")
# read lines without leading/traling whitespace
g0 = (raw.strip() for raw in fd)
# remove empty lines and comments
g1 = (line for line in g0 if line and not line.startswith("#"))
# get rules and substitutes
dic = dict(tuple(s.strip() for s in sub.split(":")) for sub in g1)
# optional
incr = 0
mark = lambda i: '[%d]' % i
splice = lambda l, a, b: l[:a] + l[b+1:]
index = None
if cmd not in dic:
fail(f"unknown command '{cmd}'")
while mark (incr) in cmd:
index = cmd.index (mark (incr))
incr += 1
sub = dic[cmd]
if argv and "*" not in sub:
fail("command '{cmd}' does not accept arguments")
if len (args):
cmd[index] = args.pop ()
else:
# splice empty [\d+] from cmd
cmd = cmd[:index] + cmd[index + 1:]
return sub.replace("*", " ".join(argv))
# insert leftover args
if len(args):
if index is None:
raise Exception ("More arguments than expected")
args.reverse ()
index += 1
return cmd[:index] + args + cmd[index:]
fail(f"unknown command {cmd}")
return cmd
if __name__ == "__main__":
caller, alias, *argv = sys.argv
cmd, *argv = argv if len(argv) else ("",)
def __init__ (self, cmd_template):
self._cmd_template = cmd_template
def execute (self, args):
"""
Execute command based on the saved template.