forked from etcd-io/bbolt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
etcd-io#472 migrate info command to cobra style
Signed-off-by: tommy shem <[email protected]>
- Loading branch information
tommy shem
committed
Nov 12, 2024
1 parent
b89ce20
commit d85f7bb
Showing
5 changed files
with
83 additions
and
71 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,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
bolt "go.etcd.io/bbolt" | ||
) // infoCommand represents the "info" command execution. | ||
type infoCommand struct { | ||
baseCommand | ||
} | ||
|
||
// newInfoCommand returns a infoCommand. | ||
func newInfoCommand() *cobra.Command { | ||
var infoCmd = &cobra.Command{ | ||
Use: "info <path>", | ||
Short: "Info prints basic information about the Bolt database at PATH.", | ||
Long: "Info prints basic information about the Bolt database at PATH.", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return info(cmd, args[0]) | ||
}, | ||
} | ||
return infoCmd | ||
} | ||
|
||
// Run executes the command. | ||
func info(cmd *cobra.Command, path string) error { | ||
|
||
// Require database path. | ||
if path == "" { | ||
return ErrPathRequired | ||
} else if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return ErrFileNotFound | ||
} | ||
|
||
// Open the database. | ||
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true}) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
// Print basic database info. | ||
info := db.Info() | ||
fmt.Fprintf(cmd.OutOrStdout(), "Page Size: %d\n", info.PageSize) | ||
|
||
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,32 @@ | ||
package main_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
main "go.etcd.io/bbolt/cmd/bbolt" | ||
"go.etcd.io/bbolt/internal/btesting" | ||
) | ||
|
||
// Ensure the "info" command can print information about a database. | ||
// | ||
// This test case verifies that the "info" command can print information about a | ||
// database. It checks that the output of the command matches the expected result. | ||
func TestInfoCommand_Run(t *testing.T) { | ||
// Create a test database. | ||
db := btesting.MustCreateDB(t) | ||
db.Close() | ||
|
||
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) | ||
|
||
// Run the info command. | ||
t.Log("Running info command") | ||
rootCmd := main.NewRootCommand() | ||
_, actualOutput, err := executeCommand(rootCmd, "info", db.Path()) | ||
require.NoError(t, err) | ||
|
||
// check results | ||
t.Log("Verify result") | ||
expected := "Page Size: 4096\n" | ||
require.EqualValues(t, expected, actualOutput) | ||
} |
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