-
Notifications
You must be signed in to change notification settings - Fork 15
/
common.env
95 lines (82 loc) · 2.35 KB
/
common.env
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
: ${no_docker=0}
: ${no_start=0}
: ${on_vpn=0}
: ${WIN_BIN=$(wslpath "C:\bin")}
: ${WSLBIN_URL=https://github.com/AmmarRahman/wsl-vpn/releases/latest/download/wslbin.tar.gz}
: ${NPIPRELAY_URL=https://github.com/jstarks/npiperelay/releases/download/v0.1.0/npiperelay_windows_amd64.zip}
: ${DOCKER_WSL=$(wslpath "C:\Program Files\Docker\Docker\resources")}
: ${SYSTEM_ROOT=$(wslpath "C:\Windows")}
# Download even if WSL vpn is not patched yet
function download_ps()
{
# $1 - Url
# $2 - Download file (relative to cwd)
"${SYSTEM_ROOT}/system32/WindowsPowerShell/v1.0/powershell.exe" -NoProfile -Command "Invoke-WebRequest -Uri \"$1\" -OutFile \"$2\""
}
function unzip_ps()
{
# $1 - Archive name, current dir
# $2 - filename to extract
# Extract-Archive extracts all files :(
"${SYSTEM_ROOT}/system32/WindowsPowerShell/v1.0/powershell.exe" -NoProfile -Command '
Add-Type -AssemblyName System.IO.Compression.FileSystem
$cwd = $pwd -replace "^Microsoft\.PowerShell\.Core\\FileSystem::"
$zip = [System.IO.Compression.ZipFile]::OpenRead("$cwd\'"${1}"'")
$zip.entries | Where-Object { $_.FullName -eq "'"${2}"'" } | ForEach-Object {
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$cwd\'"${2}"'", $true)
}
'
}
function extract_from_iso_ps()
{
# $1 - iso name
# $2 - filename path in iso
# $3 - destination filename
# Extract-Archive extracts all files :(
"${SYSTEM_ROOT}/system32/WindowsPowerShell/v1.0/powershell.exe" -NoProfile -Command '
try
{
$iso = Mount-DiskImage -ImagePath "'"$(wslpath -w "${1}")"'" -NoDriveLetter
Copy-Item "$($iso.DevicePath)\'"${2}"'" "'"${3}"'"
}
finally
{
$null = Dismount-DiskImage -DevicePath $iso.DevicePath
}
'
}
# Get the parent pid, in a way that should work on any WSL distro
function ppid()
{
sed -n "s|PPid:\s*||p" /proc/$1/status
}
# This function check if the file exist then only append the line once
function write_to_file()
{
if [ -e "$2" ]; then
grep -qFs "$1" "$2" || echo "$1" | tee -a "$2"
fi
}
function rm_if()
{
local file_name="${1}"
if [ -e "${file_name}" ]; then
if ! rm "${file_name}"; then
shift 1
"${@}"
rm "${file_name}"
fi
fi
}
function rmdir_if()
{
if [ -d "${1}" ]; then
rmdir "${1}"
fi
}
function sed_file()
{
if [ -e "${2}" ]; then
sed -i "${1}" "${2}"
fi
}