-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.js
195 lines (146 loc) · 5.53 KB
/
cli.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const Githereum = require('./githereum');
const neodoc = require('neodoc');
const TruffleContract = require("truffle-contract");
const GithereumContract = require("./build/contracts/Githereum.json");
const help =
// There is a helpful webapp to generate this spec:
// https://felixschl.github.io/neodoc
`Githereum
Usage:
githereum <contract> register <repo> [<blob storage>] [options]
githereum <contract> push <path> <repo:tag> [options]
githereum <contract> clone <repo:tag> <path> [options]
githereum <contract> pull <repo:tag> <path> [options]
githereum <contract> head <repo:tag> [options]
githereum <contract> (add|remove) owner <repo> <owner> [options]
githereum <contract> (add|remove) writer <repo> <writer> [options]
githereum <contract> (add|remove) reader <repo> <reader> [options]
githereum keygen <keydir>
Options:
-f, --from <address> Address of transaction sender
-h, --help Show this screen
-v, --version Show version
-p, --provider <url> Web3 Provider address, default http://localhost:9545
--private <keydir> Directory of private key to use for this operation
--public <keydir> Directory of public key to use for this operation
Blob storage when registering a repo:
This should be a json string containing a description of where the blobs for
this repo are stored. This is written publically to the blockchain so should
not contain secrets.
Default:
{"type":"tmpfile","path":"tmp/blobs"}
S3:
{"type":"s3","bucket":"my-s3-bucket"}
S3 credentials can be provided with environment variables AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS KEY, or implicitly with security groups within AWS.
`;
let contract, from, log, privateKeyDir, publicKeyDir;
async function register(repo, blobStorageJSON, keyPath) {
let blobStorageConfig;
if (blobStorageJSON) { blobStorageConfig = JSON.parse(blobStorageJSON); }
await Githereum.register(repo, contract, from, { keyPath, log, blobStorageConfig });
}
async function push(path, repoName, tag) {
let githereum = new Githereum(path, repoName, contract, from, { privateKeyDir, log });
await githereum.push(tag);
}
async function addOwner(repo, owner) {
await Githereum.addOwner(repo, owner, contract, from, { log, privateKeyDir, publicKeyDir });
}
async function removeOwner(repo, owner) {
await Githereum.removeOwner(repo, owner, contract, from, { log });
}
async function addWriter(repo, writer) {
await Githereum.addWriter(repo, writer, contract, from, { log, privateKeyDir, publicKeyDir });
}
async function removeWriter(repo, writer) {
await Githereum.removeWriter(repo, writer, contract, from, { log });
}
async function addReader(repo, reader) {
await Githereum.addReader(repo, reader, privateKeyDir, publicKeyDir, contract, from, { log });
}
async function removeReader(repo, reader) {
await Githereum.removeReader(repo, reader, contract, from, { log });
}
async function clone(repoName, tag, path) {
let githereum = new Githereum(path, repoName, contract, from, { privateKeyDir, log });
await githereum.clone(tag);
}
async function pull(repoName, tag, path) {
let githereum = new Githereum(path, repoName, contract, from, { log });
await githereum.pull(tag);
}
async function head(repoName, tag) {
await Githereum.head(repoName, tag, contract, { log });
}
async function keygen(keydirPath) {
await Githereum.keygen(keydirPath);
}
module.exports = async function (done) {
let argv;
if (arguments.length > 1) {
[, argv, log] = arguments;
} else {
argv = process.argv.slice(process.argv.indexOf('cli.js') + 1);
// eslint-disable-next-line no-console
log = console.log.bind(console);
}
const { version } = require('./package.json');
try {
const args = neodoc.run(help, { argv, version, smartOptions: true });
let contractAddress = args['<contract>'];
let providerUrl = args['--provider'] || "http://localhost:9545";
from = args['--from'];
privateKeyDir = args['--private'];
publicKeyDir = args['--public'];
if (contractAddress) {
let Githereum = TruffleContract(GithereumContract);
Githereum.setProvider(providerUrl);
contract = await Githereum.at(contractAddress);
}
if (args.register) {
await register(args['<repo>'], args['<blob storage>'], privateKeyDir);
}
if (args.add && args.owner) {
await addOwner(args['<repo>'], args['<owner>']);
}
if (args.remove && args.owner) {
await removeOwner(args['<repo>'], args['<owner>']);
}
if (args.add && args.writer) {
await addWriter(args['<repo>'], args['<writer>']);
}
if (args.remove && args.writer) {
await removeWriter(args['<repo>'], args['<writer>']);
}
if (args.add && args.reader) {
await addReader(args['<repo>'], args['<reader>']);
}
if (args.remove && args.reader) {
await removeReader(args['<repo>'], args['<reader>']);
}
if (args.push) {
let [repoName, tag] = args['<repo:tag>'].split(":");
await push(args['<path>'], repoName, tag);
}
if (args.clone) {
let [repoName, tag] = args['<repo:tag>'].split(":");
await clone(repoName, tag, args['<path>']);
}
if (args.pull) {
let [repoName, tag] = args['<repo:tag>'].split(":");
await pull(repoName, tag, args['<path>']);
}
if (args.head) {
let [repoName, tag] = args['<repo:tag>'].split(":");
await head(repoName, tag);
}
if (args.keygen) {
let keydirPath = args['<keydir>'];
await keygen(keydirPath);
}
} catch(e) {
done(e);
}
done();
};