-
Notifications
You must be signed in to change notification settings - Fork 648
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
cmd: migrate check
command to cobra style
#723
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
"go.etcd.io/bbolt/internal/guts_cli" | ||
) | ||
|
||
func newCheckCommand() *cobra.Command { | ||
checkCmd := &cobra.Command{ | ||
Use: "check <bbolt-file>", | ||
Short: "verify integrity of bbolt database data", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return checkFunc(cmd, args[0]) | ||
}, | ||
} | ||
|
||
return checkCmd | ||
} | ||
|
||
func checkFunc(cmd *cobra.Command, dbPath string) error { | ||
if _, err := checkSourceDBPath(dbPath); err != nil { | ||
return err | ||
} | ||
|
||
// Open database. | ||
db, err := bolt.Open(dbPath, 0600, &bolt.Options{ | ||
ReadOnly: true, | ||
PreLoadFreelist: true, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
// Perform consistency check. | ||
return db.View(func(tx *bolt.Tx) error { | ||
var count int | ||
for err := range tx.Check(bolt.WithKVStringer(CmdKvStringer())) { | ||
fmt.Fprintln(cmd.OutOrStdout(), err) | ||
count++ | ||
} | ||
|
||
// Print summary of errors. | ||
if count > 0 { | ||
fmt.Fprintf(cmd.OutOrStdout(), "%d errors found\n", count) | ||
return guts_cli.ErrCorrupt | ||
} | ||
|
||
// Notify user that database is valid. | ||
fmt.Fprintln(cmd.OutOrStdout(), "OK") | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
main "go.etcd.io/bbolt/cmd/bbolt" | ||
"go.etcd.io/bbolt/internal/btesting" | ||
) | ||
|
||
func TestCheckCommand_Run(t *testing.T) { | ||
db := btesting.MustCreateDB(t) | ||
db.Close() | ||
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) | ||
|
||
rootCmd := main.NewRootCommand() | ||
// capture output for assertion | ||
outputBuf := bytes.NewBufferString("") | ||
rootCmd.SetOut(outputBuf) | ||
|
||
rootCmd.SetArgs([]string{ | ||
"check", db.Path(), | ||
}) | ||
err := rootCmd.Execute() | ||
require.NoError(t, err) | ||
|
||
output, err := io.ReadAll(outputBuf) | ||
require.NoError(t, err) | ||
require.Equalf(t, "OK\n", string(output), "unexpected stdout:\n\n%s", string(output)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while we're at it, can you add some test that exercises the corruption?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR just migrates the check command to cobra style. This comment is an enhancement request. It's OK to discuss it separately.
I think the purpose of CLI's test cases are just to verify the CLI itself instead of the functionalities. Also we already some test cases to verify the corrupted pages. FYI. https://github.com/etcd-io/bbolt/blob/main/tx_check_test.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes the corruption check has been test already, this test case just to verify the cli is working as expected 👍🏽
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, part of testing that CLI is also ensuring this case works. Feel free to do it as a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am confused now, so is this
or just adding new test for the corruption check, because we already have tests for this https://github.com/etcd-io/bbolt/blob/main/tx_check_test.go
just to make sure we are all on the same page