add basic subcommands list,generate,show,delete

This commit is contained in:
2024-10-07 14:51:05 +02:00
parent fcdad8c2a3
commit 2fefbe5e6c
4 changed files with 181 additions and 38 deletions

View File

@@ -3,8 +3,10 @@ package main
import (
"flag"
"fmt"
"os"
logging "github.com/ipfs/go-log/v2"
"github.com/spf13/cobra"
"github.com/k4lipso/pentapass/rpc"
"github.com/k4lipso/pentapass/crypto"
@@ -18,53 +20,189 @@ var (
// config = "globaldb-example"
)
// Create the root command
var rootCmd = &cobra.Command{
Use: "ppass",
Short: "Interact with the Password Store",
}
// Create the 'list' subcommand
var listCmd = &cobra.Command{
Use: "list",
Short: "List all passwords",
Run: func(cmd *cobra.Command, args []string) {
//all, _ := cmd.Flags().GetBool("all")
dbPath, _ := cmd.Flags().GetString("db")
client, err := rpc.Receive(dbPath)
if err != nil {
fmt.Printf("dialing: %s\n", err)
return
}
var names []string
namespace := "root"
err = client.Call("Query.GetAllNames", &namespace, &names)
if err != nil {
fmt.Println(err)
}
for _, name := range names {
fmt.Println(name)
}
},
}
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate a Password",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
dbPath, _ := cmd.Flags().GetString("db")
client, err := rpc.Receive(dbPath)
if err != nil {
fmt.Printf("dialing: %s\n", err)
return
}
serviceName := args[0]
var password *crypto.Password
np := rpc.NamespaceService{ Namespace: "root", Service: serviceName }
err = client.Call("Query.Generate", &np, &password)
if err != nil {
fmt.Println(err)
}
fmt.Println(*password)
},
}
var showCmd = &cobra.Command{
Use: "show",
Short: "show a Password",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
dbPath, _ := cmd.Flags().GetString("db")
client, err := rpc.Receive(dbPath)
if err != nil {
fmt.Printf("dialing: %s\n", err)
return
}
serviceName := args[0]
var password *crypto.Password
np := rpc.NamespaceService{ Namespace: "root", Service: serviceName }
err = client.Call("Query.Get", &np, &password)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(password.Password)
},
}
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete a Password",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
dbPath, _ := cmd.Flags().GetString("db")
client, err := rpc.Receive(dbPath)
if err != nil {
fmt.Printf("dialing: %s\n", err)
return
}
serviceName := args[0]
var success *bool
np := rpc.NamespaceService{ Namespace: "root", Service: serviceName }
err = client.Call("Query.Delete", &np, &success)
if err != nil {
fmt.Println(err)
}
fmt.Println(*success)
},
}
func init() {
// Add flags to the 'list' command
//listCmd.Flags().BoolP("all", "a", false, "List all items")
rootCmd.PersistentFlags().String("db", "", "db path")
rootCmd.MarkPersistentFlagRequired("db")
// Add subcommands to the root command
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(generateCmd)
rootCmd.AddCommand(showCmd)
rootCmd.AddCommand(deleteCmd)
}
func main() {
flag.Parse()
client, err := rpc.Receive(*dbPath)
if err != nil {
fmt.Printf("dialing: %s\n", err)
return
}
var names []string
namespace := "root"
err = client.Call("Query.GetAllNames", &namespace, &names)
if err != nil {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(names)
//flag.Parse()
var password *crypto.Password
np := rpc.NamespaceService{ Namespace: "root", Service: "Test" }
err = client.Call("Query.Generate", &np, &password)
//client, err := rpc.Receive(*dbPath)
if err != nil {
fmt.Println(err)
}
//if err != nil {
// fmt.Printf("dialing: %s\n", err)
// return
//}
fmt.Println(*password)
//var names []string
//namespace := "root"
//err = client.Call("Query.GetAllNames", &namespace, &names)
var success bool
err = client.Call("Query.Delete", &np, &success)
//if err != nil {
// fmt.Println(err)
//}
if success == true {
fmt.Println("Deleted Test")
}
//fmt.Println(names)
var password2 *crypto.Password
err = client.Call("Query.Get", &np, &password2)
//var password *crypto.Password
//np := rpc.NamespaceService{ Namespace: "root", Service: "Test" }
//err = client.Call("Query.Generate", &np, &password)
if err != nil {
fmt.Println(err)
return
}
//if err != nil {
// fmt.Println(err)
//}
fmt.Println(*password2)
//fmt.Println(*password)
//var success bool
//err = client.Call("Query.Delete", &np, &success)
//if success == true {
// fmt.Println("Deleted Test")
//}
//var password2 *crypto.Password
//err = client.Call("Query.Get", &np, &password2)
//if err != nil {
// fmt.Println(err)
// return
//}
//fmt.Println(*password2)
// fmt.Printf(`
//Peer ID: %s