2023-08-30 09:43:41 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2023-08-31 16:23:40 +01:00
|
|
|
"github.com/AlexSSD7/linsk/utils"
|
2023-08-30 09:43:41 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var cleanCmd = &cobra.Command{
|
|
|
|
|
Use: "clean",
|
2023-08-30 12:39:38 +01:00
|
|
|
Short: "Remove the Linsk data directory.",
|
2023-08-30 09:43:41 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
store := createStore()
|
|
|
|
|
|
2023-08-30 12:39:38 +01:00
|
|
|
rmPath := store.DataDirPath()
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Will permanently remove '"+rmPath+"'. Proceed? (y/n) > ")
|
2023-08-30 09:43:41 +01:00
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
answer, err := reader.ReadBytes('\n')
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("Failed to read answer", "error", err.Error())
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 16:23:40 +01:00
|
|
|
if utils.ClearUnprintableChars(strings.ToLower(string(answer)), false) != "y" {
|
2023-08-30 09:43:41 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "Aborted.\n")
|
|
|
|
|
os.Exit(2)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 12:39:38 +01:00
|
|
|
err = os.RemoveAll(rmPath)
|
2023-08-30 09:43:41 +01:00
|
|
|
if err != nil {
|
2023-08-30 12:39:38 +01:00
|
|
|
slog.Error("Failed to remove all", "error", err.Error(), "path", rmPath)
|
2023-08-30 09:43:41 +01:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 16:23:40 +01:00
|
|
|
// TODO: Clean network tap allocations, if any.
|
|
|
|
|
|
2023-08-30 12:39:38 +01:00
|
|
|
slog.Info("Deleted data directory", "path", rmPath)
|
2023-08-30 09:43:41 +01:00
|
|
|
},
|
|
|
|
|
}
|