Debian Codegraph @

ansible_collections/community/general/tests/integration/targets/connection_wsl/files/wsl.exe

raw
1 #!/usr/bin/env bash
2 # Derived from ../../connection_proxmox_pct_remote/files/pct Copyright (c) 2025 Nils Stein (@mietzen) <[email protected]>
3 # Copyright (c) 2025 Rui Lopes (@rgl) <ruilopes.com>
4 # Copyright (c) 2025 Ansible Project
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 # Shell script to mock wsl.exe behavior
9
10 set -euo pipefail
11
12 function quote_args {
13 local quoted_args=()
14 for arg in "$@"; do
15 if [[ -z "$arg" || "$arg" =~ [^a-zA-Z0-9@%+=:,./-] ]]; then
16 local escaped_arg=${arg//\'/\'\\\'\'}
17 quoted_args+=("'$escaped_arg'")
18 else
19 quoted_args+=("$arg")
20 fi
21 done
22 echo -n "${quoted_args[@]}"
23 }
24
25 declare -a mock_args=()
26 declare -a cmd_args=()
27 wsl_distribution=""
28 wsl_user=""
29
30 while [[ $# -gt 0 ]]; do
31 case $1 in
32 --distribution|-d)
33 wsl_distribution="$2"
34 mock_args+=("$1" "$2")
35 shift 2
36 ;;
37 --user|-u)
38 wsl_user="$2"
39 mock_args+=("$1" "$2")
40 shift 2
41 ;;
42 --)
43 mock_args+=("$1")
44 shift
45 while [[ $# -gt 0 ]]; do
46 mock_args+=("$1")
47 cmd_args+=("$1")
48 shift
49 done
50 ;;
51 *)
52 >&2 echo "unexpected args: $@"
53 exit 1
54 ;;
55 esac
56 done
57
58 mock_cmd="wsl.exe $(quote_args "${mock_args[@]}")"
59 cmd="$(quote_args "${cmd_args[@]}")"
60
61 >&2 echo "[INFO] MOCKING: $mock_cmd"
62 >&2 echo "[INFO] CMD: $cmd"
63
64 tmp_dir="/tmp/ansible-remote/wsl/integration_test/wsl_distribution_${wsl_distribution}"
65
66 mkdir -p "$tmp_dir"
67
68 pushd "$tmp_dir" >/dev/null
69
70 eval "$cmd"
71
72 popd >/dev/null