Skip to content

Commit

Permalink
refactor: separate cluster connect cmd from its implementation logic (#…
Browse files Browse the repository at this point in the history
…159)

* refactor UIX: finish basic implementation and separate cluster connect cmd

Signed-off-by: sh2 <shawnhxh@outlook.com>

* fix linter

Signed-off-by: sh2 <shawnhxh@outlook.com>

* fix ut

Signed-off-by: sh2 <shawnhxh@outlook.com>

---------

Signed-off-by: sh2 <shawnhxh@outlook.com>
  • Loading branch information
shawnh2 authored Oct 9, 2023
1 parent 05409fc commit 61684d9
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 202 deletions.
10 changes: 5 additions & 5 deletions pkg/cmd/gtctl/cluster/cluster.go → cmd/gtctl/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cluster
package main

import (
"errors"

"github.com/spf13/cobra"

"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/connect"
"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/create"
"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/delete"
"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/get"
Expand All @@ -35,20 +34,21 @@ func NewClusterCommand(l logger.Logger) *cobra.Command {
Short: "Manage GreptimeDB cluster",
Long: `Manage GreptimeDB cluster in Kubernetes`,
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
if err := cmd.Help(); err != nil {
return err
}

return errors.New("subcommand is required")
},
}

// TODO(sh2): will refactor them in the following PR.
cmd.AddCommand(create.NewCreateClusterCommand(l))
cmd.AddCommand(delete.NewDeleteClusterCommand(l))
cmd.AddCommand(scale.NewScaleClusterCommand(l))
cmd.AddCommand(get.NewGetClusterCommand(l))
cmd.AddCommand(list.NewListClustersCommand(l))
cmd.AddCommand(connect.NewConnectCommand(l))
cmd.AddCommand(NewConnectCommand(l))

return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package connect
package main

import (
"context"
Expand All @@ -23,24 +23,24 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/connect/mysql"
"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/connect/pg"
"github.com/GreptimeTeam/gtctl/pkg/connector"
"github.com/GreptimeTeam/gtctl/pkg/deployer/k8s"
"github.com/GreptimeTeam/gtctl/pkg/logger"
)

const (
connectionProtocolMySQL = "mysql"
connectionProtocolPostgres = "pg"
)

type getClusterCliOptions struct {
type clusterConnectCliOptions struct {
Namespace string
Protocol string
}

func NewConnectCommand(l logger.Logger) *cobra.Command {
var options getClusterCliOptions
const (
connectionProtocolMySQL = "mysql"
connectionProtocolPostgres = "pg"
)

var options clusterConnectCliOptions

cmd := &cobra.Command{
Use: "connect",
Short: "Connect to a GreptimeDB cluster",
Expand Down Expand Up @@ -70,29 +70,30 @@ func NewConnectCommand(l logger.Logger) *cobra.Command {
l.Errorf("cluster %s in %s not found\n", clusterName, namespace)
return nil
}

rawCluster, ok := cluster.Raw.(*greptimedbclusterv1alpha1.GreptimeDBCluster)
if !ok {
return fmt.Errorf("invalid cluster type")
}

switch options.Protocol {
case connectionProtocolMySQL:
err := mysql.ConnectCommand(rawCluster, l)
if err != nil {
if err = connector.MySQLConnectCommand(rawCluster, l); err != nil {
return fmt.Errorf("error connecting to mysql: %v", err)
}
case connectionProtocolPostgres:
err := pg.ConnectCommand(rawCluster, l)
if err != nil {
if err = connector.PostgresSQLConnectCommand(rawCluster, l); err != nil {
return fmt.Errorf("error connecting to postgres: %v", err)
}
default:
return fmt.Errorf("database type not supported")
return fmt.Errorf("database type not supported: %s", options.Protocol)
}
return nil
},
}

cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "default", "Namespace of GreptimeDB cluster.")
cmd.Flags().StringVarP(&options.Protocol, "protocol", "p", "mysql", "Specify a database")
cmd.Flags().StringVarP(&options.Protocol, "protocol", "p", "mysql", "Specify a database, like mysql or pg.")

return cmd
}
53 changes: 51 additions & 2 deletions cmd/gtctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,60 @@ import (
"fmt"
"os"

"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl"
"github.com/spf13/cobra"
"sigs.k8s.io/kind/pkg/log"

"github.com/GreptimeTeam/gtctl/pkg/logger"
"github.com/GreptimeTeam/gtctl/pkg/version"
)

func NewRootCommand() *cobra.Command {
const GtctlTextBanner = `
__ __ __
____ _/ /______/ /_/ /
/ __ '/ __/ ___/ __/ /
/ /_/ / /_/ /__/ /_/ /
\__, /\__/\___/\__/_/
/____/`

var (
verbosity int32

l = logger.New(os.Stdout, log.Level(verbosity), logger.WithColored())
)

cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "gtctl",
Short: "gtctl is a command-line tool for managing GreptimeDB cluster.",
Long: fmt.Sprintf("%s\ngtctl is a command-line tool for managing GreptimeDB cluster.", GtctlTextBanner),
Version: version.Get().String(),
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
type verboser interface {
SetVerbosity(log.Level)
}
if v, ok := l.(verboser); ok {
v.SetVerbosity(log.Level(verbosity))
return nil
}

return fmt.Errorf("logger does not implement SetVerbosity")
},
}

cmd.PersistentFlags().Int32VarP(&verbosity, "verbosity", "v", 0, "info log verbosity, higher value produces more output")

// Add all top level subcommands.
cmd.AddCommand(NewVersionCommand(l))
cmd.AddCommand(NewClusterCommand(l))
cmd.AddCommand(NewPlaygroundCommand(l))

return cmd
}

func main() {
if err := gtctl.NewRootCommand().Execute(); err != nil {
if err := NewRootCommand().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package playground
package main

import (
"github.com/lucasepe/codename"
Expand Down Expand Up @@ -41,7 +41,7 @@ func NewPlaygroundCommand(l logger.Logger) *cobra.Command {
EnableCache: false,
}

if err := create.NewCluster([]string{playgroundName}, playgroundOptions, l); err != nil {
if err = create.NewCluster([]string{playgroundName}, playgroundOptions, l); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gtctl/version/version.go → cmd/gtctl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package version
package main

import (
"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion pkg/artifacts/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestDownloadBinariesFromCNRegion(t *testing.T) {
if err != nil {
t.Errorf("failed to create source: %v", err)
}
artifactFile, err := m.DownloadTo(ctx, src, destDir(tempDir, src), &DownloadOptions{UseCache: false})
artifactFile, err := m.DownloadTo(ctx, src, destDir(tempDir, src), &DownloadOptions{EnableCache: false, BinaryInstallDir: filepath.Join(filepath.Dir(destDir(tempDir, src)), "bin")})
if err != nil {
t.Errorf("failed to download: %v", err)
}
Expand Down
24 changes: 0 additions & 24 deletions pkg/cmd/gtctl/constants/constants.go

This file was deleted.

87 changes: 0 additions & 87 deletions pkg/cmd/gtctl/root.go

This file was deleted.

Loading

0 comments on commit 61684d9

Please sign in to comment.