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 #!/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 if [[ -L "$0" ]]
# bash $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 then
# exit 1 PWD=$(dirname -- "$(readlink -f -- "$0")")
#fi fi
declare -Ax args wxi-args(){
declare -Ax config declare -gAx args
declare -Ax messages 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(){ 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 case $1 in
login) login)
case $2 in case $2 in
@@ -160,12 +275,12 @@ wxi-content(){
if [[ $1 == "text" ]] if [[ $1 == "text" ]]
then then
echo "$2" echo "$2"
elif [[ $1 == "text*" ]]
then
echo -n "$2"
elif [[ $1 == "status" ]] elif [[ $1 == "status" ]]
then then
wxi-repeat "\n" 2 echo -n "Status: "
echo -n "$wxiBold"
echo "Status"
echo -n "$wxiNormal"
echo "$2 - $3" echo "$2 - $3"
elif [[ $1 == "link" ]] elif [[ $1 == "link" ]]
then then
@@ -200,32 +315,44 @@ wxiNormal=$(tput sgr0)
wxi-header(){ wxi-header(){
if [[ $2 == "h1" ]] if [[ $2 == "h1" ]]
then then
wxi-repeat "\n" 3 #wxi-repeat " " $(($(tput cols)/2-${#1}/2))
echo "=============================="
wxi-repeat " " $((30/2-${#1}/2))
echo -n "$wxiBold" echo -n "$wxiBold"
echo "$1" echo ${1^^}
echo -n "$wxiNormal" echo -n "$wxiNormal"
echo "==============================" echo ""
fi fi
if [[ $2 == "h2" || -z $2 ]] if [[ $2 == "h1*" ]]
then then
wxi-start #wxi-repeat " " $(($(tput cols)/2-${#1}/2))
wxi-repeat " " $((30/2-6/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 -n "$wxiBold"
echo ">> $1 <<" echo ">> $1 <<"
echo -n "$wxiNormal" echo -n "$wxiNormal"
echo "------------------------------"
echo ""
fi fi
if [[ $2 == "h3" ]] if [[ $2 == "h3" ]]
then then
#wxi-repeat " " $(($(tput cols)/2-${#1}/2))
echo -n "$wxiBold" echo -n "$wxiBold"
echo "$1" echo "$1"
echo -n "$wxiNormal" echo -n "$wxiNormal"
fi 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 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 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 then
mkdir -p "$INFRA_PATH/vault" &> /dev/null mkdir -p "$HOME/.warengroup/credentials/ansible-vault" &> /dev/null
curl \ curl \
-H "X-Vault-Token: $TOKEN" \ -H "X-Vault-Token: $TOKEN" \
-X GET \ -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 \ curl \
-H "X-Vault-Token: $TOKEN" \ -H "X-Vault-Token: $TOKEN" \
-X GET \ -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 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-content status "User" "Unsupported"
wxi-footer wxi-footer
wxi-stop wxi-stop
INFRA_PATH="$HOME/.warengroup/infra" INFRA_PATH="$HOME/.warengroup/infra"
INFRA_VAULT="--vault-id warengroup@vault/warengroup" INFRA_VAULT="--vault-id warengroup@$HOME/.warengroup/credentials/ansible-vault/warengroup"
;; ;;
esac esac
@@ -334,7 +461,7 @@ wx-infra(){
then then
echo "Tag Required" echo "Tag Required"
else else
cd "$INFRA_PATH" cd "$INFRA_PATH/ansible"
#git pull &> /dev/null #git pull &> /dev/null
#ansible-galaxy collection install -r requirements.yml --upgrade &> /dev/null #ansible-galaxy collection install -r requirements.yml --upgrade &> /dev/null
@@ -424,7 +551,7 @@ wx-login(){
elif [[ ! -z ${args['token']} ]] elif [[ ! -z ${args['token']} ]]
then then
AUTH_METHOD=token AUTH_METHOD=token
elif [[ $(wxi-config login read) ]] elif [[ $(wxi-config-legacy login read) ]]
then then
AUTH_METHOD=token AUTH_METHOD=token
elif [[ ! -z ${args['username']} ]] elif [[ ! -z ${args['username']} ]]
@@ -478,7 +605,7 @@ wx-login(){
fi fi
TOKEN=$VAULT_LOGIN TOKEN=$VAULT_LOGIN
wxi-config login write wxi-config-legacy login write
;; ;;
token) token)
echo -n "Token: " echo -n "Token: "
@@ -488,9 +615,9 @@ wx-login(){
then then
TOKEN=${args['token']} TOKEN=${args['token']}
fi fi
elif [[ $(wxi-config login read) ]] elif [[ $(wxi-config-legacy login read) ]]
then then
TOKEN=$(wxi-config login read) TOKEN=$(wxi-config-legacy login read)
else else
read -s TOKEN read -s TOKEN
fi fi
@@ -520,7 +647,7 @@ wx-login(){
wxi-content status "Login" "Failed" wxi-content status "Login" "Failed"
wxi-footer wxi-footer
if [[ $(wxi-config login read) ]] if [[ $(wxi-config-legacy login read) ]]
then then
wx-logout &> /dev/null wx-logout &> /dev/null
wx-login wx-login
@@ -530,7 +657,7 @@ wx-login(){
fi fi
TOKEN=$VAULT_LOGIN TOKEN=$VAULT_LOGIN
wxi-config login write wxi-config-legacy login write
;; ;;
*) *)
wxi-content status "Login Type" "Unsupported" wxi-content status "Login Type" "Unsupported"
@@ -576,7 +703,7 @@ wx-logout(){
then then
wxi-header "$ORG_HEADER" h3 wxi-header "$ORG_HEADER" h3
echo "Logging Out..." echo "Logging Out..."
wxi-config login erase wxi-config-legacy login erase
wxi-footer wxi-footer
fi fi
@@ -901,52 +1028,26 @@ wxi-ssh-keys-sync(){
} }
i=1 wxi-args "$@"
while [[ "$1" != "" ]] wxi-config
do
case $1 in
--*)
key="${1%%=*}"
value="${1#*=}"
if [[ "$value" == "$key" ]] echo -n $wxiBold$wxiRed
then cat <<'EOF'
shift
value="$1"
fi
if [[ -z $value ]] (`\ .-') /` ('-. _ .-') ('-. .-') _
then `.( OO ),' ( OO ).-.( \( -O ) _( OO) ( OO ) )
value=true ,--./ .--. / . --. / ,------. (,------.,--./ ,--,'
fi | | | | \-. \ | /`. ' | .---'| \ | |\
| | | |,.-'-' | | | / | | | | | \| | )
| |.'.| |_)\| |_.' | | |_.' |(| '--. | . |/
| | | .-. | | . '.' | .--' | |\ |
| ,'. | | | | | | |\ \ | `---.| | \ |
'--' '--' `--' `--' `--' '--' `------'`--' `--'
args["${key#--}"]="$value" EOF
;; echo -n $wxiNormal
-*)
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
wxi-start
if [[ ! -z ${args['1']} ]] && [[ $(type -t wx-${args['1']}) == function ]] if [[ ! -z ${args['1']} ]] && [[ $(type -t wx-${args['1']}) == function ]]
then then
wx-${args['1']} wx-${args['1']}