Skip to content

Commit

Permalink
Add hint command for print all hints, add generated aliases to .bash_…
Browse files Browse the repository at this point in the history
…aliases example #29
  • Loading branch information
meteoritt committed Nov 21, 2020
1 parent 3c14ada commit 94832a5
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 35 deletions.
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ source ~/.bashrc

![generated bash aliases](https://github.com/CSRedRat/topalias/raw/master/images/bash_screenshot.png "Bash topalias output")

Syntax: `topalias [OPTIONS] COMMAND [ARGUMENTS]`

Without command utility check if you use alias in ~/.bash_aliases - analyze and print usage statistics, offers to find new simple aliases
Without parameters utility check if you use alias in ~/.bash_aliases - analyze and print usage statistics, then find new simple aliases

```bash
python3 -m topalias # run as python module
Expand All @@ -87,12 +85,48 @@ topalias -f /home/user # or topalias --path /home/user

Also you can use topalias utility in [Bash for Git](https://gitforwindows.org/) on Windows and in [WSL](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux).

Add you awesome aliases to [Insights topic](https://github.com/CSRedRat/topalias/issues/29) or [.bash_aliases](https://github.com/CSRedRat/topalias/blob/master/topalias/data/.bash_aliases) example.

### Documentation

```
Usage: topalias [OPTIONS] COMMAND [ARGS]
Options:
-l, --min INTEGER Print alias acronym not less that value. Default: 1
-c, --count INTEGER Print specified number acronym suggestions. Default:
20
--filter Filter used aliases in history. Default: False
-z, --zsh Use zsh shell history file .zsh_history. Default:
False
-f, --path TEXT Change custom directory for files: .bash_aliases,
.bash_history, .zsh_history
--version Print current program version and check latest on
pypi.org.
--debug / --no-debug Enable debug strings in output.
-h, --help Show this message and exit.
Commands:
hint Print all hints.
history Print bash history file.
version Get program current and available version.
```

## TODO

- multiline command in history
- check if alias name already used
- check if alias already added
- add any another acronym algorithm with semantic
- more statistics & analytics (used dir, utils, parameters, time)
- alias max length parameter
- command ignore list flag: top, emacs, vim
- often used command "ssh username@servername" suggest add to .ssh/config/
- find password and other sensitivity data in history and suggest clean (now print "Hint: add space ' ' before sensitive command in terminal for skip save current command in history!")

Please add you feature requests: [https://github.com/CSRedRat/topalias/issues/new](https://github.com/CSRedRat/topalias/issues/new)

Expand Down
8 changes: 4 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ max-complexity = 6
max-line-length = 120
select = B,C,E,F,W,T4
i-control-code = False
ignore = D100,D104,D106,D401,W504,X100,RST303,RST304,DAR103,DAR203,E203,E266,E501,W503,WPS410,WPS305,WPS421,WPS336,WPS317,WPS111,WPS226,WPS323,WPS210,WPS429,WPS220,WPS231,C901,WPS441,WPS229,WPS232
exclude =
ignore = D100,D104,D106,D401,W504,X100,RST303,RST304,DAR103,DAR203,E203,E266,E501,W503,WPS410,WPS305,WPS421,WPS336,WPS317,WPS111,WPS226,WPS323,WPS210,WPS429,WPS220,WPS231,C901,WPS441,WPS229,WPS232,WPS202
exclude =
.git
__pycache__
.venv
Expand All @@ -34,7 +34,7 @@ exclude =
docs/conf.py
old,build
dist
per-file-ignores =
per-file-ignores =
tests/*.py: S101, WPS226, WPS432

[aliases]
Expand All @@ -43,7 +43,7 @@ test = pytest
[tool:pytest]
testpaths = tests/
norecursedirs = *.egg .eggs dist build docs .tox .git __pycache__
addopts =
addopts =
--strict
--tb=short
--doctest-modules
Expand Down
12 changes: 11 additions & 1 deletion topalias/aliascore.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Main module. Not for executing, only library. Run project from cli.py"""

import io
import logging
import os
Expand Down Expand Up @@ -158,13 +159,15 @@ def print_stat(raw_lines, filtered) -> None:
HISTTIMEFORMAT = "".join((HISTTIMEFORMAT_FIRST, HISTTIMEFORMAT_SECOND))

hint_bank = (
"Hint (secure): add space ' ' before sensitive command in terminal for skip save current command in history!",
HISTTIMEFORMAT,
"Hint: add space ' ' before sensitive command in terminal for skip save current command in history!",
"Hint: command 'sudo !!' after you forget add sudo before command in previous command",
"Hint: command !<command number in history> for repeat command from history",
"Hint: ignore command in history: echo \"export HISTIGNORE='ls -l:pwd:date:ll:ls:'\" >> ~/.bashrc",
'Hint: ignore duplicates in history: echo "export HISTCONTROL=ignoreboth" >> ~/.bashrc',
"Hint: run 'alias' command to print all used aliases",
"Hint: example aliases: https://github.com/CSRedRat/topalias/blob/master/topalias/data/.bash_aliases"
+ " (you can add their)",
)


Expand All @@ -174,6 +177,13 @@ def print_hint() -> None:
print(random.choice(hint_bank))


def print_all_hint() -> None:
"""Print all hints"""
print("\nRun after add alias: source ~/.bash_aliases")
for number, hint in enumerate(hint_bank):
print(number, hint)


def process_bash_line(line: str, filtering: bool = False):
""" Process bash history line """
if (not line.startswith("#", 0, 1)) and line != "":
Expand Down
6 changes: 6 additions & 0 deletions topalias/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ def version(ctx) -> None:
print_version(ctx, ver=True)


@cli.command()
def hint() -> None:
"""Print all hints."""
core.print_all_hint()


@cli.command(name="history")
@click.pass_context
def top_history(ctx) -> None:
Expand Down
62 changes: 36 additions & 26 deletions topalias/data/.bash_aliases
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
# default
alias psgrep="ps aux | egrep -i $1" # process search
alias hh="history | egrep -i" # search in history
alias upd="sudo apt update && sudo apt full-upgrade -y && sudo apt dist-upgrade -y" # update system to latest packages in release
alias psgrep='ps aux | egrep -i $1' # process search
alias hh='history | egrep -i' # search in history
alias upd='sudo apt update && sudo apt full-upgrade -y && sudo apt dist-upgrade -y' # update system to latest packages in release

# grc colored
alias cvs="grc --colour=auto cvs"
alias diff="grc --colour=auto diff"
alias esperanto="grc --colour=auto esperanto"
alias gcc="grc --colour=auto gcc"
alias irclog="grc --colour=auto irclog"
alias ldap="grc --colour=auto ldap"
alias log="grc --colour=auto log"
alias netstat="grc --colour=auto netstat"
alias ping="grc --colour=auto ping"
alias proftpd="grc --colour=auto proftpd"
alias traceroute="grc --colour=auto traceroute"
alias wdiff="grc --colour=auto wdiff"
alias make="grc --colour=auto make"
alias ll="grc --colour=auto ls -laFh --color=always"
alias l = "grc --colour=auto ls -lah --color=always"
alias cvs='grc --colour=auto cvs'
alias diff='grc --colour=auto diff'
alias esperanto='grc --colour=auto esperanto'
alias gcc='grc --colour=auto gcc'
alias irclog='grc --colour=auto irclog'
alias ldap='grc --colour=auto ldap'
alias log='grc --colour=auto log'
alias netstat='grc --colour=auto netstat'
alias ping='grc --colour=auto ping'
alias proftpd='grc --colour=auto proftpd'
alias traceroute='grc --colour=auto traceroute'
alias wdiff='grc --colour=auto wdiff'
alias make='grc --colour=auto make'
alias ll='grc --colour=auto ls -laFh --color=always'
alias l = 'grc --colour=auto ls -CF --color=always'

# admin tooling
alias tcp="netstat -ltupn" # list open tcp port without sudo
alias port="sudo ss -altp" # list open tcp port with process name/PID
alias tcp='netstat -ltupn' # list open tcp port without sudo
alias port='sudo ss -altp' # list open tcp port with process name/PID
alias es='eval `ssh-agent -s` && ssh-add'

# docker
alias dockupd="docker-compose stop && docker-compose rm && docker-compose pull && docker-compose up -d" # restart & update service from docker-compose.yml config in current dir
alias dcupd="dockupd" # short alias
alias dc="docker-compose"
alias dcl="docker-compose logs -f"
alias dcr="docker-compose stop && docker-compose up -d"
alias n='nvim'
alias dgrep='docker ps | egrep -i $1' # docker process search
alias dcupd='docker-compose stop && docker-compose rm && docker-compose pull && docker-compose up -d' # restart & update service from docker-compose.yml config in current dir
alias dc='docker-compose'
alias dcl='docker-compose logs -f'
alias dcr='docker-compose stop && docker-compose up -d'
alias snd='sudo nano docker-compose.yml'
alias du='docker-compose up -d'
alias ds='docker-compose stop'

# kubernetes
alias k='kubectl'
alias kgp='kubectl get pods,svc,ing --all-namespaces'

# git
alias gr='git rebase --continue'
157 changes: 157 additions & 0 deletions topalias/data/.bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi

if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'

alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alFh'
alias la='ls -Ah'
alias l='ls -CFh'

# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
[ -r /home/csredrat/.byobu/prompt ] && . /home/csredrat/.byobu/prompt #byobu-prompt#

#return value visualisation
set_prompt () {
Last_Command=$? # Must come first!
Blue='\[\e[01;34m\]'
White='\[\e[01;37m\]'
Red='\[\e[01;31m\]'
Green='\[\e[01;32m\]'
Reset='\[\e[00m\]'
FancyX='\342\234\227'
Checkmark='\342\234\223'
# Add a bright white exit status for the last command
PS1="$White\$? "
# If it was successful, print a green check mark. Otherwise, print
# a red X.
if [[ $Last_Command == 0 ]]; then
PS1+="$Green$Checkmark "
else
PS1+="$Red$FancyX "
fi
# If root, just print the host in red. Otherwise, print the current user
# and host in green.
if [[ $EUID == 0 ]]; then
PS1+="$Red\\h "
else
PS1+="$Green\\u@\\h "
fi
# Print the working directory and prompt marker in blue, and reset
# the text color to the default.
PS1+="$Blue\\w \\\$$Reset "
}
PROMPT_COMMAND='set_prompt'

export HISTTIMEFORMAT='%F %T '
export HISTIGNORE='ls -l:pwd:date:ll:ls:'
export HISTCONTROL=ignoreboth
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r;"

export KUBECONFIG=$HOME/.kube/config
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ doctests = True

# Disable some pydocstyle checks:
# Exclude some pydoctest checks globally:
ignore = D100,D104,D106,D401,W504,X100,RST303,RST304,DAR103,DAR203,E203,E266,E501,W503,WPS410,WPS305,WPS421,WPS336,WPS317,WPS111,WPS226,WPS323,WPS210,WPS429,WPS220,WPS231,C901,WPS441,WPS229,WPS232
ignore = D100,D104,D106,D401,W504,X100,RST303,RST304,DAR103,DAR203,E203,E266,E501,W503,WPS410,WPS305,WPS421,WPS336,WPS317,WPS111,WPS226,WPS323,WPS210,WPS429,WPS220,WPS231,C901,WPS441,WPS229,WPS232,WPS202

# Excluding some directories:
exclude =
Expand Down

0 comments on commit 94832a5

Please sign in to comment.