Skip to content

Commit 341f89a

Browse files
committed
add option to oc whoami that prints server url
Fixes #11148 Currently there is no "simple" way to obtain the rest api's URL. This patch adds an option `--show-server-url` || `-u` to `oc whoami` that retrieves the server url from the first cluster in `config.Cluster` **Before** ``` $ oc config view --minify -o jsonpath='{.clusters[*].cluster.server}' https://10.13.137.149:8443% # no linebreak at the end ``` **After** ``` $ oc whoami -u # or --show-server-url https://10.13.137.149:8443 ```
1 parent 8b46157 commit 341f89a

File tree

8 files changed

+46
-0
lines changed

8 files changed

+46
-0
lines changed

contrib/completions/bash/oc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13715,6 +13715,8 @@ _oc_whoami()
1371513715
flags+=("--show-context")
1371613716
flags+=("-c")
1371713717
local_nonpersistent_flags+=("--show-context")
13718+
flags+=("--show-server")
13719+
local_nonpersistent_flags+=("--show-server")
1371813720
flags+=("--show-token")
1371913721
flags+=("-t")
1372013722
local_nonpersistent_flags+=("--show-token")

contrib/completions/bash/openshift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18267,6 +18267,8 @@ _openshift_cli_whoami()
1826718267
flags+=("--show-context")
1826818268
flags+=("-c")
1826918269
local_nonpersistent_flags+=("--show-context")
18270+
flags+=("--show-server")
18271+
local_nonpersistent_flags+=("--show-server")
1827018272
flags+=("--show-token")
1827118273
flags+=("-t")
1827218274
local_nonpersistent_flags+=("--show-token")

contrib/completions/zsh/oc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13876,6 +13876,8 @@ _oc_whoami()
1387613876
flags+=("--show-context")
1387713877
flags+=("-c")
1387813878
local_nonpersistent_flags+=("--show-context")
13879+
flags+=("--show-server")
13880+
local_nonpersistent_flags+=("--show-server")
1387913881
flags+=("--show-token")
1388013882
flags+=("-t")
1388113883
local_nonpersistent_flags+=("--show-token")

contrib/completions/zsh/openshift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18428,6 +18428,8 @@ _openshift_cli_whoami()
1842818428
flags+=("--show-context")
1842918429
flags+=("-c")
1843018430
local_nonpersistent_flags+=("--show-context")
18431+
flags+=("--show-server")
18432+
local_nonpersistent_flags+=("--show-server")
1843118433
flags+=("--show-token")
1843218434
flags+=("-t")
1843318435
local_nonpersistent_flags+=("--show-token")

docs/man/man1/oc-whoami.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ user context.
2626
\fB\-c\fP, \fB\-\-show\-context\fP=false
2727
Print the current user context name
2828

29+
.PP
30+
\fB\-\-show\-server\fP=false
31+
Print the current server's REST API URL
32+
2933
.PP
3034
\fB\-t\fP, \fB\-\-show\-token\fP=false
3135
Print the token the current session is using. This will return an error if you are using a different form of authentication.

docs/man/man1/openshift-cli-whoami.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ user context.
2626
\fB\-c\fP, \fB\-\-show\-context\fP=false
2727
Print the current user context name
2828

29+
.PP
30+
\fB\-\-show\-server\fP=false
31+
Print the current server's REST API URL
32+
2933
.PP
3034
\fB\-t\fP, \fB\-\-show\-token\fP=false
3135
Print the token the current session is using. This will return an error if you are using a different form of authentication.

pkg/cmd/cli/cmd/whoami.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func NewCmdWhoAmI(name, fullName string, f *clientcmd.Factory, out io.Writer) *c
5353

5454
cmd.Flags().BoolP("show-token", "t", false, "Print the token the current session is using. This will return an error if you are using a different form of authentication.")
5555
cmd.Flags().BoolP("show-context", "c", false, "Print the current user context name")
56+
cmd.Flags().Bool("show-server", false, "Print the current server's REST API URL")
5657

5758
return cmd
5859
}
@@ -89,6 +90,17 @@ func RunWhoAmI(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []s
8990
fmt.Fprintf(out, "%s\n", cfg.CurrentContext)
9091
return nil
9192
}
93+
if kcmdutil.GetFlagBool(cmd, "show-server") {
94+
cfg, err := f.OpenShiftClientConfig.RawConfig()
95+
if err != nil {
96+
return err
97+
}
98+
for _, c := range cfg.Clusters {
99+
fmt.Fprintf(out, "%s\n", c.Server)
100+
return nil
101+
}
102+
return fmt.Errorf("unable to get clusters. Cannot retrieve server URL.")
103+
}
92104

93105
client, _, err := f.Clients()
94106
if err != nil {

test/cmd/whoami.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
source "$(dirname "${BASH_SOURCE}")/../../hack/lib/init.sh"
3+
trap os::test::junit::reconcile_output EXIT
4+
5+
# Cleanup cluster resources created by this test
6+
(
7+
set +e
8+
oc delete all,templates --all
9+
exit 0
10+
) &>/dev/null
11+
12+
13+
os::test::junit::declare_suite_start "cmd/whoami"
14+
# This test validates the whoami command's --show-server flag
15+
os::cmd::expect_success_and_text 'oc whoami --show-server' 'http(s)?:\/\/[0-9\.]+\:[0-9]+'
16+
17+
echo "whoami: ok"
18+
os::test::junit::declare_suite_end

0 commit comments

Comments
 (0)