-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
argparse.sh
85 lines (74 loc) · 2.61 KB
/
argparse.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# CLI CONSTS
version="3.1.3"
num_args=$#
date=$(date)
year="${date:24:29}"
# ~~~~~~~~~~~~~~~~ Load classes ~~~~~~~~~~~~~~~~~~~~~~~~
# Load all the bash OOP-style classes to be inherited
# by downstream scripts
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source $WYX_DIR/src/classes/sys/sys.h
sys sys
source $WYX_DIR/src/classes/wgit/wgit.h
wgit wgit
source $WYX_DIR/src/classes/wyxd/wyxd.h
wyxd wyxd
source $WYX_DIR/src/classes/lib/lib.h
lib lib
# ~~~~~~~~~~~~~ Load source git data ~~~~~~~~~~~~~~~~~~~~
# Check if the current directory is a git repository,
# and if so, get the branch and remote URLs across the
# different git hosting services.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
branch=""
if git rev-parse --git-dir > /dev/null 2>&1; then
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
fi
remote=$(git config --get remote.origin.url)
repo_url=$(echo "$remote" | sed 's/[^ \/]*\/\([^ ]*\/[^.]*\).*/\1/' | sed 's/\.git//')
git_host=""
#
if [[ $remote == *"github.com"* ]]; then
git_host="github"
repo_url=${repo_url#"[email protected]:"}
elif [[ $remote == *"gitlab.com"* ]]; then
git_host="gitlab"
repo_url=${repo_url#"[email protected]:"}
elif [[ $remote == *"bitbucket.org"* ]]; then
git_host="bitbucket"
repo_url=$(echo "$repo_url" | sed 's/\/[^ \/]*\/\([^ \/]*\/[^ \/]*\)/\1/')
elif [[ $remote == *"azure.com"* ]]; then
git_host="azure"
org=$(echo "$repo_url" | sed 's/\([^ \/]*\).*/\1/')
project=$(echo "$repo_url" | sed 's/[^ \/]*\/\([^ \/]*\).*/\1/')
repo=$(echo "$repo_url" | sed 's/.*\/\([^ \/]*\)/\1/')
repo_url="${org}/${project}/_git/${repo}"
fi
# ~~~~~~~~~~~~~~~~ Parse input ~~~~~~~~~~~~~~~~~~~~~~~~~
# Parse the input into a command object and run it if
# it is valid. If the command is invalid, show an error
# message.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if [ $num_args -eq 0 ]; then
# No input - show command info
wyxd.command_info
else
source $WYX_DIR/src/classes/cmd/cmd.h
# Parse input into command object and run it (if valid)
cmd inputCommand
inputCommand.id '=' $1
inputCommand_path="${WYX_DIR}/src/commands/$(inputCommand.path).sh"
if [ -f "${inputCommand_path}" ]; then
# set trap
trap 'inputCommand.unset && echo "trap happening"' EXIT
# Valid command found - run it
source "${inputCommand_path}" "${@:2}"
inputCommand.unset
else
# Invalid command - show error message
sys.log.error "Invalid command! Try again"
echo "Type 'wyx' to see the list of available commands (and their arguments), or 'wyx help' to be redirected to more in-depth online documentation"
fi
fi
unset WYX_DIR WYX_DATA_DIR WYX_SCRIPT_DIR