Skip to content

Commit

Permalink
feat(horaectl): initial commit (#1450)
Browse files Browse the repository at this point in the history
## Rationale
Introduce command line tool for horaedb, `horaectl`.

## Detailed Changes
* Initial commit.
* Support `list clusters` and `cluster diagnose`.
```
go run main.go --meta_addr 127.0.0.1:8080 --cluster_name defaultCluster
127.0.0.1:8080(defaultCluster) > c  
+----+----------------+------------+--------------+-----------------------------+-------------------------+-------------------------+
| ID | NAME           | SHARDTOTAL | TOPOLOGYTYPE | PROCEDUREEXECUTINGBATCHSIZE | CREATEDAT               | MODIFIEDAT              |
+----+----------------+------------+--------------+-----------------------------+-------------------------+-------------------------+
|  0 | defaultCluster |          8 | static       |                  4294967295 | 2024-01-19 15:18:57.034 | 2024-01-19 15:18:57.034 |
+----+----------------+------------+--------------+-----------------------------+-------------------------+-------------------------+
127.0.0.1:8080(defaultCluster) > c d
+---------------------+-------------------------+--------------------------+-----------------------+
| UNREGISTERED_SHARDS | UNREADY_SHARDS:SHARD_ID | UNREADY_SHARDS:NODE_NAME | UNREADY_SHARDS:STATUS |
+---------------------+-------------------------+--------------------------+-----------------------+
| []                  |                         |                          |                       |
+---------------------+-------------------------+--------------------------+-----------------------+

```

## Test Plan
Manual test.
  • Loading branch information
chunshao90 authored Jan 22, 2024
1 parent f0491bf commit a1041cd
Show file tree
Hide file tree
Showing 13 changed files with 710 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ctl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# HoraeCTL

![License](https://img.shields.io/badge/license-Apache--2.0-green.svg)

HoraeCTL is the operation tool for managing the HoraeDB cluster.

## Quick Start
TODO

## Contributing
The project is under rapid development so that any contribution is welcome.

## License
HoraeCTL is under [Apache License 2.0](./LICENSE).
38 changes: 38 additions & 0 deletions ctl/cmd/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package cmd

import (
"github.com/apache/incubator-horaedb/ctl/operation"
"github.com/spf13/cobra"
)

var clusterCmd = &cobra.Command{
Use: "cluster",
Aliases: []string{"c"},
Short: "Operations on cluster",
Run: func(cmd *cobra.Command, args []string) {
operation.ClustersList()
},
}

func init() {
rootCmd.AddCommand(clusterCmd)
}
38 changes: 38 additions & 0 deletions ctl/cmd/diagnose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package cmd

import (
"github.com/apache/incubator-horaedb/ctl/operation"
"github.com/spf13/cobra"
)

var diagnoseCmd = &cobra.Command{
Use: "diagnose",
Aliases: []string{"d"},
Short: "Cluster diagnose",
Run: func(cmd *cobra.Command, args []string) {
operation.ClusterDiagnose()
},
}

func init() {
clusterCmd.AddCommand(diagnoseCmd)
}
40 changes: 40 additions & 0 deletions ctl/cmd/quit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package cmd

import (
"github.com/spf13/cobra"

"os"
)

var quitCmd = &cobra.Command{
Use: "quit",
Aliases: []string{"q", "exit"},
Short: "Quit horaectl",
Run: func(cmd *cobra.Command, args []string) {
println("Bye!")
os.Exit(0)
},
}

func init() {
rootCmd.AddCommand(quitCmd)
}
130 changes: 130 additions & 0 deletions ctl/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package cmd

import (
"bufio"
"fmt"
"io"
"os"
"strings"

"github.com/apache/incubator-horaedb/ctl/operation"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var rootCmd = &cobra.Command{
Use: "horaectl",
Short: "horaectl is a command line tool for HoraeDB",
Run: func(cmd *cobra.Command, args []string) {},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}

for _, arg := range os.Args {
if arg == "-h" || arg == "--help" {
os.Exit(0)
}
}

for {
printPrompt(viper.GetString(operation.RootMetaAddr), viper.GetString(operation.RootCluster))
err = ReadArgs(os.Stdin)
if err != nil {
fmt.Println(err)
continue
}
if err = rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Args = []string{}
}
}
}

func init() {
rootCmd.PersistentFlags().String(operation.RootMetaAddr, "127.0.0.1:8080", "meta addr is used to connect to meta server")
viper.BindPFlag(operation.RootMetaAddr, rootCmd.PersistentFlags().Lookup(operation.RootMetaAddr))

rootCmd.PersistentFlags().StringP(operation.RootCluster, "c", "defaultCluster", "")
viper.BindPFlag(operation.RootCluster, rootCmd.PersistentFlags().Lookup(operation.RootCluster))

rootCmd.CompletionOptions = cobra.CompletionOptions{
DisableDefaultCmd: true,
DisableNoDescFlag: true,
DisableDescriptions: true,
HiddenDefaultCmd: true,
}
}

func printPrompt(address, cluster string) {
fmt.Printf("%s(%s) > ", address, cluster)
}

// ReadArgs Forked from https://github.com/apache/incubator-seata-ctl/blob/8427314e04cdc435b925ed41573b37e3addeea34/action/common/args.go#L29
func ReadArgs(in io.Reader) error {
os.Args = []string{""}

scanner := bufio.NewScanner(in)

var lines []string

for scanner.Scan() {
line := strings.Trim(scanner.Text(), "\r\n ")
if line == "" {
return nil
}
if line[len(line)-1] == '\\' {
line = line[:len(line)-1]
lines = append(lines, line)
} else {
lines = append(lines, line)
break
}
}

argsStr := strings.Join(lines, " ")
rawArgs := strings.Split(argsStr, "'")

if len(rawArgs) != 1 && len(rawArgs) != 3 {
return errors.New("read args from input error")
}

args := strings.Split(rawArgs[0], " ")

if len(rawArgs) == 3 {
args = append(args, rawArgs[1])
args = append(args, strings.Split(rawArgs[2], " ")...)
}

for _, arg := range args {
if arg != "" {
os.Args = append(os.Args, strings.TrimSpace(arg))
}
}
return nil
}
91 changes: 91 additions & 0 deletions ctl/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// Forked from https://github.com/apache/incubator-seata-ctl/blob/8427314e04cdc435b925ed41573b37e3addeea34/action/common/args_test.go.

package cmd

import (
"bytes"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

var argsTestCases = []struct {
input string
args []string
valid bool
}{
{
`-a xxx -b yyy -c ' { "a": "b", "c": "d" }' -d -e`,
[]string{"-a", "xxx", "-b", "yyy", "-c", `{ "a": "b", "c": "d" }`, "-d", "-e"},
true,
},
{
`-a xxx -b yyy \
-c \
' { \
"a": "b", \
"c": "d" \
}' \
-d \
-e`,
[]string{"-a", "xxx", "-b", "yyy", "-c", `{ "a": "b", "c": "d" }`, "-d", "-e"},
true,
},
{
`-a xxx -b yyy
-c \
' { \
"a": "b", \
"c": "d" \
}' \
-d \
-e`,
[]string{"-a", "xxx", "-b", "yyy"},
true,
},
{
`-a \
' { \
"a": "b" \
-b`,
[]string{},
false,
},
}

func TestReadArgs(t *testing.T) {
var stdin bytes.Buffer
for _, testCase := range argsTestCases {
stdin.Reset()
stdin.Write([]byte(testCase.input))
if !testCase.valid {
assert.NotNil(t, ReadArgs(&stdin))
continue
}
assert.Nil(t, ReadArgs(&stdin))
assert.Equal(t, len(os.Args), len(testCase.args))
for i := 0; i < len(os.Args); i++ {
assert.Equal(t, os.Args[i], testCase.args[i])
}
}
}
38 changes: 38 additions & 0 deletions ctl/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module github.com/apache/incubator-horaedb/ctl

go 1.21.2

require (
github.com/jedib0t/go-pretty/v6 v6.5.3
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit a1041cd

Please sign in to comment.