-
Notifications
You must be signed in to change notification settings - Fork 53
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
feat: CLI purge command #2998
feat: CLI purge command #2998
Changes from 3 commits
eeb02ff
c9dd6a9
f1f20be
162dccb
165d37f
f5d2995
d6ecc7a
53dde96
62116d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func MakePurgeCommand() *cobra.Command { | ||
var force bool | ||
var cmd = &cobra.Command{ | ||
Use: "purge", | ||
Short: "Delete all persisted DefraDB data", | ||
Long: `Delete all persisted DefraDB data. | ||
WARNING this operation will delete all data and cannot be reversed.`, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
if err := setContextRootDir(cmd); err != nil { | ||
return err | ||
} | ||
return setContextConfig(cmd) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if !force { | ||
return ErrPurgeForceFlagRequired | ||
} | ||
cfg := mustGetContextConfig(cmd) | ||
return os.RemoveAll(cfg.GetString("datastore.badger.path")) | ||
}, | ||
} | ||
cmd.Flags().BoolVarP(&force, "force", "f", false, "Must be set for the operation to run") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: I don't think requiring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only remember the force flag from the discussion. As long as its non interactive I'm open to changing it to anything else. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we had discussed the possibility of having a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't remember this, but I like it - is kind of like a build flag without the hassle of multiple builds, and like you said it scales to other situations very nicely too |
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPurgeCommandWithoutForceFlag_ReturnsError(t *testing.T) { | ||
rootDir := t.TempDir() | ||
|
||
cmd := NewDefraCommand() | ||
cmd.SetArgs([]string{"purge", "--rootdir", rootDir}) | ||
|
||
err := cmd.Execute() | ||
require.ErrorIs(t, err, ErrPurgeForceFlagRequired) | ||
} | ||
|
||
func TestPurgeCommandWithEmptyDirectory_DoesNothing(t *testing.T) { | ||
rootDir := t.TempDir() | ||
|
||
cmd := NewDefraCommand() | ||
cmd.SetArgs([]string{"purge", "--force", "--rootdir", rootDir}) | ||
|
||
err := cmd.Execute() | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestPurgeCommandWithForceFlag_Succeeds(t *testing.T) { | ||
rootDir := t.TempDir() | ||
|
||
var dataDir string | ||
// load the config and create the data directory | ||
configLoader = func(rootdir string, flags *pflag.FlagSet) (*viper.Viper, error) { | ||
cfg, err := loadConfig(rootdir, flags) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dataDir = cfg.GetString("datastore.badger.path") | ||
if err := os.MkdirAll(dataDir, 0755); err != nil { | ||
return nil, err | ||
} | ||
return cfg, nil | ||
} | ||
|
||
cmd := NewDefraCommand() | ||
cmd.SetArgs([]string{"purge", "--force", "--rootdir", rootDir}) | ||
|
||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
|
||
assert.NoDirExists(t, dataDir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: This test does not test that the badger data directory is cleared, but the hardcoded path set in the test (that currently corresponds with the default badger path). Please either change this test (or add another one) so that it will test that the correct directory is cleared, no matter what value is set within There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you need to change the prod code to do this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was reverted after the team discussion |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## defradb purge | ||
|
||
Delete all persisted DefraDB data | ||
|
||
### Synopsis | ||
|
||
Delete all persisted DefraDB data. | ||
WARNING this operation will delete all data and cannot be reversed. | ||
|
||
``` | ||
defradb purge [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-f, --force Must be set for the operation to run | ||
-h, --help help for purge | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
--keyring-backend string Keyring backend to use. Options are file or system (default "file") | ||
--keyring-namespace string Service name to use when using the system backend (default "defradb") | ||
--keyring-path string Path to store encrypted keys when using the file backend (default "keys") | ||
--log-format string Log format to use. Options are text or json (default "text") | ||
--log-level string Log level to use. Options are debug, info, error, fatal (default "info") | ||
--log-output string Log output path. Options are stderr or stdout. (default "stderr") | ||
--log-overrides string Logger config overrides. Format <name>,<key>=<val>,...;<name>,... | ||
--log-source Include source location in logs | ||
--log-stacktrace Include stacktrace in error and fatal logs | ||
--no-keyring Disable the keyring and generate ephemeral keys | ||
--no-log-color Disable colored log output | ||
--rootdir string Directory for persistent data (default: $HOME/.defradb) | ||
--source-hub-address string The SourceHub address authorized by the client to make SourceHub transactions on behalf of the actor | ||
--url string URL of HTTP endpoint to listen on or connect to (default "127.0.0.1:9181") | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [defradb](defradb.md) - DefraDB Edge Database | ||
|
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.
thought: This will only clear the badger directory, if the user is using another datastore this command will not work (unclear if will panic, or quietly do nothing), and the added tests do not automatically cover other dbs. I cannot remember how hard-coded other CLI commands are to badger, this might be a drop in the ocean when it comes to adding support for other datastores in the CLI.
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.
When we add more datastores we can have them all use the same
datastore.path
as their root path and have sub directories for each datastore type. Then we just need to delete the root path to remove them all.