-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseFlags.go
162 lines (146 loc) · 4.54 KB
/
parseFlags.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/AaronGoldman/ccfs/objects"
"github.com/AaronGoldman/ccfs/services"
)
func parseFlags() (
Flags struct {
mount *bool
serve *bool
dht *bool
drive *bool
apps *bool
direct *bool
lan *bool
},
Command struct {
createDomain *bool
createRepository *bool
insertDomain *bool
insertRepository *bool
path *string
hkid *string
},
) {
Flags.mount = flag.Bool("mount", false, "Mount the fuse file system")
Flags.serve = flag.Bool("serve", true, "Start content object server")
Flags.dht = flag.Bool("dht", false, "Starts Kademliadht service")
Flags.drive = flag.Bool("drive", false, "Starts Googledrive service")
Flags.apps = flag.Bool("apps", false, "Starts Appsscript service")
Flags.direct = flag.Bool("direct", false, "Starts direct http service")
Flags.lan = flag.Bool("lan", false, "Starts the multicast service")
Command.createDomain = flag.Bool("createDomain", false, "Creates a new domain at path argument")
Command.createRepository = flag.Bool("createRepository", false, "Creates a new repository at path argument")
Command.insertDomain = flag.Bool("insertDomain", false, "Inserts the domain HKID argument at path argument")
Command.insertRepository = flag.Bool("insertRepository", false, "Inserts the repository HKID argument at path argument")
Command.path = flag.String("path", "", "The path to inserted collection")
Command.hkid = flag.String("hkid", "", "HKID of collection to insert")
flag.Parse()
return Flags, Command
}
func addCurators(inCommand struct {
createDomain *bool
createRepository *bool
insertDomain *bool
insertRepository *bool
path *string
hkid *string
}) {
if *inCommand.path != "" {
//log.Printf("HKID: %s", *hkid)
in := bufio.NewReader(os.Stdin)
var err error
h, collectionPath := fileSystemPath2CollectionPath(inCommand.path)
//log.Printf("systemPath %s", *path)
//log.Printf("collectionPath %s", collectionPath)
FileInfos, err := ioutil.ReadDir(*inCommand.path)
if err != nil {
log.Printf("Error reading directory %s", err)
os.Exit(2)
return
}
if len(FileInfos) != 0 {
fmt.Printf("The folder is not empty")
os.Exit(2)
return // Ends function
}
collectionName := filepath.Base(*inCommand.path)
//fmt.Printf("Name of Collection: %s\n", collectionName)
switch {
case *inCommand.createDomain:
//err = InitDomain(h, fmt.Sprintf("%s/%s", collectionPath, collectionName))
err = services.InitDomain(h, collectionPath)
if err != nil {
log.Println(err)
return
}
case *inCommand.createRepository:
//err = InitRepo(h, fmt.Sprintf("%s/%s", collectionPath, collectionName))
err = services.InitRepo(h, collectionPath)
if err != nil {
log.Println(err)
return
}
case *inCommand.insertDomain:
fmt.Println("Insert HKID as a hexadecimal number:")
hex := *inCommand.hkid
if *inCommand.hkid == "" {
hex, _ = in.ReadString('\n')
hex = strings.Trim(hex, "\n")
}
log.Print(len(hex))
foreignHkid, err := objects.HkidFromHex(hex)
if err != nil {
log.Printf("Somethng went wrong in insertDomain %s", err)
os.Exit(2)
}
log.Printf("hkid: %s\n", h)
err = services.InsertDomain(h, fmt.Sprintf("%s/%s", collectionPath, collectionName), foreignHkid)
if err != nil {
log.Println(err)
return
}
case *inCommand.insertRepository:
fmt.Println("Insert HKID as a hexadecimal number:")
hex := *inCommand.hkid
if *inCommand.hkid == "" {
hex, _ = in.ReadString('\n')
hex = strings.Trim(hex, "\n")
}
fmt.Printf("%s", hex)
foreignHkid, err := objects.HkidFromHex(hex)
if err != nil {
log.Printf("Somethng went wrong in insertRepo %s", err)
os.Exit(2)
}
fmt.Printf("hkid: %s", h)
err = services.InsertRepo(h, fmt.Sprintf("%s/%s", collectionPath, collectionName), foreignHkid)
if err != nil {
log.Println(err)
return
}
}
}
}
func fileSystemPath2CollectionPath(fileSystemPath *string) (objects.HKID, string) {
h, err := objects.HkidFromHex("c09b2765c6fd4b999d47c82f9cdf7f4b659bf7c29487cc0b357b8fc92ac8ad02")
wd, err := os.Getwd()
path := filepath.Join(wd, "../mountpoint")
//log.Printf("%s", *fileSystemPath)
//LEFT OFF HERE- so fileSystemPath isnt what I want it to be
*fileSystemPath = strings.Trim(*fileSystemPath, "\"")
collectionPath, err := filepath.Rel(string(path), *fileSystemPath)
if err != nil {
log.Printf("OH NO! An Error %s", err)
os.Exit(2)
}
return h, collectionPath
}