<commit_before>#!/bin/bash

process_args() {
    # Default
    VAULT_CERT_PATH="certs"

    COMMAND=$1

    if [[ "$COMMAND" == "certonly" || "$COMMAND" == "renew" ]]; then
        echo ""
    else
        echo "letsencrypt-to-vault: Wrong command"
        echo
        show_help
        exit 1
    fi

    shift

    while [[ $# -gt 0 ]]
    do
        key="$1"

        case $key in
            -h|--help)
            show_help
            exit 0
            ;;
            -t|--vault-token)
            VAULT_TOKEN="$2"
            shift
            ;;
            -a|--vault-addr)
            VAULT_ADDR="$2"
            shift
            ;;
            -p|--vault-cert-path)
            VAULT_CERT_PATH="$2"
            shift
            ;;
            -f|--certbot-flags)
            CERTBOT_FLAGS="$2"
            shift
            -*|--*)
            echo "letsencrypt-to-vault: unknown flag: $2"
            show_help
            exit 1
            ;;
            *)
            if [[ "$COMMAND" == "certonly" ]]; then
                DOMAINS=$@
            fi
            return
        esac
    done
}

show_help() {
    echo "Let's encrypt to Hashicorp Vault"
    echo
    echo "Renew or get Let's Encrypt certificates and send it to Hashicorp Vault" 
    echo
    echo "Usage:"
    echo "  letsencrypt-to-vault command [-flags] [sitesnames]"
    echo ""
}

cert_renew() {
    echo
}

send_to_vault() { 
    local certs_dir="/etc/letsencrypt/live"

    if [[ $COMMAND == "certonly" ]]; then
        local sitesnames="$DOMAINS"
    else
        local sitesnames=$(find $certs_dir -maxdepth 1 -mindepth 1 -type d -exec basename {} \;)
    fi

    for sitename in $sitenames
    do
        local cert=$(cat $certs_dir/$sitename/fullchain.pem)
        local privkey=$(cat $certs_dir/$sitename/privkey.pem)

        curl \
            -H "X-Vault-Token: $VAULT_TOKEN" \
            -H "Content-Type: application/json" \
            -X POST \
            -d "{\"key\":\"$privkey\", \"cert\": \"$cert\"}" \
            "http://$VAULT_ADDR/v1/secret/$VAULT_CERT_PATH/$sitename"
    done
}<commit_msg>Add info log<commit_after>#!/bin/bash

process_args() {
    # Default
    VAULT_CERT_PATH="certs"

    COMMAND=$1

    if [[ "$COMMAND" == "certonly" || "$COMMAND" == "renew" ]]; then
        echo ""
    else
        echo "letsencrypt-to-vault: Wrong command"
        echo
        show_help
        exit 1
    fi

    shift

    while [[ $# -gt 0 ]]
    do
        key="$1"

        case $key in
            -h|--help)
            show_help
            exit 0
            ;;
            -t|--vault-token)
            VAULT_TOKEN="$2"
            shift
            ;;
            -a|--vault-addr)
            VAULT_ADDR="$2"
            shift
            ;;
            -p|--vault-cert-path)
            VAULT_CERT_PATH="$2"
            shift
            ;;
            -f|--certbot-flags)
            CERTBOT_FLAGS="$2"
            shift
            -*|--*)
            echo "letsencrypt-to-vault: unknown flag: $2"
            show_help
            exit 1
            ;;
            *)
            if [[ "$COMMAND" == "certonly" ]]; then
                DOMAINS=$@
            fi
            return
        esac
    done
}

show_help() {
    echo "Let's encrypt to Hashicorp Vault"
    echo
    echo "Renew or get Let's Encrypt certificates and send it to Hashicorp Vault" 
    echo
    echo "Usage:"
    echo "  letsencrypt-to-vault command [-flags] [sitesnames]"
    echo ""
}

cert_renew() {
    echo "Trying to get or renew certificates"
}

send_to_vault() { 
    local certs_dir="/etc/letsencrypt/live"

    echo "Sending certs to vault..."
    echo

    if [[ $COMMAND == "certonly" ]]; then
        local sitesnames="$DOMAINS"
    else
        local sitesnames=$(find $certs_dir -maxdepth 1 -mindepth 1 -type d -exec basename {} \;)
    fi

    for sitename in $sitenames
    do
        local cert=$(cat $certs_dir/$sitename/fullchain.pem)
        local privkey=$(cat $certs_dir/$sitename/privkey.pem)

        curl \
            -H "X-Vault-Token: $VAULT_TOKEN" \
            -H "Content-Type: application/json" \
            -X POST \
            -d "{\"key\":\"$privkey\", \"cert\": \"$cert\"}" \
            "http://$VAULT_ADDR/v1/secret/$VAULT_CERT_PATH/$sitename"
    done

    echo "Done. Certificates were sent"
}
{'fraction_non_alphanumeric': 0.13385645049619202, 'fraction_numerical': 0.006462035541195477, 'mean_word_length': 1.3891951488423373, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1037486', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1397, 'n_words': 307}