split files
This commit is contained in:
246
cmd/ppass/ppass.go
Normal file
246
cmd/ppass/ppass.go
Normal file
@@ -0,0 +1,246 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
ipfslite "github.com/hsanjuan/ipfs-lite"
|
||||
badger "github.com/ipfs/go-ds-badger2"
|
||||
logging "github.com/ipfs/go-log/v2"
|
||||
|
||||
"github.com/k4lipso/pentapass/storage"
|
||||
)
|
||||
|
||||
var (
|
||||
topicNameFlag = flag.String("topicName", "akdjlask-23klaj2idalj2-ajl2kjd3i-2ldakjd2", "name of topic to join")
|
||||
dbPath = flag.String("db", "./db", "db file path")
|
||||
nameSpace = flag.String("namespace", "crdt", "namespace")
|
||||
logger = logging.Logger("globaldb")
|
||||
// topicName = "globaldb-example"
|
||||
// netTopic = "globaldb-example-net"
|
||||
// config = "globaldb-example"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
ctx := context.Background()
|
||||
data := *dbPath
|
||||
|
||||
h, dht, err := storage.SetupLibp2pHost(ctx, *dbPath)
|
||||
|
||||
pid := h.ID().String()
|
||||
fmt.Println(h.ID().String())
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ps, err := pubsub.NewGossipSub(ctx, h)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//topic, err := ps.Join(*topicNameFlag)
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
|
||||
go storage.DiscoverPeers(ctx, h, dht)
|
||||
|
||||
store, err := badger.NewDatastore(data, &badger.DefaultOptions)
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
ipfs, err := ipfslite.New(ctx, store, nil, h, dht, nil)
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
storageHandler := storage.StorageHandler{
|
||||
Ctx: ctx ,
|
||||
Store: store,
|
||||
Host: h,
|
||||
Ipfs: ipfs,
|
||||
PubSub: ps,
|
||||
}
|
||||
|
||||
Cfg := storage.NewConfig()
|
||||
|
||||
Namespaces := make(map[string]*storage.Namespace)
|
||||
for _, nsCfg := range Cfg {
|
||||
ns1, err := storage.CreateNamespace(nsCfg.Id, storageHandler)
|
||||
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
Namespaces[nsCfg.Name] = ns1
|
||||
defer ns1.Close()
|
||||
}
|
||||
|
||||
fmt.Printf(`
|
||||
Peer ID: %s
|
||||
Listen address: %s
|
||||
Topic: %s
|
||||
Data Folder: %s
|
||||
|
||||
Ready!
|
||||
|
||||
Commands:
|
||||
|
||||
> list -> list items in the store
|
||||
> get <key> -> get value for a key
|
||||
> put <key> <value> -> store value on a key
|
||||
> exit -> quit
|
||||
|
||||
|
||||
`,
|
||||
pid, storage.Listen, *topicNameFlag, data,
|
||||
)
|
||||
|
||||
if len(os.Args) > 1 && os.Args[1] == "daemon" {
|
||||
fmt.Println("Running in daemon mode")
|
||||
go func() {
|
||||
for {
|
||||
fmt.Printf("%s - %d connected peers\n", time.Now().Format(time.Stamp), len(storage.ConnectedPeers(h)))
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}()
|
||||
signalChan := make(chan os.Signal, 20)
|
||||
signal.Notify(
|
||||
signalChan,
|
||||
syscall.SIGINT,
|
||||
syscall.SIGTERM,
|
||||
syscall.SIGHUP,
|
||||
)
|
||||
<-signalChan
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("> ")
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
fields := strings.Fields(text)
|
||||
if len(fields) == 0 {
|
||||
fmt.Printf("> ")
|
||||
continue
|
||||
}
|
||||
|
||||
cmd := fields[0]
|
||||
|
||||
switch cmd {
|
||||
case "exit", "quit":
|
||||
return
|
||||
case "debug":
|
||||
if len(fields) < 2 {
|
||||
fmt.Println("debug <on/off/peers>")
|
||||
}
|
||||
st := fields[1]
|
||||
switch st {
|
||||
case "on":
|
||||
logging.SetLogLevel("globaldb", "debug")
|
||||
case "off":
|
||||
logging.SetLogLevel("globaldb", "error")
|
||||
case "peers":
|
||||
for _, p := range storage.ConnectedPeers(h) {
|
||||
addrs, err := peer.AddrInfoToP2pAddrs(p)
|
||||
if err != nil {
|
||||
logger.Warn(err)
|
||||
continue
|
||||
}
|
||||
for _, a := range addrs {
|
||||
fmt.Println(a)
|
||||
}
|
||||
}
|
||||
}
|
||||
case "list":
|
||||
if len(fields) < 2 {
|
||||
fmt.Printf("Available Namespaces:\n")
|
||||
for k := range Namespaces {
|
||||
fmt.Printf("%s\n", k)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
namespace := fields[1]
|
||||
|
||||
fmt.Printf("Listing content of %s", namespace)
|
||||
|
||||
val, ok := Namespaces[namespace]
|
||||
|
||||
if !ok {
|
||||
fmt.Println("Namespace does not exist")
|
||||
continue
|
||||
}
|
||||
|
||||
val.List()
|
||||
case "get":
|
||||
if len(fields) < 3 {
|
||||
fmt.Println("get <namespace> <key>")
|
||||
fmt.Println("> ")
|
||||
continue
|
||||
}
|
||||
|
||||
namespace := fields[1]
|
||||
|
||||
val, ok := Namespaces[namespace]
|
||||
|
||||
if !ok {
|
||||
fmt.Println("Namespace does not exist")
|
||||
continue
|
||||
}
|
||||
|
||||
k := fields[2]
|
||||
v, err := val.Get(k)
|
||||
if err != nil {
|
||||
printErr(err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("[%s] -> %s\n", k, string(v))
|
||||
case "put":
|
||||
if len(fields) < 4 {
|
||||
fmt.Println("put <namespace> <key> <value>")
|
||||
fmt.Println("> ")
|
||||
continue
|
||||
}
|
||||
|
||||
namespace := fields[1]
|
||||
|
||||
val, ok := Namespaces[namespace]
|
||||
|
||||
if !ok {
|
||||
fmt.Println("Namespace does not exist")
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
k := fields[2]
|
||||
v := strings.Join(fields[3:], " ")
|
||||
err := val.Put(k, v)
|
||||
if err != nil {
|
||||
printErr(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
fmt.Printf("> ")
|
||||
}
|
||||
}
|
||||
|
||||
func printErr(err error) {
|
||||
fmt.Println("error:", err)
|
||||
fmt.Println("> ")
|
||||
}
|
||||
Reference in New Issue
Block a user