Skip to content

Commit a724fdf

Browse files
authored
add pwsh script to run archived image. (#1552)
1 parent 38abbf9 commit a724fdf

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<#
2+
.SYNOPSIS
3+
4+
Run GPSBabel GUI or CLI.
5+
6+
.DESCRIPTION
7+
8+
A docker container containing the GPSBabel GUI and CLI is created and the GUI or CLI is run.
9+
It is assumed you are running on Windows, Docker Desktop is installed and has been started,
10+
and WSL 2 is installed.
11+
Your Desktop is available in the container /app directory. This container working directory
12+
will be automatically set to /app.
13+
14+
.PARAMETER version
15+
Specify the version of GPSBabel.
16+
17+
.PARAMETER cli
18+
A double quoted string of space separated parameters to pass to the gpsbabel CLI.
19+
Omit this parameter to run the gpsbabel GUI.
20+
21+
.PARAMETER list
22+
This option will list the related containers and images for all versions that exist
23+
locally. This option precludes running the GUI or CLI.
24+
25+
.PARAMETER clean
26+
This option will stop and remove an existing container for the given version and
27+
delete the corresponding image. This will save space but it will take longer
28+
to use the image the next time. This option precludes running the GUI or CLI.
29+
30+
.EXAMPLE
31+
.\run_gpsbabel.ps1
32+
Runs the gpsbabel GUI.
33+
34+
.EXAMPLE
35+
.\run_gpsbabel.ps1 -cli "-D 1 -i gpx -f cp.gpx -o kml -F junk.kml"
36+
Runs the gpsbabel CLI with the command line parameters pass as the -cli option.
37+
38+
.EXAMPLE
39+
.\run_gpsbabel.ps1 -list
40+
Will list any existing related containers and images.
41+
42+
.EXAMPLE
43+
.\run_gpsbabel.ps1 -clean
44+
Stop and delete the container and delete the image (for the specified version).
45+
46+
.LINK
47+
https://hub.docker.com/r/tsteven4/gpsbabel
48+
49+
#>
50+
51+
param (
52+
[ValidateSet("1.5.0", "1.5.1", "1.5.2", "1.5.3", "1.5.4", "1.6.0", "1.7.0", "1.8.0", "1.9.0", "1.10.0", "latest", "dev")]
53+
[string] $version = "latest",
54+
55+
[string] $cli = "",
56+
57+
[switch] $list,
58+
59+
[switch] $clean
60+
)
61+
62+
# we use a persistent container so the gpsbabel GUI state is saved and restored (in the container).
63+
$ContainerName = "tsteven4_gpsbabel_${version}"
64+
$ImageName = "tsteven4/gpsbabel:${version}"
65+
66+
if ($list) {
67+
Write-Output "Containers:"
68+
docker ps --all --filter "name=tsteven4_gpsbabel_*" --format "table {{.Names}}\t{{.Image}}\t{{.Size}}"
69+
Write-Output ""
70+
Write-Output "Images:"
71+
docker image ls --all --filter "reference=tsteven4/gpsbabel:*" --format "table {{.Repository}}:{{.Tag}}\t{{.Size}}"
72+
}
73+
if ($clean) {
74+
Write-Output "Checking container ${ContainerName}"
75+
if (docker ps --all --quiet --filter "status=running" --filter "name=^${ContainerName}$") {
76+
Write-Output " Stopping container ${ContainerName}"
77+
docker stop ${ContainerName} | Out-Null
78+
}
79+
if (docker ps --all --quiet --filter "name=^${ContainerName}$") {
80+
Write-Output " Deleting container ${ContainerName}"
81+
docker rm ${ContainerName} | Out-Null
82+
}
83+
Write-Output "Checking image ${ImageName}"
84+
if (docker image ls --all --quiet ${ImageName}) {
85+
Write-Output " Deleting image ${ImageName}"
86+
docker image rm ${ImageName} | Out-Null
87+
}
88+
}
89+
if (-not $list -and -not $clean) {
90+
if (-not (docker ps --all --quiet --filter "name=^${ContainerName}$")) {
91+
Write-Output "Creating container ${ContainerName} from image ${ImageName}"
92+
$DesktopPath = [Environment]::GetFolderPath("Desktop")
93+
$cultureName = (Get-Culture).Name
94+
# Create the Docker container
95+
docker container create --quiet --interactive --tty `
96+
--name ${ContainerName} `
97+
--volume /run/desktop/mnt/host/wslg/.X11-unix:/tmp/.X11-unix `
98+
--volume /run/desktop/mnt/host/wslg:/mnt/wslg `
99+
--env DISPLAY=:0 `
100+
--env WAYLAND_DISPLAY=wayland-0 `
101+
--env XDG_RUNTIME_DIR=/mnt/wslg/runtime-dir `
102+
--env PULSE_SERVER=/mnt/wslg/PulseServer `
103+
--env LANG=${cultureName} `
104+
--workdir /app `
105+
--volume ${DesktopPath}:/app `
106+
--user 1000:1000 `
107+
"tsteven4/gpsbabel:${version}" | Out-Null
108+
}
109+
110+
# If necessary, start the container
111+
if (-not (docker ps --all --quiet --filter "status=running" --filter "name=^${ContainerName}$")) {
112+
docker start ${ContainerName} | Out-Null
113+
}
114+
115+
if (-not ${cli}) {
116+
# Run the gpsbabel GUI
117+
docker exec --detach --interactive --tty ${ContainerName} gpsbabelfe
118+
}
119+
else {
120+
# Run the gpsbabel CLI
121+
$babelargs = $cli -split ' '
122+
Write-Output "gpsbabel $babelargs"
123+
docker exec --interactive --tty ${ContainerName} gpsbabel @babelargs
124+
}
125+
}

0 commit comments

Comments
 (0)