#!/bin/bash

function cmd_ti_mycli_raw()
{
	local ti_file="${1}"
	local ti_args="${2}"
	local cmd_mod_names="${3}"
	local cmd_hosts="${4}"
	local cmd_indexes="${5}"
	local mods="${6}"
	shift 6

	local mod=`from_mods_random_mod "${mods}" 'tidb' 'false'`
	if [ -z "${mod}" ]; then
		echo "[cmd mysql/cli] no tidb selected" >&2
		return 1
	fi

	local dir=`from_mod_get_dir "${mod}"`
	local host=`from_mod_get_host "${mod}"`

	local port=`ssh_get_value_from_proc_info "${host}" "${dir}" 'tidb_port'`
	if [ -z "${host}" ]; then
		local host=`must_print_ip`
	fi

	if [ -z "${port}" ]; then
		echo "[cmd mysql/cli] getting tidb port failed from ${host}:${dir}" >&2
		return 1
	fi
	local error_handle="$-"
	set +eu
	local cmd_args=("${@}")
	restore_error_handle_flags "${error_handle}"
	if [ -z "${cmd_args+x}" ]; then
        mycli --no-warn -h "${host}" -P "${port}" -u root
	else
        mycli --no-warn -h "${host}" -P "${port}" -u root "${cmd_args[@]}"
	fi
}

set -euo pipefail
cmd_ti_mycli_raw "${@}"
