Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Windows version #110

11 changes: 9 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ jobs:
files: 'hack/switch/switcher_linux_amd64'
repo-token: ${{ secrets.GITHUB_TOKEN }}
release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}
- name: Upload Windows binaries to release
uses: AButler/upload-release-assets@v2.0
with:
files: 'hack/switch/switcher_windows_amd64.exe'
repo-token: ${{ secrets.GITHUB_TOKEN }}
release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}
- name: Upload switch script to release
uses: AButler/upload-release-assets@v2.0
with:
Expand All @@ -54,11 +60,12 @@ jobs:
mac_sha256sum=$(sha256sum hack/switch/switcher_darwin_amd64 | awk '{print $1}')
mac_arm_sha256sum=$(sha256sum hack/switch/switcher_darwin_arm64 | awk '{print $1}')
linux_sha256sum=$(sha256sum hack/switch/switcher_linux_amd64 | awk '{print $1}')
windows_sha256sum=$(sha256sum hack/switch/switcher_windows_amd64.exe | awk '{print $1}')
switch_script_sha256sum=$(sha256sum hack/switch/switch.sh | awk '{print $1}')

echo '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}'
echo '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "windows_sha": "'"$windows_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}'

curl -X POST https://api.github.com/repos/danielfoehrKn/homebrew-switch/dispatches \
-H 'Accept: application/vnd.github.everest-preview+json' \
-u "$ACCESS_TOKEN" \
--data '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}'
--data '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "windows_sha": "'"$windows_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}'
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ build-switcher:
@env GOOS=linux GOARCH=amd64 go build -ldflags "-w -X github.com/danielfoehrkn/kubeswitch/cmd/switcher.version=${VERSION} -X github.com/danielfoehrkn/kubeswitch/cmd/switcher.buildDate=${DATE}" -o hack/switch/switcher_linux_amd64 ./cmd/main.go
@env GOOS=darwin GOARCH=amd64 go build -ldflags "-w -X github.com/danielfoehrkn/kubeswitch/cmd/switcher.version=${VERSION} -X github.com/danielfoehrkn/kubeswitch/cmd/switcher.buildDate=${DATE}" -o hack/switch/switcher_darwin_amd64 ./cmd/main.go
@env GOOS=darwin GOARCH=arm64 go build -ldflags "-w -X github.com/danielfoehrkn/kubeswitch/cmd/switcher.version=${VERSION} -X github.com/danielfoehrkn/kubeswitch/cmd/switcher.buildDate=${DATE}" -o hack/switch/switcher_darwin_arm64 ./cmd/main.go
@env GOOS=windows GOARCH=amd64 go build -ldflags "-w -X github.com/danielfoehrkn/kubeswitch/cmd/switcher.version=${VERSION} -X github.com/danielfoehrkn/kubeswitch/cmd/switcher.buildDate=${DATE}" -o hack/switch/switcher_windows_amd64.exe ./cmd/main.go

.PHONY: all
all: format check build
Expand Down
6 changes: 4 additions & 2 deletions cmd/switcher/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var (
setName string

completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish]",
Use: "completion [bash|zsh|fish|powershell]",
Short: "generate completion script",
Long: "load the completion script for switch into the current shell",
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish"},
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
root := cmd.Root()
Expand All @@ -42,6 +42,8 @@ var (
return root.GenZshCompletion(os.Stdout)
case "fish":
return root.GenFishCompletion(os.Stdout, true)
case "powershell":
return root.GenPowerShellCompletion(os.Stdout)
}
return fmt.Errorf("unsupported shell type: %s", args[0])
},
Expand Down
86 changes: 84 additions & 2 deletions cmd/switcher/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,94 @@ function kubeswitch
end
printf "%s\n" $RESPONSE
end`

powershellScript string = `
function has_prefix {
param (
[string]$prefix,
[string]$string
)

if ($string.StartsWith($prefix)) {
return $true
} else {
return $false
}
}

function kubeswitch {

#You need to have switcher_windows_amd64.exe in your PATH, or you need to change the value of EXECUTABLE_PATH here
$EXECUTABLE_PATH = "switcher_windows_amd64.exe"

if (-not $args) {
Write-Output "no options provided"
Write-Output $EXECUTABLE_PATH $args
$RESPONSE = & $EXECUTABLE_PATH
}
else{
Write-Output "options provided:" $args
Write-Output $EXECUTABLE_PATH $args
$RESPONSE = & $EXECUTABLE_PATH $args
}

if ($LASTEXITCODE -ne 0 -or -not $RESPONSE) {
Write-Output $RESPONSE
return $LASTEXITCODE
}

# switcher returns a response that contains a kubeconfig path with a prefix "__ " to be able to
# distinguish it from other responses which just need to write to STDOUT
$prefix = "__ "
if (-not (has_prefix $prefix $RESPONSE)) {
Write-Output $RESPONSE
return
}


$RESPONSE = $RESPONSE -replace $prefix, ""
Write-Output $RESPONSE
$remainder = $RESPONSE
Write-Output $remainder
Write-Output $remainder.split(",")[0]
Write-Output $remainder.split(",")[1]
$KUBECONFIG_PATH = $remainder.split(",")[0]
$KUBECONFIG_PATH = $KUBECONFIG_PATH -replace '\\', '/'
$KUBECONFIG_PATH = $KUBECONFIG_PATH -replace "C:", ""
Write-Output $KUBECONFIG_PATH
$SELECTED_CONTEXT = $remainder.split(",")[1]

if (-not $KUBECONFIG_PATH) {
Write-Output $RESPONSE
return
}

if (-not $SELECTED_CONTEXT) {
Write-Output $RESPONSE
return
}

$switchTmpDirectory = "$env:USERPROFILE\.kube\.switch_tmp\config"
if ($env:KUBECONFIG -and $env:KUBECONFIG -like "*$switchTmpDirectory*") {
Remove-Item -Path $env:KUBECONFIG -Force
}

$env:KUBECONFIG = $KUBECONFIG_PATH
Write-Output "switched to context $SELECTED_CONTEXT"
}

#Env variable HOME doesn't exist on windows, we create it from USERPROFILE
$Env:HOME = $Env:USERPROFILE
`
)

var (
initCmd = &cobra.Command{
Use: "init [bash|zsh|fish]",
Use: "init [bash|zsh|fish|powershell]",
Short: "generate init and completion script",
Long: "generate and load the init and completion script for switch into the current shell. Use it like this: 'source <(switcher init zsh)'",
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish"},
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
root := cmd.Root()
Expand All @@ -189,6 +268,9 @@ var (
case "fish":
fmt.Println(fishScript)
return root.GenFishCompletion(os.Stdout, true)
case "powershell":
fmt.Println(powershellScript)
return root.GenPowerShellCompletion(os.Stdout)
}
return fmt.Errorf("unsupported shell type: %s", args[0])
},
Expand Down
8 changes: 8 additions & 0 deletions docs/command_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ echo 'source <(switch completion zsh)' >> ~/.zshrc
```sh
echo 'kubeswitch completion fish | source' >> ~/.config/fish/config.fish
```

### Powershell
```powershell
echo 'kubeswitch completion powershell' >> $PROFILE
echo 'Register-ArgumentCompleter -CommandName ''switcher_windows_amd64'' -ScriptBlock $__switcherCompleterBlock' >> $PROFILE
echo 'Register-ArgumentCompleter -CommandName ''kubeswitch'' -ScriptBlock $__switcherCompleterBlock' >> $PROFILE
. $PROFILE
```
25 changes: 24 additions & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ VERSION=0.8.0 # Pick the current version.
curl -L -o /usr/local/bin/switcher https://github.com/danielfoehrKn/kubeswitch/releases/download/${VERSION}/switcher_${OS}_amd64
chmod +x /usr/local/bin/switcher
```
If you are using Windows, go to the release webpage using you browser and download the windows binary: <https://github.com/danielfoehrKn/kubeswitch/releases/>\
Then copy it to a folder available in your path. To add a folder to your path, you can use the ``Environment Variables`` tool for the Windows' PowerToys: <https://learn.microsoft.com/en-us/windows/powertoys/environment-variables>\
If you need to add a folder to the path for the current powershell session, you can run ``$env:Path += ';C:\myfolder'``



Next, follow [required: source the shell function](#required-source-the-shell-function).

Expand Down Expand Up @@ -84,10 +89,28 @@ function s --wraps switcher
kubeswitch $argv;
end
```
### Powershell
Powershell shell have a built-in `switch` function. Hence, differently from `zsh` shells, the kubeswitch function is called `kubeswitch`.

```powershell
switcher_windows_amd64.exe init powershell >> $PROFILE

# add this for the autocomplete to work
echo 'Register-ArgumentCompleter -CommandName ''switcher_windows_amd64'' -ScriptBlock $__switcherCompleterBlock' >> $PROFILE
echo 'Register-ArgumentCompleter -CommandName ''kubeswitch'' -ScriptBlock $__switcherCompleterBlock' >> $PROFILE

# optionally use alias `s` instead of `kubeswitch` (add to $PROFILE)
echo "" >> $PROFILE
echo "Set-Alias -Name s -Value kubeswitch" >> $PROFILE
echo 'Register-ArgumentCompleter -CommandName ''s'' -ScriptBlock $__switcherCompleterBlock' >> $PROFILE

# source your profile again
. $PROFILE
```

## Check that it works

If you installed kubeswitch correctly, you can run the command `switch` (zsh, bash) or `kubeswitch` (fish) or alternatively the alias `s` from the terminal.
If you installed kubeswitch correctly, you can run the command `switch` (zsh, bash) or `kubeswitch` (fish, powershell) or alternatively the alias `s` from the terminal.
In case the terminal can't find the function, you might need to open another terminal or re-source your config file (`.zshrc`,`.bashrc`,...).

That should display the contexts the tool can find with the default configuration.
Expand Down