-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathwslview.sh
98 lines (90 loc) · 3.43 KB
/
wslview.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# shellcheck shell=bash
lname=""
skip_validation_check=${WSLVIEW_SKIP_VALIDATION_CHECK:-1}
help_short="$0 [-hsvur]\n$0 [-E ENGINE] LINK/FILE"
function del_reg_alt {
if [ "$distro" == "archlinux" ] || [ "$distro" == "alpine" ]; then
error_echo "Unsupported action for this distro. Aborted." 34
exit 34
else
sudo update-alternatives --remove x-www-browser "$(readlink -f "$0")"
sudo update-alternatives --remove www-browser "$(readlink -f "$0")"
exit
fi
}
function add_reg_alt {
if [ "$distro" == "archlinux" ] || [ "$distro" == "alpine" ]; then
error_echo "Unsupported action for this distro. Aborted." 34
else
sudo update-alternatives --install "$wslu_prefix"/bin/x-www-browser x-www-browser "$(readlink -f "$0")" 1
sudo update-alternatives --install "$wslu_prefix"/bin/www-browser www-browser "$(readlink -f "$0")" 1
exit
fi
}
function url_validator {
content=$(curl --head --silent -g "$*" | head -n 1)
if [ -n "$content" ]; then
return 0
else
return 1
fi
}
while [ "$1" != "" ]; do
case "$1" in
-s|--skip-validation-check) skip_validation_check=0; shift;;
-r|--reg-as-browser) add_reg_alt;;
-u|--unreg-as-browser) del_reg_alt;;
-h|--help) help "$0" "$help_short"; exit;;
-v|--version) version; exit;;
-E|--engine) shift; WSLVIEW_DEFAULT_ENGINE="$1"; shift;;
*) lname="$*";break;;
esac
done
debug_echo "lname: $lname"
debug_echo "WSLVIEW_DEFAULT_ENGINE: $WSLVIEW_DEFAULT_ENGINE"
if [[ "$lname" != "" ]]; then
wslutmpbuild=$(wslu_get_build)
# file:/// protocol used in linux
if [[ "$lname" =~ ^file:\/\/.*$ ]] && [[ ! "$lname" =~ ^file:\/\/(\/)+[A-Za-z]\:.*$ ]]; then
debug_echo "Received file:/// protocol used in linux"
[ "$wslutmpbuild" -ge "$BN_MAY_NINETEEN" ] || error_echo "This protocol is not supported before version 1903." 34
properfile_full_path="$(readlink -f "${lname//file:\/\//}")"
# Linux absolute path
elif [[ "$lname" =~ ^(/[^/]+)*(/)?$ ]]; then
debug_echo "Received linux absolute path"
[ "$wslutmpbuild" -ge "$BN_MAY_NINETEEN" ] || error_echo "This protocol is not supported before version 1903." 34
properfile_full_path="$(readlink -f "${lname}")"
# Linux relative path
elif [[ -d "$(readlink -f "$lname")" ]] || [[ -f "$(readlink -f "$lname")" ]]; then
debug_echo "Received linux relative path"
[ "$wslutmpbuild" -ge "$BN_MAY_NINETEEN" ] || error_echo "This protocol is not supported before version 1903." 34
properfile_full_path="$(readlink -f "${lname}")"
fi
debug_echo "properfile_full_path: $properfile_full_path"
debug_echo "validating whether if it is a link"
is_valid_url=$(url_validator "$lname")
if [ "$skip_validation_check" -eq 0 ]; then
debug_echo "Skipping validation check"
is_valid_url=0
fi
if [[ "$is_valid_url" -eq 0 ]] && [ -z "$properfile_full_path" ]; then
debug_echo "It is a link"
cmd="\"$lname\""
elif [[ "$lname" =~ ^file:\/\/(\/)+[A-Za-z]\:.*$ ]] || [[ "$lname" =~ ^[A-Za-z]\:.*$ ]]; then
debug_echo "It is not a link; received windows absolute path/file protocol windows absolute path"
cmd="\"$lname\""
else
debug_echo "It is not a link"
cmd="\"$(wslpath -w "${properfile_full_path:-$lname}" 2>/dev/null || echo "$lname")\""
fi
debug_echo "cmd: $cmd"
if [[ "$WSLVIEW_DEFAULT_ENGINE" == "powershell" ]]; then
winps_exec Start "${cmd}"
elif [[ "$WSLVIEW_DEFAULT_ENGINE" == "cmd" ]]; then
cmd_exec start "${cmd}"
elif [[ "$WSLVIEW_DEFAULT_ENGINE" == "cmd_explorer" ]]; then
cmd_exec explorer.exe "${cmd}"
fi
else
error_echo "No input, aborting" 21
fi