Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Terzibaschian committed Oct 5, 2024
1 parent a11ca5a commit 14628e5
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 0 deletions.
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
default:
@just --list
generate:
@just bashly generate
bashly command="generate":
docker run --rm -it --user $(id -u):$(id -g) --volume "$PWD:/app" dannyben/bashly {{command}}
76 changes: 76 additions & 0 deletions settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# All settings are optional (with their default values provided below), and
# can also be set with an environment variable with the same name, capitalized
# and prefixed by `BASHLY_` - for example: BASHLY_SOURCE_DIR
#
# When setting environment variables, you can use:
# - "0", "false" or "no" to represent false
# - "1", "true" or "yes" to represent true
#
# If you wish to change the path to this file, set the environment variable
# BASHLY_SETTINGS_PATH.

# The path containing the bashly source files
source_dir: src

# The path to bashly.yml
config_path: "%{source_dir}/bashly.yml"

# The path to use for creating the bash script
target_dir: .

# The path to use for common library files, relative to source_dir
lib_dir: lib

# The path to use for command files, relative to source_dir
# When set to nil (~), command files will be placed directly under source_dir
# When set to any other string, command files will be placed under this
# directory, and each command will get its own subdirectory
commands_dir: ~

# Configure the bash options that will be added to the initialize function:
# strict: true Bash strict mode (set -euo pipefail)
# strict: false Only exit on errors (set -e)
# strict: '' Do not add any 'set' directive
# strict: <string> Add any other custom 'set' directive
strict: false

# When true, the generated script will use tab indentation instead of spaces
# (every 2 leading spaces will be converted to a tab character)
tab_indent: false

# When true, the generated script will consider any argument in the form of
# `-abc` as if it is `-a -b -c`.
compact_short_flags: true

# When true, the generated script will consider any argument in the form of
# `--flag=value` and `-f=value` as if it is `--flag value` and `-f value`
# respectively.
conjoined_flag_args: true

# Set to 'production' or 'development':
env: production # Generate a smaller script, without file markers
# env: development # Generate with file markers

# The extension to use when reading/writing partial script snippets
partials_extension: sh

# Show command examples (if any) whenever the user does not provide the
# required arguments
show_examples_on_error: true

# When using private commands, flags, or environment variables, you may set
# this option to a name of an environment variable that, if set, will reveal
# all the private elements in the usage texts, as if they were public.
private_reveal_key: ~

# Display various usage elements in color by providing the name of the color
# function. The value for each property is a name of a function that is
# available in your script, for example: `green` or `bold`.
# You can run `bashly add colors` to add a standard colors library.
# This option cannot be set via environment variables.
usage_colors:
caption: bold
command: green_underlined
arg: blue
flag: magenta
environment_variable: cyan_bold
34 changes: 34 additions & 0 deletions src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: cgw
help: CyberghostVPN Wrapper CLI
version: 0.1.0
extensible: cyberghostvpn

dependencies:
curl: install with $(green "apt install curl")
cyberghostvpn:

commands:
- name: connect
alias: c
help: Connect to a VPN at specified location

args:
- name: countrycode
required: false
help: "Country code to connect to (default: current country)"

examples:
- cgw connect
- cgw connect US
- cgw connect DE
filters:
- enforce_root

- name: disconnect
alias: d
help: Disconnect from VPN
filters:
- enforce_root
- name: get-ip
alias: gi
help: Get your public IP address
14 changes: 14 additions & 0 deletions src/connect_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# echo "# this file is located in 'src/connect_command.sh'"
# echo "# code for 'cgw connect' goes here"
# echo "# you can edit it freely and regenerate (it will not be overwritten)"
# inspect_args

cc=${args[countrycode]}

if [[ -z "$cc" ]]; then
cc="$(get_current_country_code)"
fi

check_country_code "$cc"

"${deps[cyberghostvpn]}" --country-code "$cc" --connect
1 change: 1 addition & 0 deletions src/disconnect_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"${deps[cyberghostvpn]}" --disconnect
1 change: 1 addition & 0 deletions src/get_ip_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"${deps[curl]}" -s ip-api.com
13 changes: 13 additions & 0 deletions src/header.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# This wrapper is meant for an easier use of CyberghostVPN
# It is not meant to be used as a standalone script

if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n" >&2
exit 1
fi

# Enforce Root, because cyberghost-vpn requires root
if [[ $EUID -ne 0 ]]; then
exec sudo "$0" "$@"
fi
42 changes: 42 additions & 0 deletions src/lib/colors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## Color functions [@bashly-upgrade colors]
## This file is a part of Bashly standard library
##
## Usage:
## Use any of the functions below to color or format a portion of a string.
##
## echo "before $(red this is red) after"
## echo "before $(green_bold this is green_bold) after"
##
## Color output will be disabled if `NO_COLOR` environment variable is set
## in compliance with https://no-color.org/
##
print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}

red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
yellow() { print_in_color "\e[33m" "$*"; }
blue() { print_in_color "\e[34m" "$*"; }
magenta() { print_in_color "\e[35m" "$*"; }
cyan() { print_in_color "\e[36m" "$*"; }
bold() { print_in_color "\e[1m" "$*"; }
underlined() { print_in_color "\e[4m" "$*"; }
red_bold() { print_in_color "\e[1;31m" "$*"; }
green_bold() { print_in_color "\e[1;32m" "$*"; }
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
blue_bold() { print_in_color "\e[1;34m" "$*"; }
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
red_underlined() { print_in_color "\e[4;31m" "$*"; }
green_underlined() { print_in_color "\e[4;32m" "$*"; }
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }
17 changes: 17 additions & 0 deletions src/lib/country_codes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
get_current_country_code() {
local country_code
country_code="$(${deps[curl]} -s https://ipinfo.io/country)"
echo "${country_code}"
}

check_country_code() {
COUNTRY_CODE="$1"

# Check validity of country code
COUNTRY_CODE_LIST=$(${deps[cyberghostvpn]} --country-code | grep -c "$COUNTRY_CODE")
# This is a markdown list with three items per line where the last item is the country code
if [ ! "$COUNTRY_CODE_LIST" -eq 1 ]; then
echo "Invalid country code: $COUNTRY_CODE" 1>&2
exit 1
fi
}
15 changes: 15 additions & 0 deletions src/lib/filter_enforce_root.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Add any function here that is needed in more than one parts of your
## application, or that you otherwise wish to extract from the main function
## scripts.
##
## Note that code here should be wrapped inside bash functions, and it is
## recommended to have a separate file for each function.
##
## Subdirectories will also be scanned for *.sh, so you have no reason not
## to organize your code neatly.
##
filter_enforce_root() {
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
fi
}

0 comments on commit 14628e5

Please sign in to comment.