diff --git a/bin/.bin/project b/bin/.bin/project deleted file mode 100755 index 72177c58..00000000 --- a/bin/.bin/project +++ /dev/null @@ -1,256 +0,0 @@ -#!/usr/bin/env bash - -# This script sets things based on my current project and will set the symlink ~/Projects/current to the correct folder - -# GLOBAL VARIABLES -PROJECTS_PATH="$HOME/Projects" -SYMLINC="$PROJECTS_PATH/current" - -# Prints help text -function print_help(){ - echo "Manages projects" - echo "" - echo -e "Usage: $0 []" - echo "" - echo -e "Commands" - echo -e "--------" - echo -e "list\t\t\tLists The available projects" - echo -e "switch\t\t\tSwitches the current project" - echo -e "\t--auto\t\tAutomatically deduce the project based on current working directory" - echo -e "\t--fzf\t\tUses fzf to choose a project" - echo -e "\t--dmenu\t\tUses dmenu to choose a project" - echo -e "\t--rofi\t\tUses rofi to choose a project" - echo -e "\t--unset\t\tUnsets the project" - echo -e "\t\tSets the project" - echo -e "current\t\t\tPrints the current project" - echo -e "\t--notify\t\tUses notify send to print current project" - echo -e "\t--path\t\tPrints the full path rather than the name" - echo -e "new\t\t\tCreates a new project. Will prompt for a name if no argements are given" - echo -e "\t\t\tCreates a new project with the name given" - echo -e "\t--dmenu\t\tPrompt using dmenu" - echo -e "\t--rofi\t\tPrompt using rofi" - echo -e "copy [..]\t\tCopies files to a target in the project" - echo -e "\t--bin\t\tCopy to bin directory (inside \$PATH)" - echo -e "\t--www\t\tCopy to the www directory" - - exit 0 -} - -# Lists the available projects -# If the --options flag is given, it will also list --new and, if relevant, --unset. -# This is useful foa piping it into a fuzzy finder -function list_projects(){ - if [[ "$1" == "--options" ]]; then - if [ -L "$SYMLINC" ]; then - echo "--unset" - fi - echo "--new" - fi - find "$PROJECTS_PATH/" -maxdepth 1 -mindepth 1 -type d | xargs -L 1 basename -} - -# Switches the project -# Normally takes the first argement to be the project name -# If first argument is --auto, it will try to set the project based on the current working directory -# If the first argument it --unset, it will unstet the project -# If the first argument is --fzf, --dmenu or --rofi it will use the specified tool to list options -function switch(){ - local switchto="$1" - if [[ "$1" == "--auto" ]]; then - if echo "$PWD" | grep -q "$PROJECTS_PATH"; then - switchto=$( echo "$PWD" | sed "s#$PROJECTS_PATH/##") - switchto="${switchto%/*}" - if [[ "$PWD" == "$PROJECTS_PATH" ]]; then - switchto="--unset" - fi - else - switchto="--unset" - fi - elif [[ "$1" == "--fzf" ]]; then - switchto=$(list_projects --options | fzf) - elif [[ "$1" == "--dmenu" ]]; then - switchto=$(list_projects --options | dmenu -i ) - elif [[ "$1" == "--rofi" ]]; then - switchto=$(list_projects --options | rofi -dmenu -i ) - elif [[ "$1" == "--unset" ]]; then - if [ -L "$SYMLINC" ]; then - rm "$SYMLINC" 2> /dev/null - # See note about SIGWINCH below - pkill -u $USER -SIGWINCH zsh - fi - exit 0 - fi - # If $switchto is --unset, it has just been selected from one of the menus - # Re-run switch with the --unset flag - if [[ "$switchto" == "--unset" ]]; then - switch --unset - exit 0 - fi - if [[ "$switchto" == "--new" ]]; then - new_project "$1" - exit 0 - fi - switchto="$PROJECTS_PATH/$switchto" - # This sends the SIGWINCH signal to all zsh processes - # This tells zsh that the window has been resized. - # Althouph we are not resizing the window, it causes zsh to re-draw the prompt - pkill -u $USER -SIGWINCH zsh - if [ -d "$switchto" ]; then - if [ -L "$SYMLINC" ]; then - rm "$SYMLINC" 2> /dev/null - fi - ln -s "$switchto" "$SYMLINC" - else - echo "No such directory $switchto" - exit 1 - fi -} - -# Prints the name of the current project -# If there is an index.yaml file containing a name, it will print the name -# If not, it will fall back to the folder name -# If --notify is th first argument, it will use notify-send to print the project name -# If --path is the first argument, it will print the path to the current project rather than the name -# It will return an empty string if there isn't an active project -function get_current(){ - if [ -L "$SYMLINC" ]; then - local current=$(readlink "$SYMLINC") - if [[ "$1" != "--path" ]]; then - if [ -f "$current"/index.yaml ]; then - current=$(cat "$current/index.yaml" | yq -r '.name') - else - current=$(echo "$current" | xargs -L 1 basename) - fi - fi - if [[ "$1" == "--notify" ]]; then - notify-send "Current Project" "$current" - else - echo "$current" - fi - - else - if [[ "$1" == "--notify" ]]; then - notify-send "Current Project" "not set" - else - echo "" - fi - fi -} - -# Creates a new project -# The first arguemnt is normally the project name -# If there is no argument, it will prompt for the username -# If the first argument is --rofi or --dmenu, it will prompt for the name using that tool -# It will also create a few directories that most projects will need -function new_project(){ - local name="$1" - local default_dirs=( bin screenshots Downloads www ) - if [ ! -n "$name" ]; then - echo -n "Project name: " - read name < /dev/tty - elif [[ "$name" == "--rofi" ]]; then - name=$(rofi -dmenu -l 0 -p "Project name" ) - elif [[ "$name" == "--dmenu" ]]; then - name=$(echo "" | dmenu -l 0 -p "Project name: " ) - fi - # If name still empty, exit - if [ ! -n "$name" ]; then - exit 1 - fi - mkdir "$PROJECTS_PATH/$name" - # Make all the directories - for dir in "${default_dirs[@]}"; do - mkdir "$PROJECTS_PATH/$name/$dir" - done - switch "$name" -} - -# A wrapper around cp for copying to places inside the current directory -# The first argument should be one of the followind: -# --bin The bin directory inside the project - This is in your path if thre is a project set -# --www This www directory which is where http servers are served from -# It will copy all folders / files given as arguments to the location - -function copyto(){ - local current="$(get_current --path)" - if [ -n "$current" ]; then - local destination="$current/" - if [[ "$1" == "--bin" ]]; then - destination="$current/bin/" - shift - elif [[ "$1" == "--www" ]]; then - destination="$current/www/" - shift - fi - if [[ -n "$destination" ]]; then - mkdir -p "$destination" - cp -r "$@" "$destination" - fi - fi -} - -# Sets up a python server in the www directory -# First optional argement is the port to list on -# Second optionl argument is the bind address to listen to -function serve(){ - local current="$(get_current --path)" - if [ ! -n "$current" ]; then - echo "Set current project first" - exit 1 - fi - local www="$current/www/" - local port=${1:-"8000"} - local bind="$2" - if [ -d "$www" ]; then - echo "Creating www directory" - fi - local count=$( find "$www" -type f | wc -l) - if [ -n "$bind" ]; then - echo "Serving $count files to $bind from the $www directory" - python -m http.server "$port" --bind "$bind" --directory "$www" - else - echo "Serving $count files from the $www directory" - python -m http.server "$port" --directory "$www" - fi -} - -if [ -n "$1" ] ; then - case "$1" in - -h|--help) - print_help - exit 0 - ;; - list) - list_projects - exit 0 - ;; - switch) - switch "$2" - exit 0 - ;; - current) - get_current "$2" - exit 0 - ;; - new) - new_project "$2" - exit 0 - ;; - copy) - shift - copyto "$@" - exit 0 - ;; - serve) - shift - serve "$@" - exit 0 - ;; - *) - echo "Unknown command $1" - print_help - exit 1 - ;; - esac -fi - diff --git a/bin/.bin/project-management/project b/bin/.bin/project-management/project new file mode 100755 index 00000000..ce7f8d59 --- /dev/null +++ b/bin/.bin/project-management/project @@ -0,0 +1,142 @@ +#!/usr/bin/env bash + +# This script sets things based on my current project and will set the symlink ~/Projects/current to the correct folder + +# GLOBAL VARIABLES +export PROJECTS_PATH="$HOME/Projects" +export SYMLINC="$PROJECTS_PATH/current" + +function print_subcommand_details(){ + "$1" --parent-help +} + +# Prints help text +function print_help(){ + echo "Manages projects" + echo "" + echo -e "Usage: $0 []" + echo "" + echo -e "Commands" + echo -e "--------" + + echo $PATH | tr ':' '\n' | while read line; do + find "$line" -maxdepth 1 -type f -executable -name project-\* -exec basename {} \; 2> /dev/null + done | sort | uniq | while read line; do + print_subcommand_details "$line" + done | column -t -s" " + + #echo -e "copy [..]\t\tCopies files to a target in the project" + #echo -e "\t--bin\t\tCopy to bin directory (inside \$PATH)" + #echo -e "\t--www\t\tCopy to the www directory" + + exit 0 +} + +# Manipulates hosts for the project +function hosts(){ + local current="$(project current --path)" + index="$current/index.yaml" + if [ -n "$1" ] ; then + case "$1" in + ip) + local ip + case "$2" in + --fzf) + ip=$(hosts list | fzf --no-preview | cut -d',' -f2) + ;; + --dmenu) + ip=$(hosts list | dmenu | cut -d',' -f2) + echo "$ip" | xclip -selection primary + echo "$ip" | xclip -selection clipboard + ;; + --rofi) + ip=$(hosts list | rofi -dmenu | cut -d',' -f2) + echo "$ip" | xclip -selection primary + echo "$ip" | xclip -selection clipboard + ;; + esac + echo "$ip" + ;; + add) + echo -n "Name: " + read name < /dev/tty + echo -n "IP: " + read ip < /dev/tty + echo -n "Description: " + read description < /dev/tty + local host='{}' + if [ -n "$name" ]; then + host="$(echo $host | jq ".name=\"$name\"" )" + fi + if [ -n "$ip" ]; then + host="$(echo $host | jq ".ip=\"$ip\"" )" + fi + if [ -n "$description" ]; then + host="$(echo $host | jq ".description=\"$description\"" )" + fi + yq --yaml-output ".hosts |= .+ [$host]" "$index" > newindex + rm "$index" + mv newindex "$index" + ;; + *) + echo "Unknown command $1" + print_help + exit 1 + ;; + esac + fi + +} + +if [ -n "$1" ] ; then + case "$1" in + -h|--help) + print_help + exit 0 + ;; + #list) + # list_projects + # exit 0 + # ;; + #switch) + # switch "$2" + # exit 0 + # ;; + #current) + # get_current "$2" + # exit 0 + # ;; + #new) + # new_project "$2" + # exit 0 + # ;; + #copy) + # shift + # copyto "$@" + # exit 0 + # ;; + #serve) + # shift + # serve "$@" + # exit 0 + # ;; + #hosts) + # shift + # hosts "$@" + # exit 0 + # ;; + *) + executable="project-${1}" + if command -v "$executable" > /dev/null; then + shift + "$executable" "$@" + exit 0 + else + echo "Unknown command $1" + print_help + exit 1 + fi + ;; + esac +fi + diff --git a/bin/.bin/project-management/project-copy b/bin/.bin/project-management/project-copy new file mode 100755 index 00000000..a10dadb5 --- /dev/null +++ b/bin/.bin/project-management/project-copy @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +function print_help(){ + echo -e "copy [..]\t\tCopies files to a target in the project" + echo -e "\t--target \tCopy files to a target directory in the project" + echo -e "\t--bin\tShortcut to copy to bin folder (inside \$PATH)" + echo -e "\t--www\tShortcut to copy to the www directory" +} + +function copy(){ + local current="$(get_current --path)" + local target="$1" + shift + local files="$@" + local destination= "$current/$target" + if [[ "$target" == "--NONE" ]]; then + local destination= "$current/" + fi + if [[ -n "$destination" ]]; then + mkdir -p "$destination" + cp -r "$files" "$destination" + fi +} + + +if [ -n "$1" ]; then + case "$1" in + -h|--help) + print_help | column -t -s" " + exit 0 + ;; + --parent-help) + print_help + exit 0 + ;; + --target) + target="$2" + shift + shift + copy "$target" "$@" + exit 0 + ;; + --www) + shift + copy "www" "$@" + exit 0 + ;; + --bin) + shift + copy "bin" "$@" + exit 0 + ;; + *) + copy "--NONE" "$@" + exit 0 + ;; + esac +else + read_prompt | xargs project-new +fi diff --git a/bin/.bin/project-management/project-current b/bin/.bin/project-management/project-current new file mode 100755 index 00000000..021c1d32 --- /dev/null +++ b/bin/.bin/project-management/project-current @@ -0,0 +1,83 @@ +#!/usr/bin/env bash + +NOTIFY=0 +PRINTPATH=0 + +function print_help(){ + echo -e "current\t\tPrints the current project" + echo -e "\t--notify\tUses notify send to print current project" + echo -e "\t--path\tPrints the full path rather than the name" +} + +function get_name(){ + local path=$(get_path) + local name + if [ $? -eq "0" ]; then + if [ -f "$path"/index.yaml ]; then + name=$(cat "$path/index.yaml" | yq -r '.name') + fi + if [ -z "$name" ]; then + name=$(basename "$path") + fi + echo "$name" + exit 0 + fi + exit 1 +} + +function get_path(){ + if [ -L "$SYMLINC" ]; then + readlink "$SYMLINC" + exit 0 + fi + exit 1 +} + +function get_current(){ + local ret + if [ "$PRINTPATH" -eq 1 ]; then + ret=$(get_path) + else + ret=$(get_name) + fi + + if [ "$NOTIFY" -eq 1 ]; then + if [ -n "$ret" ]; then + notify-send "Current Project" "$ret" + else + notify-send "Current Project" "Not Set" + fi + else + echo "$ret" + fi + + if [ -n "$ret" ]; then + exit 0 + else + exit 1 + fi +} + +while [ -n "$1" ]; do + case "$1" in + -h|--help) + print_help | column -t -s" " + exit 0 + ;; + --parent-help) + print_help + exit 0 + ;; + --notify) + NOTIFY=1 + shift + ;; + --path) + PRINTPATH=1 + shift + ;; + esac +done + +get_current +exit $? diff --git a/bin/.bin/project-management/project-hosts b/bin/.bin/project-management/project-hosts new file mode 100755 index 00000000..bd441a6a --- /dev/null +++ b/bin/.bin/project-management/project-hosts @@ -0,0 +1,87 @@ +#!/usr/bin/env bash + +function print_help(){ + echo -e "hosts\t\tManages hosts in the yaml file" +} + +function listhosts(){ + local current="$(project current --path)" + index="$current/index.yaml" + if [ ! -f "$index" ]; then + echo "No index file" + exit 1 + fi + if [ $(yq -r '.hosts | length ' "$index" ) -gt 0 ]; then + yq -r '.hosts[] | [.name, .ip, .domain, .description] | join(",")' "$index" + else + echo "No hosts in index file" + exit 1 + fi +} + +function getip(){ + local current="$(project current --path)" + index="$current/index.yaml" + case "$1" in + --fzf) + ip=$( listhosts | fzf --no-preview | cut -d',' -f2) + ;; + --dmenu) + ip=$( listhosts | dmenu | cut -d',' -f2) + echo "$ip" | xclip -selection primary + echo "$ip" | xclip -selection clipboard + ;; + --rofi) + ip=$( listhosts | rofi -dmenu | cut -d',' -f2) + echo "$ip" | xclip -selection primary + echo "$ip" | xclip -selection clipboard + ;; + *) + ip=$( listhosts | fzf --no-preview | cut -d',' -f2) + ;; + esac + echo $ip +} + +function addip(){ + echo -n "Name: " + read name < /dev/tty + echo -n "IP: " + read ip < /dev/tty + echo -n "Description: " + read description < /dev/tty + local host='{}' + if [ -n "$name" ]; then + host="$(echo $host | jq ".name=\"$name\"" )" + fi + if [ -n "$ip" ]; then + host="$(echo $host | jq ".ip=\"$ip\"" )" + fi + if [ -n "$description" ]; then + host="$(echo $host | jq ".description=\"$description\"" )" + fi + yq --yaml-output ".hosts |= .+ [$host]" "$index" > newindex + rm "$index" + mv newindex "$index" +} + +case "$1" in + -h|--help) + print_help | column -t -s" " + exit 0 + ;; + --parent-help) + print_help + exit 0 + ;; + list) + listhosts + ;; + ip) + getip + ;; + add) + addip + ;; +esac + diff --git a/bin/.bin/project-management/project-list b/bin/.bin/project-management/project-list new file mode 100755 index 00000000..74eff8fe --- /dev/null +++ b/bin/.bin/project-management/project-list @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Lists the available projects +# If the --options flag is given, it will also list --new and, if relevant, --unset. +# This is useful foa piping it into a fuzzy finder +function list_projects(){ + if [[ "$1" == "--options" ]]; then + if [ -L "$SYMLINC" ]; then + echo "--unset" + fi + echo "--new" + fi + find "$PROJECTS_PATH/" -maxdepth 1 -mindepth 1 -type d | xargs -L 1 basename +} + +function print_help(){ + echo -e "list\t\tLists the available projects" + echo -e "\t--options\tAdds the --new option and the --unset option if appropriate" +} + +case "$1" in + -h|--help) + print_help | column -t -s" " + exit 0 + ;; + --parent-help) + print_help + exit 0 + ;; + *) + list_projects "$@" + exit 0 + ;; +esac diff --git a/bin/.bin/project-management/project-new b/bin/.bin/project-management/project-new new file mode 100755 index 00000000..4657893f --- /dev/null +++ b/bin/.bin/project-management/project-new @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +function print_help(){ + echo -e "new\t\tCreates a new project. Will prompt for a name if no argements are given" + echo -e "\t\tCreates a new project with the name given" + echo -e "\t--dmenu\tPrompt using dmenu" + echo -e "\t--rofi\tPrompt using rofi" + echo -e "\t--read\tPrompt using read" +} + +function read_prompt(){ + local name + echo -n "Project name: " > /dev/tty + read name < /dev/tty + echo "$name" +} + +function dmenu_prompt(){ + local name + name=$(echo "" | dmenu -p "Project name: " ) + echo "$name" +} + +function rofi_prompt(){ + local name + name=$(rofi -dmenu -l 0 -p "Project name" ) + echo "$name" +} + +function create_project(){ + local name="$1" + local default_dirs=( bin screenshots Downloads www ) + # If name empty, exit + if [ -z "$name" ]; then + exit 1 + fi + mkdir -p "$PROJECTS_PATH/$name" + # Make all the directories + for dir in "${default_dirs[@]}"; do + mkdir "$PROJECTS_PATH/$name/$dir" + done +} + +if [ -n "$1" ]; then + case "$1" in + -h|--help) + print_help | column -t -s" " + exit 0 + ;; + --parent-help) + print_help + exit 0 + ;; + --read) + read_prompt | xargs project-new + exit 0 + ;; + --dmenu) + dmenu_prompt | xargs project-new + exit 0 + ;; + --rofi) + rofi_prompt | xargs project-new + exit 0 + ;; + *) + create_project "$1" + exit 0 + ;; + esac +else + read_prompt | xargs project-new +fi diff --git a/bin/.bin/project-management/project-serve b/bin/.bin/project-management/project-serve new file mode 100755 index 00000000..059976eb --- /dev/null +++ b/bin/.bin/project-management/project-serve @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +function print_help(){ + echo -e "serve\t\tmanages a webserver for the project" + echo -e "\t--start [port [bind]]\tstarts the server" + echo -e "\t--stop\tstops the server" + echo -e "\t--status\treports the status of the server" +} + +function start_server(){ + local current="$(project current --path)" + if [ -z "$current" ]; then + echo "Set current project first" + exit 1 + fi + local www="$current/www/" + local port=${1:-"8000"} + local bind="$2" + if [ -d "$www" ]; then + echo "Creating www directory" + fi + local count=$( find "$www" -type f | wc -l) + if [ -n "$bind" ]; then + echo "Serving $count files to $bind from the $www directory" + setsid python -m http.server "$port" --bind "$bind" --directory "$www" > /dev/null 2> /dev/null & + else + echo "Serving $count files from the $www directory" + setsid python -m http.server "$port" --directory "$www" > /dev/null 2> /dev/null & + fi +} + +function stop_server(){ + echo "Stopping..." + ps -aux | grep -i "python" | grep -v "grep" | awk '{print $2}' | xargs kill + echo "Stopped" +} + +function status_server(){ + ps -aux | grep -v "grep" | grep -qi "python" + if [ $? -eq 1 ]; then + # There is not a server running + echo "Server not running" + else + echo "Server is running" + fi +} + +case "$1" in + -h|--help) + print_help | column -t -s" " + exit 0 + ;; + --parent-help) + print_help + exit 0 + ;; + --start) + start_server + exit 0 + ;; + --stop) + stop_server + exit 0 + ;; + --status) + status_server + exit 0 + ;; +esac diff --git a/bin/.bin/project-management/project-switch b/bin/.bin/project-management/project-switch new file mode 100755 index 00000000..b7cc555c --- /dev/null +++ b/bin/.bin/project-management/project-switch @@ -0,0 +1,86 @@ +#!/usr/bin/env bash + +function print_help(){ + echo -e "switch\t\tSwitches the current project" + echo -e "\t--auto\tAutomatically deduce the project based on current working directory" + echo -e "\t--fzf\tUses fzf to choose a project" + echo -e "\t--dmenu\tUses dmenu to choose a project" + echo -e "\t--rofi\tUses rofi to choose a project" + echo -e "\t--unset\tUnsets the project" + echo -e "\t\tSets the project" +} + +function auto_switch(){ + local switchto + if echo "$PWD" | grep -q "$PROJECTS_PATH"; then + switchto=$( echo "$PWD" | sed "s#$PROJECTS_PATH/##") + switchto="${switchto%/*}" + if [[ "$PWD" == "$PROJECTS_PATH" ]]; then + switchto="--unset" + fi + else + switchto="--unset" + fi + "$0" "$switchto" +} +function unset_project(){ + if [ -L "$SYMLINC" ]; then + rm "$SYMLINC" 2> /dev/null + # See note about SIGWINCH below + pkill -u $USER -SIGWINCH zsh + fi +} + +function set_project(){ + local switchto="$PROJECTS_PATH/$1" + if [ -d "$switchto" ]; then + if [ -L "$SYMLINC" ]; then + rm "$SYMLINC" 2> /dev/null + fi + ln -s "$switchto" "$SYMLINC" + pkill -u $USER -SIGWINCH zsh + else + echo "No such directory $switchto" + exit 1 + fi +} + +case "$1" in + -h|--help) + print_help | column -t -s" " + exit 0 + ;; + --parent-help) + print_help + exit 0 + ;; + --auto) + auto_switch + exit 0 + ;; + --fzf) + project-list --options | fzf --no-preview | xargs "$0" + exit 0 + ;; + --dmenu) + project-list --options | dmenu -i | xargs "$0" + exit 0 + ;; + --rofi) + project-list --options | rofi -dmenu -i | xargs "$0" + exit 0 + ;; + --unset) + unset_project + exit 0 + ;; + --new) + project_new + exit 0 + ;; + *) + set_project "$1" + #list_projects "$@" + exit 0 + ;; +esac