diff --git a/cmd/ppass/ppass.go b/cmd/ppass/ppass.go index 3fbe499..cd9e434 100644 --- a/cmd/ppass/ppass.go +++ b/cmd/ppass/ppass.go @@ -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 diff --git a/go.mod b/go.mod index f667908..79b1768 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.22.5 require ( filippo.io/age v1.2.0 + github.com/google/uuid v1.6.0 github.com/hsanjuan/ipfs-lite v1.8.2 github.com/ipfs/go-datastore v0.6.0 github.com/ipfs/go-ds-badger2 v0.1.3 @@ -13,6 +14,7 @@ require ( github.com/libp2p/go-libp2p-kad-dht v0.25.2 github.com/libp2p/go-libp2p-pubsub v0.11.0 github.com/multiformats/go-multiaddr v0.13.0 + github.com/spf13/cobra v0.0.5 ) require ( @@ -48,13 +50,13 @@ require ( github.com/golang/snappy v0.0.1 // indirect github.com/google/gopacket v1.1.19 // indirect github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/golang-lru v1.0.2 // indirect github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/huin/goupnp v1.3.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/ipfs/bbloom v0.0.4 // indirect github.com/ipfs/boxo v0.21.0 // indirect github.com/ipfs/go-bitfield v1.1.0 // indirect @@ -138,6 +140,7 @@ require ( github.com/quic-go/webtransport-go v0.8.0 // indirect github.com/raulk/go-watchdog v1.3.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect + github.com/spf13/pflag v1.0.3 // indirect github.com/stretchr/testify v1.9.0 // indirect github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect diff --git a/go.sum b/go.sum index 0372eb3..8230083 100644 --- a/go.sum +++ b/go.sum @@ -188,6 +188,7 @@ github.com/hsanjuan/ipfs-lite v1.8.2 h1:PwYpfvh4HpActJyP03O4k6QznBR3xd6NmRWeOSJu github.com/hsanjuan/ipfs-lite v1.8.2/go.mod h1:PfY4I2whwnZBHviwoajNzwjSyR9IQIJHxkpPAc5SLxw= github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc= github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs= github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0= @@ -487,8 +488,10 @@ github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0b github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/storage/storage.go b/storage/storage.go index 28dca83..4d8fa8a 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -38,7 +38,7 @@ import ( ) var ( - topicNameFlag = "afbjlask-23klaj2idalj2-ajl2kjd3i-2ldakjd2" + topicNameFlag = "afbjlask-23klaj2idalj2-ajl2kjd3i-2ldakjd4" logger = logging.Logger("globaldb") Listen = libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0") ) @@ -432,8 +432,7 @@ func CreateNamespace(ID string, storageHandler StorageHandler) (*Namespace, erro opts.Logger = logger opts.RebroadcastInterval = 5 * time.Second opts.PutHook = func(k ds.Key, v []byte) { - fmt.Printf("Added: [%s] -> %s\n", k, string(v)) - + fmt.Printf("Added: [%s]\n", k) } opts.DeleteHook = func(k ds.Key) { fmt.Printf("Removed: [%s]\n", k)