├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── bin └── kubectl-alias └── test ├── mockbin └── kubectl └── tests.bats /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | jobs: 9 | ci: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Setup BATS 13 | uses: mig4/setup-bats@v1 14 | with: 15 | bats-version: 1.2.1 16 | - name: Checkout 17 | uses: actions/checkout@v3 18 | - name: make 19 | run: make all 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /output/ 2 | /alias/ 3 | VERSION 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Wenhao Ji 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | NAME=kubectl-alias 2 | GIT_TAG := $(shell git describe --tags --abbrev=0) 3 | GIT_COMMIT_ID := $(shell git rev-parse HEAD) 4 | VERSION := $(GIT_TAG:v%=%) 5 | 6 | OUTPUT_DIR=output 7 | RELEASE_FILE_NAME=$(NAME)-$(VERSION).tar.gz 8 | RELEASE_FILE_PATH=$(OUTPUT_DIR)/$(RELEASE_FILE_NAME) 9 | SIG_FILE_NAME=$(NAME)-$(VERSION).asc 10 | SIG_FILE_PATH=$(OUTPUT_DIR)/$(SIG_FILE_NAME) 11 | CHECKSUM_FILE_NAME=$(RELEASE_FILE_NAME).sha256 12 | CHECKSUM_FILE_PATH=$(OUTPUT_DIR)/$(CHECKSUM_FILE_NAME) 13 | VERSION_FILE_PATH=VERSION 14 | 15 | ifeq ($(OS), Windows_NT) 16 | OS_UNAME := Windows 17 | else 18 | OS_UNAME := $(shell uname -s) 19 | endif 20 | 21 | .PHONY: build version sign checksum clean mk-output-dir test 22 | 23 | all: test $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH) 24 | 25 | build: $(RELEASE_FILE_PATH) 26 | 27 | version: $(VERSION_FILE_PATH) 28 | 29 | test: 30 | bats test/ 31 | 32 | checksum: $(CHECKSUM_FILE_PATH) 33 | 34 | sign: $(SIG_FILE_PATH) 35 | 36 | mk-output-dir: 37 | mkdir -p $(OUTPUT_DIR) 38 | 39 | clean: 40 | rm -rf $(VERSION_FILE_PATH) $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH) $(SIG_FILE_PATH) 41 | 42 | $(VERSION_FILE_PATH): 43 | echo "$(VERSION) (commit = $(GIT_COMMIT_ID))" > $(VERSION_FILE_PATH) 44 | 45 | $(RELEASE_FILE_PATH): mk-output-dir $(VERSION_FILE_PATH) 46 | tar czvf $(RELEASE_FILE_PATH) bin/ LICENSE $(VERSION_FILE_PATH) 47 | 48 | $(SIG_FILE_PATH): $(RELEASE_FILE_PATH) 49 | gpg -ab $(RELEASE_FILE_PATH) 50 | 51 | $(CHECKSUM_FILE_PATH): $(RELEASE_FILE_PATH) 52 | ifeq ($(OS_UNAME), Darwin) 53 | shasum -a 256 $(RELEASE_FILE_PATH) | awk '{print $$1}' > $(CHECKSUM_FILE_PATH) 54 | else ifeq ($(OS_UNAME), Linux) 55 | sha256sum $(RELEASE_FILE_PATH) | awk '{print $$1}' > $(CHECKSUM_FILE_PATH) 56 | else 57 | echo "Unsupported OS: $(OS_UNAME)" 58 | exit 1 59 | endif 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kubectl-alias 2 | 3 | ![GitHub release version](https://img.shields.io/github/v/release/predatorray/kubectl-alias) 4 | ![License](https://img.shields.io/github/license/predatorray/kubectl-alias) 5 | ![Github Workflow status](https://img.shields.io/github/actions/workflow/status/predatorray/kubectl-alias/ci.yml?branch=master) 6 | 7 | The missing `alias` command for `kubectl`. 8 | 9 | ## Examples 10 | 11 | ```sh 12 | # "v" for version 13 | kubectl alias v version 14 | 15 | kubectl v --client 16 | ``` 17 | 18 | ```sh 19 | # "cd" to switch namespace 20 | kubectl alias cd 'config set-context $(kubectl config current-context) --namespace' 21 | 22 | kubectl cd my-namespace 23 | ``` 24 | 25 | ```sh 26 | # Get Pod's YAML spec and open it with less 27 | kubectl alias --no-args gpyl 'get pod -o yaml $1 | less' 28 | 29 | kubectl gpyl my-pod 30 | ``` 31 | 32 | ```sh 33 | # Make kubectl awesome! 34 | kubectl alias avada-kedavra 'delete' 35 | kubectl alias alohomora 'exec -it $1 -- bash' 36 | ``` 37 | 38 | ## Installation 39 | 40 | ### Homebrew 41 | 42 | 1. Install [Homebrew](https://brew.sh/). 43 | 44 | 2. `brew install predatorray/brew/kubectl-alias` 45 | 46 | 3. Add this line to your rc file (e.g.: `~/.bashrc`, `~/.zshrc`). 47 | ```sh 48 | export PATH="$PATH:$(brew --prefix kubectl-alias)/alias" 49 | ``` 50 | 51 | ### Krew 52 | 53 | 1. [Install Krew](https://krew.sigs.k8s.io/docs/user-guide/setup/install/) or upgrade to its latest version using `kubectl krew upgrade`. 54 | 55 | 2. `kubectl krew index add predatorray https://github.com/predatorray/my-krew-index.git` 56 | 57 | 3. `kubectl krew install predatorray/alias` 58 | 59 | 4. Add this line to your rc file (e.g.: `~/.bashrc`, `~/.zshrc`). 60 | ```sh 61 | export PATH="$PATH:$(kubectl alias --prefix)" 62 | ``` 63 | 64 | ### Manually 65 | 66 | 1. Download the [latest release](https://github.com/predatorray/kubectl-alias/releases/latest). 67 | 68 | 2. Unpack the `kubectl-alias-*.tar.gz` file and copy all the files to a directory, `/usr/local/kubectl-alias` for instance. 69 | 70 | 3. Add both the `bin/` and the `alias/` directories to the `PATH`. For example, add this line to your rc file: 71 | ```sh 72 | export PATH="$PATH:/usr/local/kubectl-alias/bin:/usr/local/kubectl-alias/alias" 73 | ``` 74 | 75 | 4. If it is not running on GNU-Linux, install the GNU `getopt`. After that, add this line `export GNU_GETOPT_PREFIX="path/to/gnu-getopt"` to your rc file. 76 | 77 | ## Usage 78 | 79 | ### Create an alias 80 | 81 | ```sh 82 | kubectl alias ALIAS COMMAND 83 | 84 | kubectl alias -N ALIAS COMMAND 85 | kubectl alias --no-args ALIAS COMMAND 86 | ``` 87 | 88 | The `-N, --no-args` flag is used when the arguments should not be passed to the end of the commands. It is useful when the offset parameter is explicitly declared in the alias command. For example, 89 | 90 | ```sh 91 | kubectl alias --no-args gpyl 'get pod -o yaml $1 | less' 92 | ``` 93 | 94 | If the flag is absent, by executing `kubectl gpyl my-pod`, the `my-pod` argument will also be passed to the `less` commnd. 95 | 96 | ```sh 97 | # WRONG 98 | kubectl alias gpyl 'get pod -o yaml $1 | less' 99 | kubectl get pod -o yaml my-pod | less my-pod 100 | ``` 101 | 102 | ### Delete an alias 103 | 104 | ```sh 105 | kubectl alias -d ALIAS 106 | kubectl alias --delete ALIAS 107 | ``` 108 | ### List all the alias 109 | 110 | ```sh 111 | kubectl alias -l 112 | kubectl alias --list 113 | ``` 114 | 115 | 116 | ## FAQ 117 | 118 | ### `error: unknown command "ALIAS NAME" for "kubectl"` 119 | 120 | This means that the `alias/` directory is not correctly added to the `PATH` environment variable. 121 | 122 | Add this line to your rc file. 123 | 124 | ```sh 125 | export PATH="$PATH:$(brew --prefix kubectl-alias)/alias" 126 | 127 | # Or, if installed manually. 128 | export PATH="$PATH:${PREFIX}/alias" 129 | ``` 130 | 131 | After that, run `kubectl plugin list` to check if the aliases have been loaded successfully. If the alias is named `v`, the output of the plugin list will be: 132 | 133 | ```txt 134 | The following compatible plugins are available: 135 | 136 | /usr/local/bin/kubectl-alias 137 | /usr/local/opt/kubectl-alias/alias/kubectl-v 138 | ``` 139 | 140 | ## Support 141 | 142 | Please feel free to [open an issue](https://github.com/predatorray/kubectl-alias/issues/new) if you find any bug or have any suggestion. 143 | -------------------------------------------------------------------------------- /bin/kubectl-alias: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright (c) 2022 Wenhao Ji 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | # this software and associated documentation files (the "Software"), to deal in 7 | # the Software without restriction, including without limitation the rights to 8 | # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | # the Software, and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | set -euf -o pipefail 23 | 24 | readonly PROG_NAME='kubectl alias' 25 | 26 | declare -ra RESERVED_COMMANDS=( 27 | 'alias' 'alpha' 'annotate' 'api-resources' 'api-versions' 'apply' 'attach' 28 | 'auth' 'autoscale' 'certificate' 'cluster-info' 'completion' 'config' 'cordon' 29 | 'cp' 'create' 'debug' 'delete' 'describe' 'diff' 'drain' 'edit' 'exec' 30 | 'explain' 'expose' 'get' 'help' 'kustomize' 'label' 'logs' 'options' 'patch' 31 | 'plugin' 'port-forward' 'proxy' 'replace' 'rollout' 'run' 'scale' 'set' 'taint' 32 | 'top' 'uncordon' 'version' 'wait' 33 | ) 34 | 35 | # PROG_DIR 36 | SOURCE="${BASH_SOURCE[0]}" 37 | while [[ -h "$SOURCE" ]]; do 38 | PROG_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" 39 | SOURCE="$(readlink "$SOURCE")" 40 | [[ $SOURCE != /* ]] && SOURCE="$PROG_DIR/$SOURCE" 41 | done 42 | PROG_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" 43 | 44 | function array_contains() { 45 | local occur="$1" 46 | local arr=("${@:2}") 47 | for e in "${arr[@]}"; do 48 | if [[ "${e}" == "${occur}" ]]; then 49 | return 0 50 | fi 51 | done 52 | return 1 53 | } 54 | 55 | function warn() { 56 | echo >&2 'warn:' "$@" 57 | } 58 | 59 | function error_and_exit() { 60 | echo >&2 'error:' "$@" 61 | echo >&2 "Run '${PROG_NAME} --help' for more information on the command." 62 | exit 1 63 | } 64 | 65 | function print_version() { 66 | local version_file="${PROG_DIR}/../VERSION" 67 | if [[ -f "${version_file}" ]]; then 68 | cat "${version_file}" 69 | else 70 | echo "unknown" 71 | fi 72 | } 73 | 74 | function check_prerequisites() { 75 | # kubectl is installed 76 | if ! which kubectl 2>&1 >/dev/null; then 77 | echo >&2 'kubectl is not found.' 78 | exit 127 79 | fi 80 | 81 | # gnu-getopt is installed 82 | if [[ ! -z "${GNU_GETOPT_PREFIX:-}" ]]; then 83 | hash -p "${GNU_GETOPT_PREFIX}/bin/getopt" getopt 84 | else 85 | local getopt_test=0 86 | getopt -T 2>&1 >/dev/null || getopt_test="$?" 87 | if [[ ! "${getopt_test}" -eq 4 ]]; then 88 | echo >&2 'The getopt is not GNU enhanced version.' 89 | echo >&2 'Please install gnu-getopt and either add it to your PATH or set GNU_GETOPT_PREFIX env variable to its installed location.' 90 | exit 4 91 | fi 92 | fi 93 | } 94 | 95 | function usage() { 96 | cat << EOF 97 | Creates aliases for the kubectl command. 98 | 99 | Examples: 100 | # "kubectl uc ctx1" instead of "kubectl config use-context ctx1" 101 | ${PROG_NAME} uc 'config use-context' 102 | 103 | # "kubectl cd ns1" instead of "kubectl config set-context \$(kubectl config current-context) --namespace ns1" 104 | ${PROG_NAME} cd 'config set-context \$(kubectl config current-context) --namespace' 105 | 106 | # "kubectl gpyl my-pod" instead of "kubectl get pod -o yaml my-pod | less" 107 | ${PROG_NAME} --no-args gpyl 'get pod -o yaml "\$1" | less' 108 | 109 | Options: 110 | -d, --delete: Delete the alias 111 | -l, --list: List all the aliases 112 | -N, --no-args Do not append arguments to the end of the command when executing 113 | 114 | --prefix Display the alias path 115 | -h, --help: Show the usage 116 | -V, --version: Print the version 117 | 118 | Usage: 119 | ${PROG_NAME} [ -N | --no-args ] ALIAS COMMAND 120 | ${PROG_NAME} ( -d | --delete ) ALIAS 121 | ${PROG_NAME} ( -l | --list ) 122 | ${PROG_NAME} --prefix 123 | EOF 124 | } 125 | 126 | function list_aliases() { 127 | local argc="$#" 128 | if [[ ! "${argc}" -eq 0 ]]; then 129 | error_and_exit "-l, --list requires zero argument but ${argc}." 130 | fi 131 | local alias_dir="${PROG_DIR}/../alias" 132 | if [[ ! -d "${alias_dir}" ]]; then 133 | exit 0 134 | fi 135 | for kubectl_plugin_bin in $(ls -1 "${alias_dir}"); do 136 | if [[ -x "${alias_dir}/${kubectl_plugin_bin}" && "${kubectl_plugin_bin}" = kubectl-* ]]; then 137 | local alias_description 138 | alias_description="$(sed -n 's/^# __ALIAS_DESCRIPTION__: \(.*\)/\1/p' "${alias_dir}/${kubectl_plugin_bin}" 2>/dev/null)" 139 | if [[ -z "${alias_description}" ]]; then 140 | local name_without_kubectl="${kubectl_plugin_bin:8}" 141 | local unescaped_name="${name_without_kubectl//_/-}" 142 | echo "${unescaped_name}" 143 | else 144 | echo "${alias_description}" 145 | fi 146 | fi 147 | done 148 | } 149 | 150 | function delete_alias() { 151 | local argc="$#" 152 | if [[ ! "${argc}" -eq 1 ]]; then 153 | error_and_exit "-d, --delete requires one argument but ${argc}." 154 | fi 155 | local alias_name="$1" 156 | check_alias_name "${alias_name}" 157 | 158 | local alias_dir="${PROG_DIR}/../alias" 159 | local kubectl_plugin_bin="${alias_dir}/kubectl-${alias_name//-/_}" 160 | if [[ ! -d "${alias_dir}" || ! -f "${kubectl_plugin_bin}" ]]; then 161 | error_and_exit "The alias ${alias_name} does not exist." 162 | fi 163 | rm -f "${kubectl_plugin_bin}" 164 | } 165 | 166 | function check_alias_name() { 167 | local alias_name="$1" 168 | if [[ ! "${alias_name}" =~ (^[A-Za-z]([A-Za-z0-9\-])*$) ]]; then 169 | error_and_exit 'The alias name must start with a letter and only include letters, numbers or dashes. ([A-Za-z][A-Za-z0-9\-]*)' 170 | fi 171 | if array_contains "${alias_name}" "${RESERVED_COMMANDS[@]}"; then 172 | error_and_exit "${alias_name} is a reserved command in kubectl." 173 | fi 174 | } 175 | 176 | function create_alias() { 177 | local argc="$#" 178 | if [[ ! "${argc}" -eq 2 ]]; then 179 | error_and_exit "requires two arguments but ${argc}." 180 | fi 181 | 182 | local alias_name="$1" 183 | check_alias_name "${alias_name}" 184 | 185 | local command="$2" 186 | # TODO check command (ensure no carriage-return) 187 | 188 | # check alias kubectl-plugin directory 189 | local alias_dir="${PROG_DIR}/../alias" 190 | if [[ ! -d "${alias_dir}" ]]; then 191 | mkdir -p "${alias_dir}" 192 | fi 193 | 194 | local kubectl_plugin_bin="${alias_dir}/kubectl-${alias_name//-/_}" 195 | if [[ -f "${kubectl_plugin_bin}" ]]; then 196 | error_and_exit "The alias already exists. Delete the alias firstly with --delete ${alias_name}." 197 | fi 198 | if [[ "${OPT_NO_ARGS:-0}" -gt 0 ]]; then 199 | cat > "${kubectl_plugin_bin}" << EOF 200 | #!/bin/sh 201 | # __ALIAS_DESCRIPTION__: ${alias_name} = ${command} (no-args) 202 | kubectl ${command} 203 | EOF 204 | else 205 | cat > "${kubectl_plugin_bin}" << EOF 206 | #!/bin/sh 207 | # __ALIAS_DESCRIPTION__: ${alias_name} = ${command} 208 | kubectl ${command} "\$@" 209 | EOF 210 | fi 211 | chmod +x-w "${kubectl_plugin_bin}" 212 | } 213 | 214 | function print_prefix() { 215 | local argc="$#" 216 | if [[ ! "${argc}" -eq 0 ]]; then 217 | error_and_exit "--prefix requires zero argument but ${argc}." 218 | fi 219 | local alias_dir="${PROG_DIR}/../alias" 220 | local prog_parent_dir="${PROG_DIR}/.." 221 | if [[ -d "${alias_dir}" && -x "${alias_dir}" ]]; then 222 | ( cd "${alias_dir}" && pwd ) 223 | elif [[ -d "${prog_parent_dir}" && -x "${prog_parent_dir}" ]]; then 224 | echo "$( cd "${prog_parent_dir}" && pwd )/alias" 225 | else 226 | error_and_exit "cannot find the alias directory: ${alias_dir}" 227 | fi 228 | } 229 | 230 | function main() { 231 | check_prerequisites 232 | 233 | if [[ $# -eq 0 ]]; then 234 | usage 235 | exit 0 236 | fi 237 | 238 | local opts 239 | opts=$(getopt -o hVdlN --long help,version,delete,list,no-args,prefix -- "$@") 240 | eval set -- $opts 241 | 242 | local op_delete=0 243 | local op_list=0 244 | local op_prefix=0 245 | local opt_no_args=0 246 | while [[ $# -gt 0 ]]; do 247 | local opt="$1" 248 | case "${opt}" in 249 | -h|--help) 250 | usage 251 | exit 0 252 | ;; 253 | -V|--version) 254 | print_version 255 | exit 0 256 | ;; 257 | -l|--list) 258 | op_list=1 259 | ;; 260 | -d|--delete) 261 | op_delete=1 262 | ;; 263 | --prefix) 264 | op_prefix=1 265 | ;; 266 | -N|--no-args) 267 | opt_no_args=1 268 | ;; 269 | --) 270 | shift 271 | break 272 | ;; 273 | *) 274 | break 275 | ;; 276 | esac 277 | shift 278 | done 279 | 280 | local op_sum="$(( op_delete + op_list + op_prefix ))" 281 | if [[ "${op_sum}" -gt 1 ]]; then 282 | error_and_exit 'The option "-l, --list" or "-d, --delete" or "--prefix" cannot be used at the same time.' 283 | elif [[ "${op_list}" -gt 0 ]]; then 284 | if [[ ${opt_no_args} -gt 0 ]]; then 285 | warn 'The option -N, --no-args is not applicable to the option -l, --list.' 286 | fi 287 | list_aliases "$@" 288 | elif [[ "${op_delete}" -gt 0 ]]; then 289 | if [[ ${opt_no_args} -gt 0 ]]; then 290 | warn 'The option -N, --no-args is not applicable to the option -d, --delete.' 291 | fi 292 | delete_alias "$@" 293 | elif [[ "${op_prefix}" -gt 0 ]]; then 294 | print_prefix "$@" 295 | else 296 | OPT_NO_ARGS="${opt_no_args}" create_alias "$@" 297 | fi 298 | } 299 | 300 | main "$@" 301 | -------------------------------------------------------------------------------- /test/mockbin/kubectl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo "$@" 3 | -------------------------------------------------------------------------------- /test/tests.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | original_path= 4 | 5 | function setup() { 6 | rm -f VERSION 7 | rm -rf alias 8 | mkdir -p alias 9 | original_path="$PATH" 10 | export PATH="$(pwd)/test/mockbin:$PATH" 11 | } 12 | 13 | function teardown() { 14 | rm -rf alias 15 | export PATH="${original_path}" 16 | } 17 | 18 | @test "create_an_simple_alias" { 19 | bin/kubectl-alias v version 20 | [ -x alias/kubectl-v ] 21 | local cmd_output=$(alias/kubectl-v) 22 | [ "${cmd_output}" = 'version' ] 23 | } 24 | 25 | @test "create_an_alias_with_arguments" { 26 | bin/kubectl-alias f foobar 27 | [ -x alias/kubectl-f ] 28 | local cmd_output=$(alias/kubectl-f 1 2) 29 | [ "${cmd_output}" = 'foobar 1 2' ] 30 | } 31 | 32 | @test "create_an_alias_no_args" { 33 | bin/kubectl-alias --no-args f foobar 34 | [ -x alias/kubectl-f ] 35 | local cmd_output=$(alias/kubectl-f 1 2) 36 | [ "${cmd_output}" = 'foobar' ] 37 | } 38 | 39 | @test "create_an_alias_with_offset_parameter_and_no_arg" { 40 | bin/kubectl-alias --no-args f '$1 foo $2 bar' 41 | [ -x alias/kubectl-f ] 42 | local cmd_output=$(alias/kubectl-f 1 2) 43 | [ "${cmd_output}" = '1 foo 2 bar' ] 44 | } 45 | 46 | @test "create_an_alias_with_offset_parameter_without_no_arg" { 47 | bin/kubectl-alias f '$1 foo $2 bar' 48 | [ -x alias/kubectl-f ] 49 | local cmd_output=$(alias/kubectl-f 1 2) 50 | [ "${cmd_output}" = '1 foo 2 bar 1 2' ] 51 | } 52 | 53 | @test "create_an_alias_with_invalid_name_number" { 54 | local exit_code 55 | bin/kubectl-alias 123 'yes' || exit_code="$?" 56 | [ "${exit_code}" = 1 ] 57 | } 58 | 59 | @test "create_an_alias_with_invalid_name_underscore" { 60 | local exit_code 61 | bin/kubectl-alias a_b 'yes' || exit_code="$?" 62 | [ "${exit_code}" = 1 ] 63 | } 64 | 65 | @test "create_an_alias_with_invalid_name_question_mark" { 66 | local exit_code 67 | bin/kubectl-alias 'a?' 'yes' || exit_code="$?" 68 | [ "${exit_code}" = 1 ] 69 | } 70 | 71 | @test "create_an_alias_with_invalid_name_leading_number" { 72 | local exit_code 73 | bin/kubectl-alias '1a' 'yes' || exit_code="$?" 74 | [ "${exit_code}" = 1 ] 75 | } 76 | 77 | @test "create_an_alias_with_reserved_command_version" { 78 | local exit_code 79 | bin/kubectl-alias 'version' 'yes' || exit_code="$?" 80 | [ "${exit_code}" = 1 ] 81 | } 82 | 83 | @test "create_an_alias_with_reserved_command_get" { 84 | local exit_code 85 | bin/kubectl-alias 'get' 'yes' || exit_code="$?" 86 | [ "${exit_code}" = 1 ] 87 | } 88 | 89 | @test "delete_an_alias" { 90 | bin/kubectl-alias v version 91 | [ -x alias/kubectl-v ] 92 | bin/kubectl-alias --delete v 93 | [ ! -f alias/kubectl-v ] 94 | } 95 | 96 | @test "delete_an_alias_that_does_not_exist" { 97 | bin/kubectl-alias --delete na || exit_code="$?" 98 | [ "${exit_code}" = 1 ] 99 | } 100 | 101 | @test "list_all_aliaes" { 102 | bin/kubectl-alias v1 version 103 | bin/kubectl-alias v2 version 104 | 105 | local list_output=$(bin/kubectl-alias --list) 106 | [ "${list_output}" = $'v1 = version\nv2 = version' ] 107 | } 108 | 109 | @test "version" { 110 | local version_output=$(bin/kubectl-alias --version) 111 | [ "${version_output}" = 'unknown' ] 112 | } 113 | 114 | @test "help" { 115 | local version_output=$(bin/kubectl-alias --help) 116 | [ ! -z "${version_output}" ] 117 | } 118 | 119 | @test "prefix" { 120 | local prefix_output=$(bin/kubectl-alias --prefix) 121 | [ "${prefix_output}" = "$(pwd)/alias" ] 122 | } 123 | 124 | @test "list_and_delete_at_same_time" { 125 | local exit_code 126 | bin/kubectl-alias '--list' '--delete' foobar || exit_code="$?" 127 | [ "${exit_code}" = 1 ] 128 | } 129 | 130 | @test "list_and_delete_and_prefix_at_same_time" { 131 | local exit_code 132 | bin/kubectl-alias '--list' '--delete' foobar '--prefix' || exit_code="$?" 133 | [ "${exit_code}" = 1 ] 134 | } 135 | --------------------------------------------------------------------------------