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 option to oc whoami that prints server url #11180

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contrib/completions/bash/oc
Original file line number Diff line number Diff line change
Expand Up @@ -13715,6 +13715,8 @@ _oc_whoami()
flags+=("--show-context")
flags+=("-c")
local_nonpersistent_flags+=("--show-context")
flags+=("--show-server")
local_nonpersistent_flags+=("--show-server")
flags+=("--show-token")
flags+=("-t")
local_nonpersistent_flags+=("--show-token")
Expand Down
2 changes: 2 additions & 0 deletions contrib/completions/bash/openshift
Original file line number Diff line number Diff line change
Expand Up @@ -18267,6 +18267,8 @@ _openshift_cli_whoami()
flags+=("--show-context")
flags+=("-c")
local_nonpersistent_flags+=("--show-context")
flags+=("--show-server")
local_nonpersistent_flags+=("--show-server")
flags+=("--show-token")
flags+=("-t")
local_nonpersistent_flags+=("--show-token")
Expand Down
2 changes: 2 additions & 0 deletions contrib/completions/zsh/oc
Original file line number Diff line number Diff line change
Expand Up @@ -13876,6 +13876,8 @@ _oc_whoami()
flags+=("--show-context")
flags+=("-c")
local_nonpersistent_flags+=("--show-context")
flags+=("--show-server")
local_nonpersistent_flags+=("--show-server")
flags+=("--show-token")
flags+=("-t")
local_nonpersistent_flags+=("--show-token")
Expand Down
2 changes: 2 additions & 0 deletions contrib/completions/zsh/openshift
Original file line number Diff line number Diff line change
Expand Up @@ -18428,6 +18428,8 @@ _openshift_cli_whoami()
flags+=("--show-context")
flags+=("-c")
local_nonpersistent_flags+=("--show-context")
flags+=("--show-server")
local_nonpersistent_flags+=("--show-server")
flags+=("--show-token")
flags+=("-t")
local_nonpersistent_flags+=("--show-token")
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/oc-whoami.1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ user context.
\fB\-c\fP, \fB\-\-show\-context\fP=false
Print the current user context name

.PP
\fB\-\-show\-server\fP=false
Print the current server's REST API URL

.PP
\fB\-t\fP, \fB\-\-show\-token\fP=false
Print the token the current session is using. This will return an error if you are using a different form of authentication.
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/openshift-cli-whoami.1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ user context.
\fB\-c\fP, \fB\-\-show\-context\fP=false
Print the current user context name

.PP
\fB\-\-show\-server\fP=false
Print the current server's REST API URL

.PP
\fB\-t\fP, \fB\-\-show\-token\fP=false
Print the token the current session is using. This will return an error if you are using a different form of authentication.
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmd/cli/cmd/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewCmdWhoAmI(name, fullName string, f *clientcmd.Factory, out io.Writer) *c

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.")
cmd.Flags().BoolP("show-context", "c", false, "Print the current user context name")
cmd.Flags().Bool("show-server", false, "Print the current server's REST API URL")

return cmd
}
Expand Down Expand Up @@ -89,6 +90,17 @@ func RunWhoAmI(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []s
fmt.Fprintf(out, "%s\n", cfg.CurrentContext)
return nil
}
if kcmdutil.GetFlagBool(cmd, "show-server") {
cfg, err := f.OpenShiftClientConfig.RawConfig()
if err != nil {
return err
}
for _, c := range cfg.Clusters {
fmt.Fprintf(out, "%s\n", c.Server)
return nil
}
return fmt.Errorf("unable to get clusters. Cannot retrieve server URL.")
}

client, _, err := f.Clients()
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions test/cmd/whoami.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
source "$(dirname "${BASH_SOURCE}")/../../hack/lib/init.sh"
trap os::test::junit::reconcile_output EXIT

# Cleanup cluster resources created by this test
(
set +e
oc delete all,templates --all
exit 0
) &>/dev/null


os::test::junit::declare_suite_start "cmd/whoami"
# This test validates the whoami command's --show-server flag
os::cmd::expect_success_and_text 'oc whoami --show-server' 'http(s)?:\/\/[0-9\.]+\:[0-9]+'

echo "whoami: ok"
os::test::junit::declare_suite_end