2019-02-22 08:49:56 -05:00
|
|
|
// Module included in the following assemblies:
|
|
|
|
|
//
|
2019-10-02 11:30:49 -04:00
|
|
|
// * cli_reference/openshift_cli/extending-cli-plugins.adoc
|
2019-02-22 08:49:56 -05:00
|
|
|
|
2023-10-30 10:13:25 -04:00
|
|
|
:_mod-docs-content-type: PROCEDURE
|
2019-05-13 08:55:00 +10:00
|
|
|
[id="cli-writing-plugins_{context}"]
|
2023-01-16 16:03:42 -05:00
|
|
|
= Writing CLI plugins
|
2019-02-22 08:49:56 -05:00
|
|
|
|
2023-10-30 10:13:25 -04:00
|
|
|
You can write a plugin for the
|
2023-06-21 11:11:14 -05:00
|
|
|
ifndef::openshift-rosa,openshift-dedicated[]
|
|
|
|
|
{product-title}
|
|
|
|
|
endif::openshift-rosa,openshift-dedicated[]
|
|
|
|
|
ifdef::openshift-rosa,openshift-dedicated[]
|
|
|
|
|
OpenShift
|
|
|
|
|
endif::openshift-rosa,openshift-dedicated[]
|
|
|
|
|
CLI in any programming language
|
2019-02-22 08:49:56 -05:00
|
|
|
or script that allows you to write command-line commands. Note that you can not
|
2023-01-16 16:03:42 -05:00
|
|
|
use a plugin to overwrite an existing `oc` command.
|
2019-02-22 08:49:56 -05:00
|
|
|
|
|
|
|
|
.Procedure
|
|
|
|
|
|
2023-01-16 16:03:42 -05:00
|
|
|
This procedure creates a simple Bash plugin that prints a message to the
|
2019-02-22 08:49:56 -05:00
|
|
|
terminal when the `oc foo` command is issued.
|
|
|
|
|
|
|
|
|
|
. Create a file called `oc-foo`.
|
|
|
|
|
+
|
2023-01-16 16:03:42 -05:00
|
|
|
When naming your plugin file, keep the following in mind:
|
2019-02-22 08:49:56 -05:00
|
|
|
|
2021-03-24 11:55:35 -04:00
|
|
|
* The file must begin with `oc-` or `kubectl-` to be recognized as a
|
2023-01-16 16:03:42 -05:00
|
|
|
plugin.
|
|
|
|
|
* The file name determines the command that invokes the plugin. For example, a
|
|
|
|
|
plugin with the file name `oc-foo-bar` can be invoked by a command of
|
2019-02-22 08:49:56 -05:00
|
|
|
`oc foo bar`. You can also use underscores if you want the command to contain
|
2023-01-16 16:03:42 -05:00
|
|
|
dashes. For example, a plugin with the file name `oc-foo_bar` can be invoked
|
2019-02-22 08:49:56 -05:00
|
|
|
by a command of `oc foo-bar`.
|
|
|
|
|
|
|
|
|
|
. Add the following contents to the file.
|
|
|
|
|
+
|
2020-08-04 22:42:35 -04:00
|
|
|
[source,bash]
|
2019-02-22 08:49:56 -05:00
|
|
|
----
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
# optional argument handling
|
|
|
|
|
if [[ "$1" == "version" ]]
|
|
|
|
|
then
|
|
|
|
|
echo "1.0.0"
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# optional argument handling
|
|
|
|
|
if [[ "$1" == "config" ]]
|
|
|
|
|
then
|
|
|
|
|
echo $KUBECONFIG
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "I am a plugin named kubectl-foo"
|
|
|
|
|
----
|
|
|
|
|
|
2023-10-30 10:13:25 -04:00
|
|
|
After you install this plugin for the
|
2023-06-21 11:11:14 -05:00
|
|
|
ifndef::openshift-rosa,openshift-dedicated[]
|
|
|
|
|
{product-title}
|
|
|
|
|
endif::openshift-rosa,openshift-dedicated[]
|
|
|
|
|
ifdef::openshift-rosa[]
|
|
|
|
|
OpenShift
|
|
|
|
|
endif::openshift-rosa[]
|
|
|
|
|
CLI, it can be invoked
|
2019-02-22 08:49:56 -05:00
|
|
|
using the `oc foo` command.
|
|
|
|
|
|
2022-02-17 13:08:23 -05:00
|
|
|
[role="_additional-resources"]
|
2019-02-22 08:49:56 -05:00
|
|
|
.Additional resources
|
|
|
|
|
|
2023-01-16 16:03:42 -05:00
|
|
|
* Review the link:https://github.com/kubernetes/sample-cli-plugin[Sample plugin repository]
|
|
|
|
|
for an example of a plugin written in Go.
|
|
|
|
|
* Review the link:https://github.com/kubernetes/cli-runtime/[CLI runtime repository] for a set of utilities to assist in writing plugins in Go.
|