Skip to content

Commit 7782a81

Browse files
committed
feat: refactor connect to use top level group
1 parent 88a3957 commit 7782a81

File tree

5 files changed

+107
-3
lines changed

5 files changed

+107
-3
lines changed

pkg/cmd/cluster/cluster.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cluster
2+
3+
import (
4+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/cluster/connect"
5+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/cluster/info"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// NewServiceAccountCommand creates a new command sub-group to manage service accounts
10+
func NewClusterCommand() *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "cluster",
13+
Short: "Managed your services in OpenShift Cluster",
14+
Long: "Managed your services in OpenShift Cluster",
15+
Args: cobra.ExactArgs(1),
16+
}
17+
18+
cmd.AddCommand(
19+
info.NewInfoCommand(),
20+
connect.NewConnectCommand(),
21+
)
22+
23+
return cmd
24+
}

pkg/cmd/cluster/info/info.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package info
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/fatih/color"
9+
10+
"github.com/spf13/cobra"
11+
"k8s.io/client-go/kubernetes"
12+
"k8s.io/client-go/tools/clientcmd"
13+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
14+
"k8s.io/client-go/util/homedir"
15+
16+
// Get all auth schemes
17+
_ "k8s.io/client-go/plugin/pkg/client/auth"
18+
)
19+
20+
var statusMsg = `
21+
Namespace: %v
22+
`
23+
24+
func NewInfoCommand() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Use: "info",
27+
Short: "Prints information about your OpenShift cluster connection",
28+
Long: `Prints information about your OpenShift cluster connection`,
29+
Run: runInfo,
30+
}
31+
32+
return cmd
33+
}
34+
35+
func runInfo(cmd *cobra.Command, _ []string) {
36+
connectToCluster()
37+
}
38+
39+
func connectToCluster() {
40+
var kubeconfig string
41+
42+
if home := homedir.HomeDir(); home != "" {
43+
kubeconfig = filepath.Join(home, ".kube", "config")
44+
}
45+
46+
if !fileExists(kubeconfig) {
47+
fmt.Fprint(os.Stderr, `
48+
Command uses oc or kubectl login context file.
49+
Please make sure that you have configured access to your cluster and selected the right namespace`)
50+
return
51+
}
52+
53+
kubeClientconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
54+
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
55+
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: ""}})
56+
57+
// use the current context in kubeconfig
58+
restConfig, err := kubeClientconfig.ClientConfig()
59+
if err != nil {
60+
fmt.Fprint(os.Stderr, "\nFailed to load kube config file", err)
61+
return
62+
}
63+
64+
// create the clientset
65+
_, err = kubernetes.NewForConfig(restConfig)
66+
67+
if err != nil {
68+
fmt.Fprint(os.Stderr, "\nFailed to load kube config file", err)
69+
return
70+
}
71+
72+
currentNamespace, _, _ := kubeClientconfig.Namespace()
73+
74+
fmt.Fprintf(os.Stderr, statusMsg, color.HiGreenString(currentNamespace))
75+
}
76+
77+
func fileExists(path string) bool {
78+
_, err := os.Stat(path)
79+
return !os.IsNotExist(err)
80+
}

pkg/cmd/kafka/kafka.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package kafka
55
import (
66
"github.com/spf13/cobra"
77

8-
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/kafka/connect"
98
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/kafka/create"
109
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/kafka/delete"
1110
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/kafka/describe"
@@ -31,7 +30,6 @@ func NewKafkaCommand() *cobra.Command {
3130
use.NewUseCommand(),
3231
status.NewStatusCommand(),
3332
topics.NewTopicsCommand(),
34-
connect.NewConnectCommand())
35-
33+
)
3634
return cmd
3735
}

pkg/cmd/root/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package root
22

33
import (
44
"github.com/MakeNowJust/heredoc"
5+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/cluster"
56
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/completion"
67
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/docs"
78
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/kafka"
@@ -31,6 +32,7 @@ func NewRootCommand(version string) *cobra.Command {
3132
cmd.AddCommand(kafka.NewKafkaCommand())
3233
cmd.AddCommand(serviceaccount.NewServiceAccountCommand())
3334
cmd.AddCommand(docs.NewDocsCommand())
35+
cmd.AddCommand(cluster.NewClusterCommand())
3436
cmd.AddCommand(completion.CompletionCmd)
3537

3638
return cmd

0 commit comments

Comments
 (0)