Browse code

add option to `oc whoami` that prints server url

Fixes https://github.com/openshift/origin/issues/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
```

juanvallejo authored on 2016/10/01 06:33:29
Showing 8 changed files
... ...
@@ -13715,6 +13715,8 @@ _oc_whoami()
13715 13715
     flags+=("--show-context")
13716 13716
     flags+=("-c")
13717 13717
     local_nonpersistent_flags+=("--show-context")
13718
+    flags+=("--show-server")
13719
+    local_nonpersistent_flags+=("--show-server")
13718 13720
     flags+=("--show-token")
13719 13721
     flags+=("-t")
13720 13722
     local_nonpersistent_flags+=("--show-token")
... ...
@@ -18267,6 +18267,8 @@ _openshift_cli_whoami()
18267 18267
     flags+=("--show-context")
18268 18268
     flags+=("-c")
18269 18269
     local_nonpersistent_flags+=("--show-context")
18270
+    flags+=("--show-server")
18271
+    local_nonpersistent_flags+=("--show-server")
18270 18272
     flags+=("--show-token")
18271 18273
     flags+=("-t")
18272 18274
     local_nonpersistent_flags+=("--show-token")
... ...
@@ -13876,6 +13876,8 @@ _oc_whoami()
13876 13876
     flags+=("--show-context")
13877 13877
     flags+=("-c")
13878 13878
     local_nonpersistent_flags+=("--show-context")
13879
+    flags+=("--show-server")
13880
+    local_nonpersistent_flags+=("--show-server")
13879 13881
     flags+=("--show-token")
13880 13882
     flags+=("-t")
13881 13883
     local_nonpersistent_flags+=("--show-token")
... ...
@@ -18428,6 +18428,8 @@ _openshift_cli_whoami()
18428 18428
     flags+=("--show-context")
18429 18429
     flags+=("-c")
18430 18430
     local_nonpersistent_flags+=("--show-context")
18431
+    flags+=("--show-server")
18432
+    local_nonpersistent_flags+=("--show-server")
18431 18433
     flags+=("--show-token")
18432 18434
     flags+=("-t")
18433 18435
     local_nonpersistent_flags+=("--show-token")
... ...
@@ -27,6 +27,10 @@ user context.
27 27
     Print the current user context name
28 28
 
29 29
 .PP
30
+\fB\-\-show\-server\fP=false
31
+    Print the current server's REST API URL
32
+
33
+.PP
30 34
 \fB\-t\fP, \fB\-\-show\-token\fP=false
31 35
     Print the token the current session is using. This will return an error if you are using a different form of authentication.
32 36
 
... ...
@@ -27,6 +27,10 @@ user context.
27 27
     Print the current user context name
28 28
 
29 29
 .PP
30
+\fB\-\-show\-server\fP=false
31
+    Print the current server's REST API URL
32
+
33
+.PP
30 34
 \fB\-t\fP, \fB\-\-show\-token\fP=false
31 35
     Print the token the current session is using. This will return an error if you are using a different form of authentication.
32 36
 
... ...
@@ -53,6 +53,7 @@ func NewCmdWhoAmI(name, fullName string, f *clientcmd.Factory, out io.Writer) *c
53 53
 
54 54
 	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.")
55 55
 	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")
56 57
 
57 58
 	return cmd
58 59
 }
... ...
@@ -89,6 +90,17 @@ func RunWhoAmI(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []s
89 89
 		fmt.Fprintf(out, "%s\n", cfg.CurrentContext)
90 90
 		return nil
91 91
 	}
92
+	if kcmdutil.GetFlagBool(cmd, "show-server") {
93
+		cfg, err := f.OpenShiftClientConfig.RawConfig()
94
+		if err != nil {
95
+			return err
96
+		}
97
+		for _, c := range cfg.Clusters {
98
+			fmt.Fprintf(out, "%s\n", c.Server)
99
+			return nil
100
+		}
101
+		return fmt.Errorf("unable to get clusters. Cannot retrieve server URL.")
102
+	}
92 103
 
93 104
 	client, _, err := f.Clients()
94 105
 	if err != nil {
95 106
new file mode 100755
... ...
@@ -0,0 +1,18 @@
0
+#!/bin/bash
1
+source "$(dirname "${BASH_SOURCE}")/../../hack/lib/init.sh"
2
+trap os::test::junit::reconcile_output EXIT
3
+
4
+# Cleanup cluster resources created by this test
5
+(
6
+  set +e
7
+  oc delete all,templates --all
8
+  exit 0
9
+) &>/dev/null
10
+
11
+
12
+os::test::junit::declare_suite_start "cmd/whoami"
13
+# This test validates the whoami command's --show-server flag
14
+os::cmd::expect_success_and_text 'oc whoami --show-server' 'http(s)?:\/\/[0-9\.]+\:[0-9]+'
15
+
16
+echo "whoami: ok"
17
+os::test::junit::declare_suite_end