Added kubectx installer
authorMichael Paesold <mpaesold@gmail.com>
Fri, 8 Jan 2021 14:53:39 +0000 (15:53 +0100)
committerAJ ONeal <aj@therootcompany.com>
Sat, 9 Jan 2021 20:27:21 +0000 (20:27 +0000)
See https://github.com/ahmetb/kubectx

Implementation for #173

Collaborative work of Nico Kerschbaumer and me

kubectx/README.md [new file with mode: 0644]
kubectx/install.ps1 [new file with mode: 0644]
kubectx/install.sh [new file with mode: 0644]
kubectx/releases.js [new file with mode: 0644]

diff --git a/kubectx/README.md b/kubectx/README.md
new file mode 100644 (file)
index 0000000..2b40f83
--- /dev/null
@@ -0,0 +1,38 @@
+---
+title: Kubectx
+homepage: https://github.com/ahmetb/kubectx
+tagline: |
+  kubectx: kubectx is a utility to manage and switch between kubectl contexts.
+---
+
+### Updating `kubectx`
+
+`webi kubectx@stable`
+
+Use the `@beta` tag for pre-releases.
+
+## Cheat Sheet
+
+> `kubectx` kubectx helps you switch between Kubernetes clusters back and forth
+
+To run kubectx:
+
+```bash
+kubectx
+```
+
+### Command line arguments
+
+```bash
+USAGE:
+  kubectx                   : list the contexts
+  kubectx <NAME>            : switch to context <NAME>
+  kubectx -                 : switch to the previous context
+  kubectx -c, --current     : show the current context name
+  kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
+  kubectx <NEW_NAME>=.      : rename current-context to <NEW_NAME>
+  kubectx -d <NAME>         : delete context <NAME> ('.' for current-context)
+                              (this command won't delete the user/cluster entry
+                              that is used by the context)
+  kubectx -u, --unset       : unset the current context
+```
diff --git a/kubectx/install.ps1 b/kubectx/install.ps1
new file mode 100644 (file)
index 0000000..962c9d5
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/env pwsh
+
+###################
+# Install kubectx #
+###################
+
+# Every package should define these variables
+$pkg_cmd_name = "kubectx"
+
+$pkg_dst_cmd = "$Env:USERPROFILE\.local\bin\kubectx.exe"
+$pkg_dst = "$pkg_dst_cmd"
+
+$pkg_src_cmd = "$Env:USERPROFILE\.local\opt\kubectx-v$Env:WEBI_VERSION\bin\kubectx.exe"
+$pkg_src_bin = "$Env:USERPROFILE\.local\opt\kubectx-v$Env:WEBI_VERSION\bin"
+$pkg_src_dir = "$Env:USERPROFILE\.local\opt\kubectx-v$Env:WEBI_VERSION"
+$pkg_src = "$pkg_src_cmd"
+
+$pkg_download = "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE"
+
+# Fetch archive
+IF (!(Test-Path -Path "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE"))
+{
+    # TODO: arch detection
+    echo "Downloading kubectx from $Env:WEBI_PKG_URL to $pkg_download"
+    & curl.exe -A -L "$Env:WEBI_UA" -fsSL "$Env:WEBI_PKG_URL" -o "$pkg_download.part"
+    & move "$pkg_download.part" "$pkg_download"
+}
+
+IF (!(Test-Path -Path "$pkg_src_cmd"))
+{
+    echo "Installing kubectx"
+
+    # TODO: create package-specific temp directory
+    # Enter tmp
+    pushd .local\tmp
+
+        # Remove any leftover tmp cruft
+        Remove-Item -Path ".\kubectx-v*" -Recurse -ErrorAction Ignore
+        Remove-Item -Path ".\kubectx.exe" -Recurse -ErrorAction Ignore
+
+        # Unpack archive file into this temporary directory
+        # Windows BSD-tar handles zip. Imagine that.
+        echo "Unpacking $pkg_download"
+        & tar xf "$pkg_download"
+
+        # Settle unpacked archive into place
+        echo "Install Location: $pkg_src_cmd"
+        New-Item "$pkg_src_bin" -ItemType Directory -Force        
+        Move-Item -Path "kubectx.exe" -Destination "$pkg_src_bin"
+
+    # Exit tmp
+    popd
+}
+
+echo "Copying into '$pkg_dst_cmd' from '$pkg_src_cmd'"
+Remove-Item -Path "$pkg_dst_cmd" -Recurse -ErrorAction Ignore
+Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse
+Remove-Item -Path "$pkg_src" -Recurse -ErrorAction Ignore
diff --git a/kubectx/install.sh b/kubectx/install.sh
new file mode 100644 (file)
index 0000000..40b1801
--- /dev/null
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+{
+    set -e
+    set -u
+
+    ###################
+    # Install kubectx #
+    ###################
+
+    # Every package should define these 6 variables
+    pkg_cmd_name="kubectx"
+
+    pkg_dst_cmd="$HOME/.local/bin/kubectx"
+    pkg_dst="$pkg_dst_cmd"
+
+    pkg_src_cmd="$HOME/.local/opt/kubectx-v$WEBI_VERSION/bin/kubectx"
+    pkg_src_dir="$HOME/.local/opt/kubectx-v$WEBI_VERSION"
+    pkg_src="$pkg_src_cmd"
+
+    # pkg_install must be defined by every package
+    pkg_install() {
+        # e.g. ~/.local/opt/kubectx-v0.99.9/bin
+        mkdir -p "$(dirname $pkg_src_cmd)"
+
+        # mv ./kubectx-*/kubectx ~/.local/opt/kubectx-v0.99.9/bin/kubectx
+        mv kubectx "$pkg_src_cmd"
+    }
+
+    # pkg_get_current_version is recommended, but (soon) not required
+    pkg_get_current_version() {
+        # 'kubectx' has no version parameter
+        echo       
+    }
+
+}
diff --git a/kubectx/releases.js b/kubectx/releases.js
new file mode 100644 (file)
index 0000000..b94d09b
--- /dev/null
@@ -0,0 +1,28 @@
+'use strict';
+
+var github = require('../_common/github.js');
+var owner = 'ahmetb';
+var repo = 'kubectx';
+
+/******************************************************************************/
+/** Note: Delete this Comment!                                               **/
+/**                                                                          **/
+/** Need a an example that filters out miscellaneous release files?          **/
+/**   See `deno`, `gitea`, or `caddy`                                        **/
+/**                                                                          **/
+/******************************************************************************/
+
+module.exports = function (request) {
+  return github(request, owner, repo).then(function (all) {
+    return all;
+  });
+};
+
+if (module === require.main) {
+  module.exports(require('@root/request')).then(function (all) {
+    all = require('../_webi/normalize.js')(all);
+    // just select the first 5 for demonstration
+    all.releases = all.releases.slice(0, 5);
+    console.info(JSON.stringify(all, null, 2));
+  });
+}