# shellcheck shell=dash
# Section: index
___x_cmd_gcode_user_gpg(){
    param:scope     ___x_cmd_gcode
    param:subcmd ___x_cmd_gcode_user_gpg      \
        "list|ls"                 "Get a list of the authenticated user’s GPG keys"       \
        get                       "Get a specific GPG key of authenticated user"          \
        add                       "Creates a new GPG key owned by the authenticated user" \
        rm                        "Deletes a GPG key owned by the authenticated user"
    param:subcmd:try
    param:run

    ___x_cmd_gcode_user_gpg _param_help_doc
    return 1
}
# EndSection
# Section: list
___x_cmd_gcode_user_gpg_list(){
    param:scope     ___x_cmd_gcode
    param:dsl       '
options:

    --json|-j       "output origin json data"
'
    param:run
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gcode_curl get "/user/gpg_keys"
    else
        ___x_cmd_gcode_curl get "/user/gpg_keys" | \
            x jo 2c              .id  .created_at .key  | \
            x csv header --add   ID   CREATED_AT KEY    | \
            x csv static_tab
    fi
}
# EndSection
# Section: get
___x_cmd_gcode_user_gpg_get(){
    param:scope     ___x_cmd_gcode
    param:dsl       '
options:
    #1              "Get a specific GPG key of authenticated user"        <>

    --json|-j       "output origin json data"
'
    param:run
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gcode_curl get "/user/gpg_keys/${1}"
    else
        ___x_cmd_gcode_curl get "/user/gpg_keys/${1}" | \
            x ja  jl2c           .id  .key .created_at | \
            x csv header --add   ID   KEY  CREATED_AT  | \
            x csv static_tab
    fi
}
# EndSection
# Section: add
___x_cmd_gcode_user_gpg_add(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "New GPG key"                   <>

    --json|-j       "output origin json data"
'
    param:run
    local gen_gcode_json
    # gen_gcode_json="$(param:option2json -repo)"
    gcode:debug "$gen_gcode_json"
    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_gcode_curl post "/user/gpg_keys" "gen_gcode_json"
    else
        ___x_cmd_gcode_curl post "/user/gpg_keys"  "gen_gcode_json"| (
        x jo env . gcode_resp_err=.error gl_resp_msg=.message
        if ___x_cmd_gcode_http_error; then
            ___x_cmd_ui_tf  true "[Success]: Add GPG key"
        else
            ___x_cmd_ui_tf false "[failure] Add GPG key failure"
            ___x_cmd_gcode____handle_resp
            return 1
        fi
    )
    fi

}
# EndSection
# Section: rm
___x_cmd_gcode_user_gpg_rm(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "ID of the GPG key"                       <>
    --yes|-y        "Ignore remove prompt interception"


    --json|-j       "output origin json data"
'
    param:run
    [ "$yes" = "true" ] || ___x_cmd_ui_yesno "Are you sure you want to remove the hook $1 in the $owner_repo ?" || return

    ___x_cmd_gcode_curl del "/user/gpg_keys/${1}" | (
        x jo env . gcode_resp_err=.error gl_resp_msg=.message
        if ___x_cmd_gcode_http_error; then
            ___x_cmd_ui_tf  true "[Success]: Delete GPG key: $1"
        else
            ___x_cmd_ui_tf false "[failure] Delete GPG key: $1 failure:"
            ___x_cmd_gcode____handle_resp
            return 1
        fi
    )
}
# EndSection
