-
Notifications
You must be signed in to change notification settings - Fork 649
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
Add Inspect
method & command to inspect db structure
#674
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
func newInspectCobraCommand() *cobra.Command { | ||
inspectCmd := &cobra.Command{ | ||
Use: "inspect", | ||
Short: "inspect the structure of the database", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return errors.New("db file path not provided") | ||
} | ||
if len(args) > 1 { | ||
return errors.New("too many arguments") | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return inspectFunc(args[0]) | ||
}, | ||
} | ||
|
||
return inspectCmd | ||
} | ||
|
||
func inspectFunc(srcDBPath string) error { | ||
if _, err := checkSourceDBPath(srcDBPath); err != nil { | ||
return err | ||
} | ||
|
||
db, err := bolt.Open(srcDBPath, 0600, &bolt.Options{ReadOnly: true}) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
return db.View(func(tx *bolt.Tx) error { | ||
bs := tx.Inspect() | ||
out, err := json.MarshalIndent(bs, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintln(os.Stdout, string(out)) | ||
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,27 @@ | ||
package main_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
main "go.etcd.io/bbolt/cmd/bbolt" | ||
"go.etcd.io/bbolt/internal/btesting" | ||
) | ||
|
||
func TestInspect(t *testing.T) { | ||
pageSize := 4096 | ||
db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize}) | ||
srcPath := db.Path() | ||
db.Close() | ||
|
||
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) | ||
|
||
rootCmd := main.NewRootCommand() | ||
rootCmd.SetArgs([]string{ | ||
"inspect", srcPath, | ||
}) | ||
err := rootCmd.Execute() | ||
require.NoError(t, err) | ||
} |
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
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,16 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func checkSourceDBPath(srcPath string) (os.FileInfo, error) { | ||
fi, err := os.Stat(srcPath) | ||
if os.IsNotExist(err) { | ||
return nil, fmt.Errorf("source database file %q doesn't exist", srcPath) | ||
} else if err != nil { | ||
return nil, fmt.Errorf("failed to open source database file %q: %v", srcPath, err) | ||
} | ||
return fi, 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
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.
Just curious: was there a reason not to add this to the
Run
method as well?Currently it seems the
inspect
command does not work, but the code is there thanks to this commit. trying to understand if that's intentionalThere 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.
When the commands are not found in the Run method, bbolt will automatically try to run the cobra commands,
bbolt/cmd/bbolt/main.go
Lines 68 to 69 in 92c7414
Have you tried it out? This isn't the first time I see this false statement. Please try it out, let me know whether you really have issue.
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.
Using
1.3.11
I get: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.
Ok it seems the latest released version v1.3.11 does not have the changes you referenced (
bbolt/cmd/bbolt/main.go
Line 69 in d128a10
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.
New features, including this one, only go into
main
branch.So please try the main branch or v1.4.0-beta.0
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.
Refer to https://github.com/etcd-io/bbolt/blob/main/CHANGELOG/CHANGELOG-1.4.md