CLI: Update

This commit is contained in:
Christer Waren
2026-09-17 14:28:43 +03:00
parent f4f0665abb
commit 756059c2d9
2 changed files with 180 additions and 77 deletions
Executable
+2
View File
@@ -0,0 +1,2 @@
rm wx
cp ../infra/cli.sh wx
+178 -77
View File
@@ -1,15 +1,130 @@
#!/bin/bash
if [ ! "$BASH_VERSION" ] ; then
echo "Warén Group - Infra"
echo "This command requires using BASH shell."
exit 1
fi
#if [ ! "$BASH_VERSION" ] ; then
# bash $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
# exit 1
#fi
if [[ -L "$0" ]]
then
PWD=$(dirname -- "$(readlink -f -- "$0")")
fi
declare -Ax args
declare -Ax config
declare -Ax messages
wxi-args(){
declare -gAx args
args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--*=*)
key="${1%%=*}"
value="${1#*=}"
args["${key#--}"]="$value"
shift
;;
--*)
key="$1"
opt="${key#--}"
shift
if [[ $# -gt 0 && "${1}" != -* ]]; then
args["$opt"]="$1"
shift
else
args["$opt"]=true
fi
;;
-*=*)
key="${1%%=*}"
value="${1#*=}"
args["${key#-}"]="$value"
shift
;;
-*)
key="$1"
opt="${key#-}"
shift
if [[ $# -gt 0 && "${1}" != -* ]]; then
args["$opt"]="$1"
shift
else
args["$opt"]=true
fi
;;
*)
args["$((++pos))"]="${1%%=*}"
shift
;;
esac
done
}
wxi-config(){
declare -gAx config
case $1 in
create)
if [[ ! -f $HOME/.warengroup/config.json ]]
then
echo '{}' > $HOME/.warengroup/config.json
fi
;;
deploy)
if [[ ! $(jq -e . $HOME/.warengroup/config.json.tmp >/dev/null 2>&1) ]]
then
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
fi
;;
write)
jq '.'"$2"' = "'$3'"' $HOME/.warengroup/config.json &> $HOME/.warengroup/config.json.tmp
wxi-config deploy
;;
load)
while IFS=$'\t' read -r k v; do config["$k"]="$v"; done < <(
jq -r '
paths(scalars) as $p
| "\( $p|map(tostring)|join(".") )\t\( getpath($p) )"
' $HOME/.warengroup/config.json
)
;;
reload)
wxi-config load
;;
*)
WX_HOME=$HOME/.warengroup
export WX_HOME
mkdir -p $WX_HOME
chmod 700 -R "$WX_HOME"
mkdir -p "$WX_HOME/credentials" \
"$WX_HOME/credentials/ssh" \
"$WX_HOME/credentials/ssh/eas" \
"$WX_HOME/credentials/ansible-vault/eas" \
"$PWD/eas/credentials/ssh"
find "$WX_HOME" -type d -exec chmod 700 {} +
find "$WX_HOME" -type f -exec chmod 600 {} +
wxi-config create
if [[ -n ${args['proxy']} ]]
then
wxi-config write proxy "${args['proxy']}"
fi
wxi-config load
PROXY_ARGS=()
if [[ -n "${config['proxy']}" ]]; then
PROXY_ARGS=(--proxy "${config['proxy']}")
fi
;;
esac
}
wxi-config-legacy(){
case $1 in
login)
case $2 in
@@ -160,12 +275,12 @@ wxi-content(){
if [[ $1 == "text" ]]
then
echo "$2"
elif [[ $1 == "text*" ]]
then
echo -n "$2"
elif [[ $1 == "status" ]]
then
wxi-repeat "\n" 2
echo -n "$wxiBold"
echo "Status"
echo -n "$wxiNormal"
echo -n "Status: "
echo "$2 - $3"
elif [[ $1 == "link" ]]
then
@@ -200,32 +315,44 @@ wxiNormal=$(tput sgr0)
wxi-header(){
if [[ $2 == "h1" ]]
then
wxi-repeat "\n" 3
echo "=============================="
wxi-repeat " " $((30/2-${#1}/2))
#wxi-repeat " " $(($(tput cols)/2-${#1}/2))
echo -n "$wxiBold"
echo "$1"
echo ${1^^}
echo -n "$wxiNormal"
echo "=============================="
echo ""
fi
if [[ $2 == "h2" || -z $2 ]]
if [[ $2 == "h1*" ]]
then
wxi-start
wxi-repeat " " $((30/2-6/2-${#1}/2))
#wxi-repeat " " $(($(tput cols)/2-${#1}/2))
echo -n "$wxiBold"
echo ${1^^}
echo -n "$wxiNormal"
fi
if [[ $2 == "h2" ]]
then
#wxi-repeat " " $(($(tput cols)/2-${#1}/2))
echo -n "$wxiBold"
echo ">> $1 <<"
echo -n "$wxiNormal"
echo "------------------------------"
echo ""
fi
if [[ $2 == "h3" ]]
then
#wxi-repeat " " $(($(tput cols)/2-${#1}/2))
echo -n "$wxiBold"
echo "$1"
echo -n "$wxiNormal"
fi
if [[ $2 == "h4" ]]
then
#wxi-repeat " " $(($(tput cols)/2-${#1}/2))
echo -n "$wxiUnderline"
echo "$1"
echo -n "$wxiNormal"
fi
}
@@ -304,29 +431,29 @@ wx-infra(){
git clone ssh://git@git.waren.io:2222/warengroup-private/infra.git --config core.sshCommand="ssh -i $HOME/.ssh/keys/warengroup-legacy -o ProxyJump=none" "$INFRA_PATH" &> /dev/null
fi
if [[ ! -f "$INFRA_PATH/vault/cwchristerw" || ! -f "$INFRA_PATH/vault/warengroup" ]]
if [[ ! -f "$HOME/.warengroup/credentials/ansible-vault/cwchristerw" || ! -f "$HOME/.warengroup/credentials/ansible-vault/warengroup" ]]
then
mkdir -p "$INFRA_PATH/vault" &> /dev/null
mkdir -p "$HOME/.warengroup/credentials/ansible-vault" &> /dev/null
curl \
-H "X-Vault-Token: $TOKEN" \
-X GET \
https://$VAULT_DOMAIN/v1/cli/data/cwchristerw/settings/infra -s | jq -r '.data.data.cwchristerw' > "$INFRA_PATH/vault/cwchristerw"
https://$VAULT_DOMAIN/v1/cli/data/cwchristerw/settings/infra -s | jq -r '.data.data.cwchristerw' > "$HOME/.warengroup/credentials/ansible-vault/cwchristerw"
curl \
-H "X-Vault-Token: $TOKEN" \
-X GET \
https://$VAULT_DOMAIN/v1/cli/data/cwchristerw/settings/infra -s | jq -r '.data.data.warengroup' > "$INFRA_PATH/vault/warengroup"
https://$VAULT_DOMAIN/v1/cli/data/cwchristerw/settings/infra -s | jq -r '.data.data.warengroup' > "$HOME/.warengroup/credentials/ansible-vault/warengroup"
fi
INFRA_VAULT="--vault-id warengroup@vault/warengroup --vault-id cwchristerw@vault/cwchristerw"
INFRA_VAULT="--vault-id warengroup@$HOME/.warengroup/credentials/ansible-vault/warengroup --vault-id cwchristerw@$HOME/.warengroup/credentials/ansible-vault/cwchristerw"
;;
*)
wxi-content status "User" "Unsupported"
wxi-footer
wxi-stop
INFRA_PATH="$HOME/.warengroup/infra"
INFRA_VAULT="--vault-id warengroup@vault/warengroup"
INFRA_VAULT="--vault-id warengroup@$HOME/.warengroup/credentials/ansible-vault/warengroup"
;;
esac
@@ -334,7 +461,7 @@ wx-infra(){
then
echo "Tag Required"
else
cd "$INFRA_PATH"
cd "$INFRA_PATH/ansible"
#git pull &> /dev/null
#ansible-galaxy collection install -r requirements.yml --upgrade &> /dev/null
@@ -424,7 +551,7 @@ wx-login(){
elif [[ ! -z ${args['token']} ]]
then
AUTH_METHOD=token
elif [[ $(wxi-config login read) ]]
elif [[ $(wxi-config-legacy login read) ]]
then
AUTH_METHOD=token
elif [[ ! -z ${args['username']} ]]
@@ -478,7 +605,7 @@ wx-login(){
fi
TOKEN=$VAULT_LOGIN
wxi-config login write
wxi-config-legacy login write
;;
token)
echo -n "Token: "
@@ -488,9 +615,9 @@ wx-login(){
then
TOKEN=${args['token']}
fi
elif [[ $(wxi-config login read) ]]
elif [[ $(wxi-config-legacy login read) ]]
then
TOKEN=$(wxi-config login read)
TOKEN=$(wxi-config-legacy login read)
else
read -s TOKEN
fi
@@ -520,7 +647,7 @@ wx-login(){
wxi-content status "Login" "Failed"
wxi-footer
if [[ $(wxi-config login read) ]]
if [[ $(wxi-config-legacy login read) ]]
then
wx-logout &> /dev/null
wx-login
@@ -530,7 +657,7 @@ wx-login(){
fi
TOKEN=$VAULT_LOGIN
wxi-config login write
wxi-config-legacy login write
;;
*)
wxi-content status "Login Type" "Unsupported"
@@ -576,7 +703,7 @@ wx-logout(){
then
wxi-header "$ORG_HEADER" h3
echo "Logging Out..."
wxi-config login erase
wxi-config-legacy login erase
wxi-footer
fi
@@ -901,52 +1028,26 @@ wxi-ssh-keys-sync(){
}
i=1
while [[ "$1" != "" ]]
do
case $1 in
--*)
key="${1%%=*}"
value="${1#*=}"
wxi-args "$@"
wxi-config
if [[ "$value" == "$key" ]]
then
shift
value="$1"
fi
echo -n $wxiBold$wxiRed
cat <<'EOF'
if [[ -z $value ]]
then
value=true
fi
(`\ .-') /` ('-. _ .-') ('-. .-') _
`.( OO ),' ( OO ).-.( \( -O ) _( OO) ( OO ) )
,--./ .--. / . --. / ,------. (,------.,--./ ,--,'
| | | | \-. \ | /`. ' | .---'| \ | |\
| | | |,.-'-' | | | / | | | | | \| | )
| |.'.| |_)\| |_.' | | |_.' |(| '--. | . |/
| | | .-. | | . '.' | .--' | |\ |
| ,'. | | | | | | |\ \ | `---.| | \ |
'--' '--' `--' `--' `--' '--' `------'`--' `--'
args["${key#--}"]="$value"
;;
-*)
key="${1%=*}"
value="${1#*=}"
if [[ "$value" == "$key" ]]
then
shift
value="$1"
fi
if [[ -z $value ]]
then
value=true
fi
args["${key#-}"]="$value"
;;
*)
args["$i"]="${1%%=*}"
i=$((i + 1))
;;
esac
shift
done
EOF
echo -n $wxiNormal
wxi-start
if [[ ! -z ${args['1']} ]] && [[ $(type -t wx-${args['1']}) == function ]]
then
wx-${args['1']}