Skip to content

Commit

Permalink
feat: add command to delete a database (#37)
Browse files Browse the repository at this point in the history
* feat: add command to delete a database

* refactor: accept DB as @db-name for deletion

* fix: return don't print err

* feat: add warning style to confirmation

* fix: show input in 'not found' msg
  • Loading branch information
bashbunni authored May 30, 2023
1 parent 50f1597 commit a1fabf4
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/charmbracelet/charm/cmd"
"github.com/charmbracelet/charm/kv"
"github.com/charmbracelet/charm/ui/common"
"github.com/charmbracelet/lipgloss"
"github.com/dgraph-io/badger/v3"
mcobra "github.com/muesli/mango-cobra"
"github.com/muesli/roff"
Expand All @@ -28,6 +29,8 @@ var (
showBinary bool
delimiterIterate string

warningStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FD5B5B"))

rootCmd = &cobra.Command{
Use: "skate",
Short: "Skate, a personal key value store.",
Expand Down Expand Up @@ -74,6 +77,14 @@ var (
RunE: listDbs,
}

deleteDbCmd = &cobra.Command{
Use: "delete-db [@DB]",
Hidden: false,
Short: "Delete a database",
Args: cobra.MinimumNArgs(1),
RunE: deleteDb,
}

syncCmd = &cobra.Command{
Use: "sync [@DB]",
Short: "Sync local db with latest Charm Cloud db.",
Expand Down Expand Up @@ -172,6 +183,47 @@ func listDbs(cmd *cobra.Command, args []string) error {
return nil
}

func deleteDb(cmd *cobra.Command, args []string) error {
n, err := nameFromArgs(args)
if err != nil {
return err
}
cc, err := client.NewClientWithDefaults()
if err != nil {
return err
}
dd, err := cc.DataPath()
if err != nil {
return err
}
dbs, err := os.ReadDir(filepath.Join(dd, "kv"))
if err != nil {
return err
}

var found bool
for _, d := range dbs {
if d.IsDir() && d.Name() == n {
found = true
var confirmation string
fmt.Println(warningStyle.Render("are you sure you want to delete " + d.Name() + " and all its contents? (y/n)"))
fmt.Scanln(&confirmation)
if confirmation == "y" {
err := os.RemoveAll(filepath.Join(dd, "kv", d.Name()))
if err != nil {
return err
}
} else {
fmt.Println("did not delete " + d.Name())
}
}
}
if !found {
fmt.Println(args[0] + " does not exist")
}
return nil
}

func list(cmd *cobra.Command, args []string) error {
var k string
var pf string
Expand Down Expand Up @@ -328,6 +380,7 @@ func init() {
deleteCmd,
listCmd,
listDbsCmd,
deleteDbCmd,
syncCmd,
resetCmd,
cmd.LinkCmd("skate"),
Expand Down

0 comments on commit a1fabf4

Please sign in to comment.