Added useLocalDatabase global flag to allow for local db files for separate engagements.

This commit is contained in:
Evan Hosinski
2025-05-15 15:08:32 -04:00
parent bb43695822
commit 93c9a353ca
6 changed files with 409 additions and 50 deletions
+32 -4
View File
@@ -6,11 +6,12 @@ import (
"github.com/spf13/cobra"
"go.uber.org/zap"
"os"
"strings"
)
var (
// Global Flags
alternativeDatabase string
useLocalDatabase bool
// rootCmd is the base command for the CLI.
rootCmd = &cobra.Command{
@@ -58,14 +59,15 @@ func Execute() {
}
func init() {
// Attempt to retreive the useLocalDB
useLocalDatabase = badger.GetUseLocalDB()
// Hide the default help command
rootCmd.CompletionOptions.HiddenDefaultCmd = true
// Add global flags
rootCmd.PersistentFlags().StringVar(&alternativeDatabase, "alt-db", "", "Alternative database path")
// Add subcommands
rootCmd.AddCommand(setKeyCmd)
rootCmd.AddCommand(setLocalDb)
}
// Command to set API key
@@ -85,6 +87,32 @@ var setKeyCmd = &cobra.Command{
},
}
var setLocalDb = &cobra.Command{
Use: "set-local-db [true|false]",
Short: "Set dehasher to use a local database path instead of the default (must be unset to use default)",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
useLocal := strings.ToLower(args[0])
if useLocal == "true" {
useLocalDatabase = true
} else if useLocal == "false" {
useLocalDatabase = false
} else {
fmt.Println("Invalid argument. Please use 'true' or 'false'.")
return
}
// Store useLocal in badger DB
err := badger.StoreUseLocalDB(useLocalDatabase)
if err != nil {
fmt.Printf("Error storing local database useLocal: %v\n", err)
return
}
fmt.Println("Local database useLocal stored successfully")
},
}
// Helper functions to store API credentials
func storeApiKey(key string) error {
err := badger.StoreKey(key)