Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ jobs:
go.mod
sdk/go.mod

# Only the installer runs. The rest of the acceptance suite is a Linux gate
# by design, and running it here would be proving something else.
# Only the installer and its removal run. The rest of the acceptance suite is
# a Linux gate by design, and running it here would be proving something else.
- name: Drive the Windows installer
run: go test ./test/acceptance/ -run TestInstallScript -count=1 -v
run: go test ./test/acceptance/ -run 'TestInstallScript|TestUninstall' -count=1 -v

acceptance:
name: Architecture Proof
Expand Down
122 changes: 122 additions & 0 deletions scripts/uninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

<#
.SYNOPSIS
Removes what scripts/install.ps1 added.

.DESCRIPTION
Removes the binary, the directory the installer created for it, the per-user PATH
entry, and the per-user WSO2_HOME variable. It does not remove configuration,
contexts, or credentials unless -Purge is given: removing a binary is not the
same decision as abandoning a setup.

Running it when nothing is installed is not a failure. It reports what it found
and exits successfully, which is also what makes it usable to clean up after an
install that failed halfway.

Nothing here needs administrator rights.

.PARAMETER Purge
Also remove configuration, contexts, and credentials.
#>
param(
[switch] $Purge
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

$stateRoot = if ($env:WSO2_HOME) { $env:WSO2_HOME } else { Join-Path $HOME '.wso2' }
$binDir = Join-Path $stateRoot 'bin'
$removed = $false

$installed = Join-Path $binDir 'wso2.exe'
if (Test-Path -LiteralPath $installed) {
try {
Remove-Item -LiteralPath $installed -Force
} catch {
[Console]::Error.WriteLine("error: could not remove ${installed}: $($_.Exception.Message). Close any running wso2 and try again.")
exit 1
}
Write-Output "Removed $installed"
$removed = $true
}

# Any staging file an interrupted install left beside the binary.
Get-ChildItem -LiteralPath $binDir -Filter '.wso2.install.*' -Force -ErrorAction SilentlyContinue |
ForEach-Object { Remove-Item -LiteralPath $_.FullName -Force -ErrorAction SilentlyContinue }

# Only if it is empty. A directory holding something this installer did not put
# there is not this script's to delete.
if ((Test-Path -LiteralPath $binDir) -and
-not (Get-ChildItem -LiteralPath $binDir -Force -ErrorAction SilentlyContinue)) {
Remove-Item -LiteralPath $binDir -Force
Write-Output "Removed $binDir"
}
Comment thread
kanushka marked this conversation as resolved.

# The PATH entry, matched the way the installer wrote it: case-insensitively and
# ignoring a trailing separator, so the entry is found however it was recorded.
# Every other entry is written back exactly as it was.
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
if ($userPath) {
$target = $binDir.TrimEnd('\')
$kept = @()
$dropped = 0
foreach ($entry in $userPath -split ';') {
if ($entry.Trim() -and $entry.Trim().TrimEnd('\') -ieq $target) {
$dropped++
} else {
$kept += $entry
}
}
if ($dropped -gt 0) {
[Environment]::SetEnvironmentVariable('Path', ($kept -join ';'), 'User')
Write-Output "Removed $binDir from your user PATH."
$removed = $true
}
}

# Only when it is this state root. A WSO2_HOME pointing somewhere else was set by
# someone for a reason, and clearing it would be removing a decision that is not
# this script's to reverse.
$userStateRoot = [Environment]::GetEnvironmentVariable('WSO2_HOME', 'User')
if ($userStateRoot -and $userStateRoot.TrimEnd('\') -ieq $stateRoot.TrimEnd('\')) {
[Environment]::SetEnvironmentVariable('WSO2_HOME', $null, 'User')
Write-Output 'Removed the user WSO2_HOME variable.'
$removed = $true
}

if ($Purge) {
if (Test-Path -LiteralPath $stateRoot) {
Remove-Item -LiteralPath $stateRoot -Recurse -Force
Write-Output "Removed $stateRoot, including configuration and credentials."
$removed = $true
}
} elseif (Test-Path -LiteralPath $stateRoot) {
# Named explicitly rather than left implicit: someone who wanted everything
# gone needs to know that something is still there and how to remove it.
Write-Output ''
Write-Output "Left $stateRoot in place, with your contexts and credentials."
Write-Output 'Remove it too with: .\uninstall.ps1 -Purge'
}

if (-not $removed) {
Write-Output "Nothing to remove: no wso2 installation was found under $stateRoot."
} else {
Write-Output ''
Write-Output 'Open a new terminal so the PATH change takes effect.'
}
116 changes: 116 additions & 0 deletions scripts/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/usr/bin/env bash
# Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# Removes what scripts/install.sh added on macOS, Linux, and WSL.
#
# bash uninstall.sh # remove the binary and the profile block
# bash uninstall.sh --purge # also remove configuration and credentials
#
# It removes the binary, the directory the installer created for it, and the
# delimited block the installer appended to a shell profile. It does not remove
# configuration, contexts, or credentials unless asked: removing a binary is not
# the same decision as abandoning a setup, and silently destroying the second
# would be the worse default.
#
# Running it when nothing is installed is not a failure. It reports what it found
# and exits successfully, which is also what makes it usable to clean up after an
# install that failed halfway.
#
# The block markers and paths here must match scripts/install.sh exactly. They are
# repeated rather than shared because each script is fetched and run on its own,
# so neither can source the other.

set -euo pipefail

BLOCK_BEGIN='# >>> wso2 cli >>>'
BLOCK_END='# <<< wso2 cli <<<'

PURGE=0
for argument in "$@"; do
case "$argument" in
--purge) PURGE=1 ;;
-h | --help)
printf 'Usage: uninstall.sh [--purge]\n\n'
printf ' --purge Also remove configuration, contexts, and credentials.\n'
exit 0
;;
*)
printf 'error: unknown option: %s\n' "$argument" >&2
exit 1
;;
esac
done

state_root="${WSO2_HOME:-$HOME/.wso2}"
bin_dir="${state_root}/bin"
removed=0

# The binary and any staging file an interrupted install left beside it.
if [ -e "${bin_dir}/wso2" ]; then
rm -f "${bin_dir}/wso2"
printf 'Removed %s\n' "${bin_dir}/wso2"
removed=1
fi
rm -f "${bin_dir}"/.wso2.install.* 2>/dev/null || true

# Only if it is empty. A directory holding something this installer did not put
# there is not this script's to delete.
if [ -d "$bin_dir" ] && [ -z "$(ls -A "$bin_dir" 2>/dev/null)" ]; then
rmdir "$bin_dir"
printf 'Removed %s\n' "$bin_dir"
fi
Comment thread
kanushka marked this conversation as resolved.

# Every profile is checked, not only the one this shell would be wired in: the
# install may have run under a different shell, and a block left behind would go
# on putting a directory that no longer exists on PATH.
for profile in "$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.zshrc" "$HOME/.zprofile" "$HOME/.profile"; do
[ -f "$profile" ] || continue
grep -qF "$BLOCK_BEGIN" "$profile" || continue

staged="${profile}.wso2-uninstall.$$"
# Only the lines between the markers go. Everything else is written back
# byte for byte, through a temporary file beside the profile so an interrupted
# run cannot truncate it.
awk -v begin="$BLOCK_BEGIN" -v end="$BLOCK_END" '
$0 == begin { inside = 1; next }
$0 == end { inside = 0; next }
!inside { print }' "$profile" >"$staged"
mv "$staged" "$profile"
Comment thread
kanushka marked this conversation as resolved.
printf 'Removed the wso2 block from %s\n' "$profile"
removed=1
done

if [ "$PURGE" -eq 1 ]; then
if [ -d "$state_root" ]; then
rm -rf "$state_root"
printf 'Removed %s, including configuration and credentials.\n' "$state_root"
removed=1
fi
else
# Named explicitly rather than left implicit: someone who wanted everything
# gone needs to know that something is still there and how to remove it.
if [ -d "$state_root" ]; then
printf '\nLeft %s in place, with your contexts and credentials.\n' "$state_root"
printf 'Remove it too with: bash uninstall.sh --purge\n'
fi
fi

if [ "$removed" -eq 0 ]; then
printf 'Nothing to remove: no wso2 installation was found under %s.\n' "$state_root"
else
printf '\nOpen a new terminal so the PATH change takes effect.\n'
fi
44 changes: 26 additions & 18 deletions test/acceptance/install_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,31 @@ func TestInstallScriptHonoursTheStateRootVariable(t *testing.T) {
func (i *installHarness) run(args ...string) (string, string, error) {
i.t.Helper()

script := filepath.Join(repoRoot(i.t), installScriptRelPath)
arguments := []string{
"-NoLogo", "-NoProfile", "-NonInteractive",
"-ExecutionPolicy", "Bypass",
"-File", script,
}
command := exec.Command(powerShell(), append(arguments, args...)...)
command.Env = i.scriptEnvironment()

var stdout, stderr strings.Builder
command.Stdout = &stdout
command.Stderr = &stderr
err := command.Run()
return stdout.String(), stderr.String(), err
}

// scriptEnvironment is what either script is given: everything it could reach
// outside the test redirected, and nothing inherited that could let it out.
//
// It is built from scratch rather than from the process environment, so an
// ambient WSO2_ variable cannot reach a run. The Windows API needs SystemRoot and
// a temp directory to function at all, and PowerShell itself is found on PATH.
func (i *installHarness) scriptEnvironment() []string {
i.t.Helper()

// Captured before the first run rather than in the constructor, so a test that
// reads these values first still records what was there originally.
if i.platform.savedUserEnvironment == nil {
Expand All @@ -264,17 +289,6 @@ func (i *installHarness) run(args ...string) (string, string, error) {
}
}

script := filepath.Join(repoRoot(i.t), installScriptRelPath)
arguments := []string{
"-NoLogo", "-NoProfile", "-NonInteractive",
"-ExecutionPolicy", "Bypass",
"-File", script,
}
command := exec.Command(powerShell(), append(arguments, args...)...)

// Built from scratch rather than inherited, so an ambient WSO2_ variable
// cannot reach the run. The Windows API needs SystemRoot and a temp directory
// to function at all, and PowerShell itself is found on PATH.
environment := []string{
"USERPROFILE=" + i.home,
"HOME=" + i.home,
Expand Down Expand Up @@ -302,13 +316,7 @@ func (i *installHarness) run(args ...string) (string, string, error) {
}
}
}
command.Env = append(environment, i.environment...)

var stdout, stderr strings.Builder
command.Stdout = &stdout
command.Stderr = &stderr
err := command.Run()
return stdout.String(), stderr.String(), err
return append(environment, i.environment...)
}

// powerShell reports the interpreter to drive the script with, preferring
Expand Down
Loading