2023-07-17 03:23:26 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-02-08 16:35:07 +01:00
|
|
|
"encoding/json"
|
2023-12-22 10:44:49 +01:00
|
|
|
"fmt"
|
2023-12-26 21:52:49 +01:00
|
|
|
"time"
|
2023-12-22 10:44:49 +01:00
|
|
|
|
2024-05-04 01:06:24 +02:00
|
|
|
"github.com/quexten/goldwarden/cli/ipc/messages"
|
2023-07-17 03:23:26 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var vaultCmd = &cobra.Command{
|
|
|
|
Use: "vault",
|
|
|
|
Short: "Manage the vault",
|
|
|
|
Long: `Manage the vault.`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var unlockCmd = &cobra.Command{
|
|
|
|
Use: "unlock",
|
|
|
|
Short: "Unlocks the vault",
|
|
|
|
Long: `Unlocks the vault. You will be prompted for your pin. The pin is empty by default.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-09-20 03:05:44 +02:00
|
|
|
request := messages.UnlockVaultRequest{}
|
2023-07-17 03:23:26 +02:00
|
|
|
|
2023-08-21 13:52:06 +02:00
|
|
|
result, err := commandClient.SendToAgent(request)
|
2023-07-17 03:23:26 +02:00
|
|
|
if err != nil {
|
2023-09-11 22:45:01 +02:00
|
|
|
handleSendToAgentError(err)
|
2023-07-17 03:23:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch result.(type) {
|
2023-09-20 03:05:44 +02:00
|
|
|
case messages.ActionResponse:
|
|
|
|
if result.(messages.ActionResponse).Success {
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Unlocked")
|
2023-07-17 03:23:26 +02:00
|
|
|
} else {
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Not unlocked: " + result.(messages.ActionResponse).Message)
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
default:
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Wrong response type")
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var lockCmd = &cobra.Command{
|
|
|
|
Use: "lock",
|
|
|
|
Short: "Locks the vault",
|
|
|
|
Long: `Locks the vault.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-09-20 03:05:44 +02:00
|
|
|
request := messages.LockVaultRequest{}
|
2023-07-17 03:23:26 +02:00
|
|
|
|
2023-08-21 13:52:06 +02:00
|
|
|
result, err := commandClient.SendToAgent(request)
|
2023-07-17 03:23:26 +02:00
|
|
|
if err != nil {
|
2023-09-11 22:45:01 +02:00
|
|
|
handleSendToAgentError(err)
|
2023-07-17 03:23:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch result.(type) {
|
2023-09-20 03:05:44 +02:00
|
|
|
case messages.ActionResponse:
|
|
|
|
if result.(messages.ActionResponse).Success {
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Locked")
|
2023-07-17 03:23:26 +02:00
|
|
|
} else {
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Not locked: " + result.(messages.ActionResponse).Message)
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
default:
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Wrong response type")
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var purgeCmd = &cobra.Command{
|
|
|
|
Use: "purge",
|
|
|
|
Short: "Wipes the vault",
|
|
|
|
Long: `Wipes the vault and encryption keys from ram and config. Does not delete any entries on the server side.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-09-20 03:05:44 +02:00
|
|
|
request := messages.WipeVaultRequest{}
|
2023-07-17 03:23:26 +02:00
|
|
|
|
2023-08-21 13:52:06 +02:00
|
|
|
result, err := commandClient.SendToAgent(request)
|
2023-07-17 03:23:26 +02:00
|
|
|
if err != nil {
|
2023-09-11 22:45:01 +02:00
|
|
|
handleSendToAgentError(err)
|
2023-07-17 03:23:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch result.(type) {
|
2023-09-20 03:05:44 +02:00
|
|
|
case messages.ActionResponse:
|
|
|
|
if result.(messages.ActionResponse).Success {
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Purged")
|
2023-07-17 03:23:26 +02:00
|
|
|
} else {
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Not purged: " + result.(messages.ActionResponse).Message)
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
default:
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Wrong response type")
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-22 10:44:49 +01:00
|
|
|
var statusCmd = &cobra.Command{
|
|
|
|
Use: "status",
|
|
|
|
Short: "Shows the vault status",
|
|
|
|
Long: `Shows the vault status.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
request := messages.VaultStatusRequest{}
|
|
|
|
|
|
|
|
result, err := commandClient.SendToAgent(request)
|
|
|
|
if err != nil {
|
|
|
|
handleSendToAgentError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch result.(type) {
|
|
|
|
case messages.VaultStatusResponse:
|
2024-02-08 16:35:07 +01:00
|
|
|
response := map[string]interface{}{}
|
2023-12-22 10:44:49 +01:00
|
|
|
status := result.(messages.VaultStatusResponse)
|
2024-02-08 16:35:07 +01:00
|
|
|
response["locked"] = status.Locked
|
|
|
|
response["loginEntries"] = status.NumberOfLogins
|
|
|
|
response["noteEntries"] = status.NumberOfNotes
|
|
|
|
response["lastSynced"] = time.Unix(status.LastSynced, 0).String()
|
|
|
|
response["websocketConnected"] = status.WebsockedConnected
|
|
|
|
response["pinSet"] = status.PinSet
|
|
|
|
response["loggedIn"] = status.LoggedIn
|
|
|
|
responseJSON, _ := json.Marshal(response)
|
|
|
|
fmt.Println(string(responseJSON))
|
2023-12-22 10:44:49 +01:00
|
|
|
default:
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Wrong response type")
|
2023-12-22 10:44:49 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-17 03:23:26 +02:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(vaultCmd)
|
|
|
|
vaultCmd.AddCommand(unlockCmd)
|
|
|
|
vaultCmd.AddCommand(lockCmd)
|
|
|
|
vaultCmd.AddCommand(purgeCmd)
|
2023-12-22 10:44:49 +01:00
|
|
|
vaultCmd.AddCommand(statusCmd)
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|