-
Notifications
You must be signed in to change notification settings - Fork 648
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #723 from Elbehery/migrate_check_cobra
cmd: migrate `check` command to cobra style
- Loading branch information
Showing
5 changed files
with
92 additions
and
92 deletions.
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