-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbit.go
80 lines (61 loc) · 1.63 KB
/
orbit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
shell "github.com/ipfs/go-ipfs-api"
)
var sh *shell.Shell
type Entry struct {
GroupId string
Port string
}
func writeToOrbitDB(groupId, port string) {
entry := Entry{GroupId: groupId, Port: port}
data := fmt.Sprintf("GroupId: %s, Port: %s", entry.GroupId, entry.Port)
cid, err := sh.Add(strings.NewReader(data))
if err != nil {
log.Fatalf("Failed to write data to IPFS: %v", err)
}
fmt.Printf("Data written to IPFS with CID: %s\n", cid)
}
func readFromOrbitDB(cid string) {
readData, err := sh.Cat(cid)
if err != nil {
log.Fatalf("Failed to read data from IPFS: %v", err)
}
fmt.Println("Data from IPFS:", readData)
}
func smain() {
sh = shell.NewShell("localhost:5001")
readCmd := flag.NewFlagSet("read", flag.ExitOnError)
writeCmd := flag.NewFlagSet("write", flag.ExitOnError)
writeGroupId := writeCmd.String("groupId", "", "Group ID to write to OrbitDB")
writePort := writeCmd.String("port", "", "Port to associate with the Group ID")
readCID := readCmd.String("cid", "", "CID to read data from OrbitDB")
if len(os.Args) < 2 {
fmt.Println("Expected 'read' or 'write' subcommands.")
os.Exit(1)
}
switch os.Args[1] {
case "write":
writeCmd.Parse(os.Args[2:])
if *writeGroupId == "" || *writePort == "" {
fmt.Println("Please provide both GroupId and Port.")
os.Exit(1)
}
writeToOrbitDB(*writeGroupId, *writePort)
case "read":
readCmd.Parse(os.Args[2:])
if *readCID == "" {
fmt.Println("Please provide a CID to read data.")
os.Exit(1)
}
readFromOrbitDB(*readCID)
default:
fmt.Println("Unknown command.")
os.Exit(1)
}
}