Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
mmascort-t4xi committed Aug 8, 2020
1 parent 04dca64 commit 4e04ba9
Show file tree
Hide file tree
Showing 12 changed files with 765 additions and 2 deletions.
88 changes: 86 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,86 @@
# bash-helpers
Simple bash helpers
# BASH HELPERS

## Installation

### Composer
```bash
composer require marcmascort/bash-helpers
```

### NPM
```bash
npm install mmb-bash-helpers
```

## Functions

Usage is very simply, you only need import helpers file.
```bash
source path/to/helpers
```

### Display

```bash
success <message>
warning <message>
error <message>
highlight <message>
text <message>
title <message>
yell <message>
```

### Execution

```bash
try <command>
die <message>
```

### Files

```bash
make_executable <path/to/fileOrFolder>
make_writable <path/to/fileOrFolder>
make_readable <path/to/fileOrFolder>
make_chmod <modes> <path/to/fileOrFolder>
make_mkdir <path/to/fileOrFolder>
make_mv <path/to/source/fileOrFolder> <path/to/target/fileOrFolder>
make_cp <path/to/source/fileOrFolder> <path/to/target/fileOrFolder>
make_rm <path/to/fileOrFolder>
```

### Strings

```bash
str_replace <search> <replace> <subject>
```

## Libraries

### Certificate generator

```bash
lib/cert-generate <hostname> <folder> <openssl_config_file>
```

### Docker machine port forwarder

```bash
lib/docker-machine-port-forwarder <port> [-h] [-s] [-f] [-e] [-hp]
```

### Manage /etc/hosts

```bash
lib/manage-etc-hosts add <hostname> [<ip>]
lib/manage-etc-hosts remove <hostname> [<ip>]
```

### Manage git

```bash
lib/manage-git clone <repository> <folder> [<branch>]
lib/manage-git submodule <arguments>
```
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "marcmascort/bash-helpers",
"description": "Bash helpers",
"keywords": [
"bash",
"helpers"
],
"homepage": "https://github.com/marcmascort/bash-helpers",
"license": "MIT",
"authors": [
{
"name": "Marc Mascort",
"email": "[email protected]",
"homepage": "https://marcmascort.com",
"role": "Developer"
}
],
}
6 changes: 6 additions & 0 deletions helpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

source ./src/display
source ./src/execution
source ./src/strings
source ./src/files
45 changes: 45 additions & 0 deletions lib/cert-generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

# Import helpers functions
source ../helpers

HOSTNAME=$1
if [ -z "${HOSTNAME}" ]
then
die "Hostname argument is required"
fi

CERT_FOLDER=$2
if [ -z "${CERT_FOLDER}" ]
then
die "Folder argument is required"
fi

OPENSSL_CONF_FILE=$3
if [ -z "${OPENSSL_CONF_FILE}" ]
then
die "Openssl configuration file argument is required"
fi

export CERT_HOSTNAME=${HOSTNAME}

highlight "> "
text "Generaton CA for "
highlight "${HOSTNAME}"
text " at "
highlight "${CERT_FOLDER}"
text " ... \n"

try openssl req -x509 -nodes -days 3650 -newkey rsa:4096 \
-reqexts SAN -extensions v3_ca \
-subj "/C=ES/ST=Barcelona/L=Barcelona/O=T4XI/CN=${HOSTNAME}/[email protected]" \
-config ${OPENSSL_CONF_FILE} \
-passin env:CERT_HOSTNAME \
-keyout ${CERT_FOLDER}/${HOSTNAME}.key \
-out ${CERT_FOLDER}/${HOSTNAME}.crt;

success "OK\n"

highlight "> Verifying ... \n"
openssl x509 -in ${CERT_FOLDER}/${HOSTNAME}.crt -noout -text
success "OK\n"
135 changes: 135 additions & 0 deletions lib/docker-machine-port-forwarder
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env bash

# https://raw.githubusercontent.com/johanhaleby/docker-machine-port-forwarder/master/pf

readonly PROGNAME=$(basename $0)

port="${1}"
foreground="false"
stop="false"
environment="default"
quiet="false"
hostport="$1"

usage="${PROGNAME} <port> [-h] [-s] [-f] [-e] [-hp] -- Forwards a docker-machine port so that you can access it locally
where:
-h, --help Show this help text
-s, --stop Stop the port forwarding process
-f, --foreground Run the docker-machine ssh client in foreground instead of background
-e, --environment The name of the docker-machine environment (default is default)
-q, --quiet Don't print anything to the console, not even errors
examples:
# Port forward port 8047 in docker-machine environment default
\$ ${PROGNAME} 8047
# Port forward docker port 8047 to host port 8087 in docker-machine environment default
\$ ${PROGNAME} 8087:8047
# Port forward port 8047 in docker-machine dev
\$ ${PROGNAME} 8047 -e dev
# Runs in foreground (port forwarding is automatically stopped when process is terminated)
\$ ${PROGNAME} 8047 -f
# Stop the port forwarding for this port
\$ ${PROGNAME} 8047 -s"

if [ $# -eq 0 ]; then
echo "$usage"
exit 1
fi

if [ -z "$1" ]; then
echo "You need to specify the port to forward" >&2
echo "$usage"
exit 1
fi

if [ "$#" -ne 0 ]; then
while [ "$#" -gt 0 ]
do
case "$1" in
-h|--help)
echo "$usage"
exit 0
;;
-f|--foreground)
foreground="true"
;;
-s|--stop)
stop="true"
;;
-e|--environment)
environment="$2"
;;
-q|--quiet)
quiet="true"
;;
--)
break
;;
-*)
echo "Invalid option '$1'. Use --help to see the valid options" >&2
exit 1
;;
# an option argument, continue
*) ;;
esac
shift
done
fi

pidport() {
lsof -n -i4TCP:$1 | grep --exclude-dir={.bzr,CVS,.git,.hg,.svn} LISTEN
}

# Check if port contains ":", if so we should split
if [[ $port == *":"* ]]; then
# Split by :
ports=(${port//:/ })
if [[ ${#ports[@]} != 2 ]]; then
if [[ $quiet == "false" ]]; then
echo "Port forwarding should be defined as hostport:targetport, for example: 8090:8080"
fi
exit 1
fi


hostport=${ports[0]}
port=${ports[1]}
fi


if [[ ${stop} == "true" ]]; then
result=`pidport $hostport`

if [ -z "${result}" ]; then
if [[ $quiet == "false" ]]; then
echo "Port $hostport is not forwarded, cannot stop"
fi
exit 1
fi

process=`echo "${result}" | awk '{ print $1 }'`
if [[ $process != "ssh" ]]; then
if [[ $quiet == "false" ]]; then
echo "Port $hostport is bound by process ${process} and not by docker-machine, won't stop"
fi
exit 1
fi

pid=`echo "${result}" | awk '{ print $2 }'` &&
kill $pid &&
echo "Stopped port forwarding for $hostport"
else
docker-machine ssh $environment `if [[ ${foreground} == "false" ]]; then echo "-f -N"; fi` -L $hostport:localhost:$port &&
if [[ $quiet == "false" ]] && [[ $foreground == "false" ]]; then
printf "Forwarding port $port"
if [[ $hostport -ne $port ]]; then
printf " to host port $hostport"
fi
echo " in docker-machine environment $environment."
fi
fi
88 changes: 88 additions & 0 deletions lib/manage-etc-hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash

# Import helpers functions
source ../helpers

ETC_HOSTS=/etc/hosts
HOSTNAME=$2
IP=${3:-"127.0.0.1"}
HOST_REGEX="\(\s\+\)${HOSTNAME}\s*$"

function checkHostnameArg() {
if [ -z "${HOSTNAME}" ]
then
die "Hostname argument is required"
fi
}

function remove() {
checkHostnameArg

highlight "> "
text "Removing "
highlight "${HOSTNAME}"
text " from your "
highlight "${ETC_HOSTS}"
text " ... "

if [ -n "$(grep -e ${HOST_REGEX} ${ETC_HOSTS})" ]
then
try sudo sed -ie "/[[:space:]]${HOSTNAME}/d" ${ETC_HOSTS}
if [ -n "$(grep -e ${HOST_REGEX} ${ETC_HOSTS})" ]
then
error "FAIL\n"
exit 1
else
success "OK\n"
fi
else
warning "SKIP\n"
fi
}

function add() {
checkHostnameArg

highlight "> "
text "Adding "
highlight "${HOSTNAME}"
text " to your "
highlight "${ETC_HOSTS}"
text " ... "

if [ -z "$(grep -e ${HOST_REGEX} ${ETC_HOSTS})" ]
then
try printf "%s\t%s\n" "${IP}" "${HOSTNAME}" | sudo tee -a ${ETC_HOSTS} > /dev/null
if [ -z "$(grep -e ${HOST_REGEX} ${ETC_HOSTS})" ]
then
error "FAIL\n"
exit 1
else
success "OK\n"
fi
else
warning "SKIP\n"
fi
}

# Execute
case $1 in
add) add ;;
remove) remove ;;
*)
title "AVAILABLE METHODS"

highlight " > ";
text "add"
warning " <HOSTNAME>"
highlight " [<IP>]"
text "\n"

highlight " > ";
text "remove"
warning " <HOSTNAME>"
text "\n"

echo ""
;;
esac
Loading

0 comments on commit 4e04ba9

Please sign in to comment.