359 lines
6.9 KiB
Go
359 lines
6.9 KiB
Go
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"
|
|
)
|
|
|
|
var (
|
|
dbPath = flag.String("db", "./db", "db file path")
|
|
)
|
|
|
|
// 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 namespaceCmd = &cobra.Command{
|
|
Use: "namespace",
|
|
Short: "Add, delete or list namespaces",
|
|
}
|
|
|
|
var addNamespaceCmd = &cobra.Command{
|
|
Use: "add",
|
|
Short: "add a namespace",
|
|
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
|
|
}
|
|
|
|
namespace := args[0]
|
|
|
|
var placeholder int
|
|
err = client.Call("Query.AddNamespace", &namespace, &placeholder)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Namespace %s was added\n", namespace)
|
|
},
|
|
}
|
|
|
|
var deleteNamespaceCmd = &cobra.Command{
|
|
Use: "delete",
|
|
Short: "delete a namespace",
|
|
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
|
|
}
|
|
|
|
namespace := args[0]
|
|
|
|
var placeholder int
|
|
err = client.Call("Query.DeleteNamespace", &namespace, &placeholder)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Namespace %s was deleted\n", namespace)
|
|
},
|
|
}
|
|
|
|
var listNamespacesCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "list all namespaces",
|
|
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
|
|
}
|
|
|
|
var reply []string
|
|
err = client.Call("Query.ListNamespaces", 0, &reply)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Namespaces:\n")
|
|
for _, ns := range reply {
|
|
fmt.Println(ns)
|
|
}
|
|
},
|
|
}
|
|
|
|
var peerCmd = &cobra.Command{
|
|
Use: "peer",
|
|
Short: "Add or remove peers. Get your own peering information",
|
|
}
|
|
|
|
|
|
var infoCmd = &cobra.Command{
|
|
Use: "info",
|
|
Short: "print your own peerstring",
|
|
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
|
|
}
|
|
|
|
var result *string
|
|
err = client.Call("Query.GetPeerString", 0, &result)
|
|
fmt.Println(*result)
|
|
},
|
|
}
|
|
|
|
|
|
var addPeerCmd = &cobra.Command{
|
|
Use: "add",
|
|
Short: "add a peer",
|
|
Args: cobra.RangeArgs(1, 2),
|
|
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
|
|
}
|
|
|
|
var namespace string
|
|
var peerString string
|
|
|
|
if len(args) == 1 {
|
|
namespace = "root"
|
|
peerString = args[0]
|
|
} else {
|
|
namespace = args[0]
|
|
peerString = args[1]
|
|
}
|
|
|
|
var success *bool
|
|
np := rpc.NamespacePeer{ Namespace: namespace, Peer: peerString }
|
|
err = client.Call("Query.AddPeer", &np, &success)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(*success)
|
|
},
|
|
}
|
|
|
|
var removePeerCmd = &cobra.Command{
|
|
Use: "remove",
|
|
Short: "remove a peer",
|
|
Args: cobra.RangeArgs(1, 2),
|
|
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
|
|
}
|
|
|
|
var namespace string
|
|
var peerString string
|
|
|
|
if len(args) == 1 {
|
|
namespace = "root"
|
|
peerString = args[0]
|
|
} else {
|
|
namespace = args[0]
|
|
peerString = args[1]
|
|
}
|
|
|
|
var success *bool
|
|
np := rpc.NamespacePeer{ Namespace: namespace, Peer: peerString }
|
|
err = client.Call("Query.DeletePeer", &np, &success)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(*success)
|
|
},
|
|
}
|
|
|
|
var deleteCmd = &cobra.Command{
|
|
Use: "delete",
|
|
Short: "delete a Password",
|
|
Args: cobra.RangeArgs(1, 2),
|
|
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
|
|
}
|
|
|
|
var namespace string
|
|
var serviceName string
|
|
|
|
if len(args) == 1 {
|
|
namespace = "root"
|
|
serviceName = args[0]
|
|
} else {
|
|
namespace = args[0]
|
|
serviceName = args[1]
|
|
}
|
|
|
|
var success *bool
|
|
np := rpc.NamespaceService{ Namespace: namespace, Service: serviceName }
|
|
err = client.Call("Query.Delete", &np, &success)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
fmt.Println(*success)
|
|
},
|
|
}
|
|
|
|
|
|
func init() {
|
|
//listCmd.Flags().BoolP("all", "a", false, "List all items")
|
|
rootCmd.PersistentFlags().String("db", "", "db path")
|
|
rootCmd.MarkPersistentFlagRequired("db")
|
|
|
|
peerCmd.AddCommand(addPeerCmd)
|
|
peerCmd.AddCommand(removePeerCmd)
|
|
peerCmd.AddCommand(infoCmd)
|
|
|
|
namespaceCmd.AddCommand(addNamespaceCmd)
|
|
namespaceCmd.AddCommand(deleteNamespaceCmd)
|
|
namespaceCmd.AddCommand(listNamespacesCmd)
|
|
|
|
rootCmd.AddCommand(listCmd)
|
|
rootCmd.AddCommand(generateCmd)
|
|
rootCmd.AddCommand(showCmd)
|
|
rootCmd.AddCommand(deleteCmd)
|
|
rootCmd.AddCommand(peerCmd)
|
|
rootCmd.AddCommand(namespaceCmd)
|
|
}
|
|
|
|
func main() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|