linsk/cmd/clean.go

45 lines
985 B
Go
Raw Normal View History

2023-08-30 09:43:41 +01:00
package cmd
import (
"bufio"
"fmt"
"log/slog"
"os"
"strings"
"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)
}
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)
}
// 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
},
}