Skip to content

Commit

Permalink
feat: update configuration and add environment variables
Browse files Browse the repository at this point in the history
- Modified `.config/bin/config.json`: Added a new entry for `/env/bin/argc`
- Added new file `.func/cli/hos` with 159 lines of code
- Modified `.func/info/me`: Fixed a typo in the address
- Modified `.profile`: Added environment variables `GUM_CHOOSE_CURSOR_FOREGROUND` and `GUM_CHOOSE_SELECTED_FOREGROUND`
  • Loading branch information
bresilla committed Apr 30, 2024
1 parent 985695f commit 3b151a2
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .config/bin/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"default_path": "/env/bin",
"bins": {
"/env/bin/argc": {
"path": "/env/bin/argc",
"remote_name": "argc",
"version": "v1.17.0",
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"url": "https://github.com/sigoden/argc",
"provider": "github",
"package_path": "argc"
},
"/env/bin/atuin": {
"path": "/env/bin/atuin",
"remote_name": "atuin",
Expand Down
159 changes: 159 additions & 0 deletions .func/cli/hos
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/bin/bash

hostnames=/etc/hosts
sshfile=~/.ssh/config

select_one() {
cat $hostnames | awk '$1 ~ /^[^;#]/' | tail -n +4 | gum choose --limit=1 --height=20
}

select_many() {
cat $hostnames | awk '$1 ~ /^[^;#]/' | tail -n +4 | gum choose --no-limit --height=20
}

select_all() {
cat $hostnames | awk '$1 ~ /^[^;#]/' | tail -n +4
}

# @cmd Add a hostname to the list
# @option --name Name of the host
# @option --ip IP address of the host
add() {
if [ -z $argc_name ]; then
name=$(gum input --placeholder "Name?")
else
name=$argc_name
fi
if [[ ! $name =~ ^[a-zA-Z0-9.]+$ ]]; then
gum log --structured --level error "Invalid name, must be alphanumeric with dots"
exit 1
elif grep -q "$name" $hostnames; then
gum log --structured --level error "Name already exists"
exit 1
fi

if [ -z $argc_ip ]; then
ip=$(gum input --placeholder "IP?")
else
ip=$argc_ip
fi
if [[ ! $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
gum log --structured --level error "Invalid IP address, must be in the form of x.x.x.x"
exit 1
elif grep -q "$ip" $hostnames; then
gum log --structured --level error "IP already exists"
exit 1
fi
new_object="$ip $name"
if gum confirm "are you sure you want to add $new_object"; then
echo $new_object | doas tee -a $hostnames
fi
gum log --structured --level info "added $new_object"
# echo "added successfully"
exit 0
}

# @cmd Edit a hostname
edit() {
echo edit "$@"
}

# @cmd List all hostnames
list() {
select_one
}

# @cmd Remove a hostname from the list
# @option --name Name of the host
# @option --ip IP address of the host
remove() {
if [ -z $argc_name ] && [ -z $argc_ip ]; then
selected=$(select_many)
[ -z "$selected" ] && gum log --structured --level error "No host selected" && exit 1
while IFS= read -r line; do
if gum confirm "are you sure you want to remove $line"; then
doas sed -i "/$line/d" $hostnames
gum log --structured --level info "host $line removed"
fi
done <<< "$selected"
exit 0
fi

if [ -z $argc_ip ]; then
grep -q "$argc_name" $hostnames || { gum log --structured --level error "Name not found"; exit 1; }
line=$(grep "$argc_name" $hostnames)
elif [ -z $argc_name ]; then
grep -q "$argc_ip" $hostnames || { gum log --structured --level error "IP not found"; exit 1; }
line=$(grep "$argc_ip" $hostnames)
else
gum log --structured --level error "Invalid arguments"
exit 1
fi
echo
if gum confirm "are you sure you want to remove $line"; then
doas sed -i "/$line/d" $hostnames
gum log --structured --level info "host $line removed"
fi
}

# @cmd SSH into a hostname
# @option --hostname Name of the host
# @option --ip IP address of the host
# @option --username Username to use
# @option --port Port to use
# @option --key Key to use / IdentityFile
# @arg num[=one|all]
ssh() {
keytrigger=true
if [ $argc_num == "all" ]; then
selected=$(select_all)
keytrigger=false
elif [ ! -z $argc_hostname ] && [ ! -z $argc_ip ]; then
selected="$argc_ip $argc_hostname"
else
selected=$(select_many)
fi

[ -z "$selected" ] && gum log --structured --level error "No host selected" && exit 1

while IFS= read -r line; do
ip=$(echo $line | awk '{print $1}')
name=$(echo $line | awk '{print $2}')
string="Host $name\n\t# $ip\n\thostname $name\n"

if grep -q "Host $name" $sshfile; then
gum log --structured --level info "Host $name already exists in $sshfile"
continue
fi

if [ -z $argc_username ]; then
username=$(whoami)
else
username=$argc_username
fi
string+="\tuser $username\n"

if [ -z $argc_port ]; then
port=22
else
port=$argc_port
fi
string+="\tport $port\n"

if [ ! -z $argc_key ]; then
string+="\tidentityFile $argc_key\n"
elif $keytrigger; then
if gum confirm "do you want to add a key for $name"; then
key=$(gum input --placeholder "Key?")
[ ! -f $key ] && { gum log --structured --level error "Key not found"; exit 1; }
string+="\tidentityFile $key\n"
fi
fi

echo -e $string | doas tee -a $sshfile
done <<< "$selected"
exit 0

}

eval "$(argc --argc-eval "$0" "$@")"
2 changes: 1 addition & 1 deletion .func/info/me
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

printf "HOME:"
echo
printf "ADDRES:\t\tTrim Bresilla, Dubceksingel 6, Ede Gld, Gelderland, 6716RG, NL"
printf "ADDRES:\t\tTrim Bresilla, Dubceksingel 6, Ede, Gelderland, 6716RG, NL"
echo
printf "PHONE:\t\t(+31) 6 214 36744"
echo
Expand Down
8 changes: 5 additions & 3 deletions .profile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export BAT_THEME="TwoDark"
export LULE_W="/env/set/.wallpaper"
export LULE_S="/env/dot/.func/wm/lule_colors"
export LULE_C="/home/bresilla/.config/lule/configs.json"
export GUM_CHOOSE_CURSOR_FOREGROUND="1"
export GUM_CHOOSE_SELECTED_FOREGROUND="9"


#--------------------------- LOCALE --------------------------
Expand Down Expand Up @@ -120,9 +122,9 @@ export PYTHONPATH=/usr/local/webots/lib/controller/python:$PYTHONPATH


#---------------------------- HIVE-SERVER ---------------------------
export OLLAMA_HOST=10.10.22.203:11434
export OATMEAL_OLLAMA_URL=http://10.10.22.176:11434
export DOCKER_HOST=tcp://10.10.22.176:2375
export OLLAMA_HOST=data:11434
export OATMEAL_OLLAMA_URL=http://data:11434
export DOCKER_HOST=tcp://data:2375

#--------------------------- CORE --------------------------
if [ "$HOSTNAME" = core ]; then
Expand Down

0 comments on commit 3b151a2

Please sign in to comment.