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

[WIP] Fix error return bug and some minor code style #4234

Merged
merged 3 commits into from
Apr 26, 2023
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
15 changes: 7 additions & 8 deletions tools/cassandra/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package cassandra

import (
"fmt"
"strings"

"github.com/urfave/cli"
Expand Down Expand Up @@ -96,7 +97,8 @@ func createKeyspace(cli *cli.Context, logger log.Logger) error {
}
keyspace := cli.String(schema.CLIOptKeyspace)
if keyspace == "" {
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError("missing "+flag(schema.CLIOptKeyspace)+" argument ")))
err := fmt.Errorf("missing %s argument", flag(schema.CLIOptKeyspace))
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError(err.Error())))
return err
}
err = doCreateKeyspace(config, keyspace, logger)
Expand All @@ -115,7 +117,8 @@ func dropKeyspace(cli *cli.Context, logger log.Logger) error {
}
keyspace := cli.String(schema.CLIOptKeyspace)
if keyspace == "" {
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError("missing "+flag(schema.CLIOptKeyspace)+" argument ")))
err := fmt.Errorf("missing %s argument", flag(schema.CLIOptKeyspace))
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError(err.Error())))
return err
}
err = doDropKeyspace(config, keyspace, logger)
Expand Down Expand Up @@ -161,12 +164,8 @@ func doDropKeyspace(cfg *CQLClientConfig, name string, logger log.Logger) error
if err != nil {
return err
}
err = client.dropKeyspace(name)
if err != nil {
return err
}
client.Close()
return nil
defer client.Close()
return client.dropKeyspace(name)
}

func newCQLClientConfig(cli *cli.Context) (*CQLClientConfig, error) {
Expand Down
26 changes: 26 additions & 0 deletions tools/cassandra/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,29 @@ func (s *HandlerTestSuite) TestParsingOfOptionsMap() {

s.Assert().Equal(0, len(parsedMap2))
}

func (s *HandlerTestSuite) TestDropKeyspaceError() {
// fake exit function to avoid exiting the application
back := osExit
defer func() { osExit = back }()
osExit = func(i int) {
s.Equal(1, i)
}
args := []string{"./tool", "drop-keyspace", "-f", "--keyspace", ""}
app := buildCLIOptions()
err := app.Run(args)
s.Nil(err)
}

func (s *HandlerTestSuite) TestCreateKeyspaceError() {
// fake exit function to avoid exiting the application
back := osExit
defer func() { osExit = back }()
osExit = func(i int) {
s.Equal(1, i)
}
args := []string{"./tool", "create-keyspace", "--keyspace", ""}
app := buildCLIOptions()
err := app.Run(args)
s.Nil(err)
}
4 changes: 3 additions & 1 deletion tools/cassandra/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func RunTool(args []string) error {
return app.Run(args)
}

var osExit = os.Exit

// root handler for all cli commands
func cliHandler(c *cli.Context, handler func(c *cli.Context, logger log.Logger) error, logger log.Logger) {
quiet := c.GlobalBool(schema.CLIOptQuiet)
err := handler(c, logger)
if err != nil && !quiet {
os.Exit(1)
osExit(1)
}
}

Expand Down