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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! Declarative definition for the end-user-facing CLI

use noosphere_core::data::Did;

use std::net::IpAddr;

use clap::Parser;
use clap::Subcommand;
use url::Url;

#[allow(missing_docs)]
#[derive(Debug, Parser)]
#[clap(name = "orb")]
#[clap(about = "A CLI tool for saving, syncing and sharing content to the Noosphere", long_about = Some(
r#"The orb CLI tool is a utility for saving, syncing and sharing content to the
Noosphere. In practical terms, this means it helps you with tasks such as key
management, creating and updating spheres, managing acccess to said spheres and
publishing the contents of those spheres to public networks."#))]
pub struct Cli {
    #[clap(subcommand)]
    pub command: OrbCommand,
}

#[allow(missing_docs)]
#[derive(Debug, Subcommand)]
pub enum OrbCommand {
    Key {
        #[clap(subcommand)]
        command: KeyCommand,
    },

    Sphere {
        #[clap(subcommand)]
        command: SphereCommand,
    },

    /// Summon a gateway geist to manage the local sphere; it will accept
    /// push, fetch and other REST actions from any clients that are authorized
    /// to operate on its counterpart sphere. When it receives changes to its
    /// counterpart sphere, it will perform various actions such as publishing
    /// and/or querying the Noosphere Name System, generating static HTML and/or
    /// updating its own sphere with various related information of interest to
    /// the counterpart sphere
    Serve {
        /// Optional origin to allow CORS for
        #[clap(short, long)]
        cors_origin: Option<Url>,

        /// URL of a Kubo Gateway RPC API
        #[clap(short = 'I', long, default_value = "http://127.0.0.1:5001")]
        ipfs_api: Url,

        /// URL for a Noosphere name system RPC API
        #[clap(short = 'N', long, default_value = "http://127.0.0.1:6667")]
        name_resolver_api: Url,

        /// The IP address of the interface that the gateway should bind to
        #[clap(short, long, default_value = "127.0.0.1")]
        interface: IpAddr,

        /// The port that the gateway should listen on
        #[clap(short, long, default_value = "4433")]
        port: u16,

        /// If set, the amount of memory that the storage provider may use
        /// for caching in bytes.
        #[clap(long)]
        storage_memory_cache_limit: Option<usize>,
    },
}

/// Create and securely manage personal keys
#[derive(Debug, Subcommand)]
pub enum KeyCommand {
    /// Generate and securely store a new named key; this key is the analog of
    /// a user account in the Noosphere.
    Create {
        /// The pet name for the newly created key; you will refer to it by this
        /// name when using it in other commands
        name: String,
    },

    /// Print the pet name and DID for all available keys
    #[clap(alias = "ls")]
    List {
        /// Output the list of available keys as formatted JSON
        #[clap(short = 'j', long)]
        as_json: bool,
    },
}

/// Create a new sphere or connect another device to an existing one
#[derive(Debug, Subcommand)]
pub enum SphereCommand {
    /// Initialize a new sphere and assign a key as its owner
    Create {
        /// The pet name of a key to assign as the owner of the sphere
        #[clap(short = 'k', long)]
        owner_key: String,
    },

    /// Join an existing sphere by its ID and set up a local working copy
    Join {
        /// The pet name of a key to use when requesting access to the sphere
        #[clap(short = 'k', long)]
        local_key: String,

        /// The URL for a gateway API host that the owner key of this sphere is authorized to use
        #[clap(short = 'g', long)]
        gateway_url: Url,

        /// The identity of the authorization that allows the specified key
        /// to join the sphere (if already known)
        #[clap(short = 'a', long)]
        authorization: Option<String>,

        /// The identity of an existing sphere to join
        id: Did,

        /// The maximum depth to traverse through followed spheres when
        /// rendering updates
        #[clap(short = 'd', long)]
        render_depth: Option<u32>,
    },

    /// Show details about files in the sphere directory that have changed since
    /// the last time the sphere was saved
    Status {
        /// Only output the orb id
        #[clap(long)]
        id: bool,
    },

    /// Saves changed files to a sphere, creating and signing a new revision in
    /// the process; does nothing if there have been no changes to the files
    /// since the last revision
    Save {
        /// The maximum depth to traverse through followed spheres when
        /// rendering updates
        #[clap(short = 'd', long)]
        render_depth: Option<u32>,
    },

    /// Synchronizes the local sphere with the copy in a configured gateway;
    /// note that this is a "conflict-free" sync that may cause local changes
    /// to be overwritten in cases where two or more clients have made changes
    /// to the same files
    Sync {
        /// Automatically retry the attempt to sync this number of times
        #[clap(short = 'r', long, default_value = "0")]
        auto_retry: u32,

        /// The maximum depth to traverse through followed spheres when
        /// rendering updates
        #[clap(short = 'd', long)]
        render_depth: Option<u32>,
    },

    /// Force a render of local sphere content as well as the peer graph; note
    /// that this will overwrite any unsaved changes to local sphere content
    Render {
        /// The maximum depth to traverse through followed spheres when
        /// rendering updates
        #[clap(short = 'd', long)]
        render_depth: Option<u32>,
    },

    /// Print a changelog of sphere in a human readable format
    History,

    #[allow(missing_docs)]
    Follow {
        #[clap(subcommand)]
        command: FollowCommand,
    },

    #[allow(missing_docs)]
    Config {
        #[clap(subcommand)]
        command: ConfigCommand,
    },

    #[allow(missing_docs)]
    Auth {
        #[clap(subcommand)]
        command: AuthCommand,
    },
}

/// Read and manage configuration values for a local sphere
#[derive(Debug, Subcommand)]
pub enum ConfigCommand {
    /// Set a configuration value for the local sphere
    Set {
        #[allow(missing_docs)]
        #[clap(subcommand)]
        command: ConfigSetCommand,
    },
    /// Retrieve a configuration value if one is set
    Get {
        #[allow(missing_docs)]
        #[clap(subcommand)]
        command: ConfigGetCommand,
    },
}

/// Write local-only metadata configuration related to this sphere
#[derive(Debug, Subcommand)]
pub enum ConfigSetCommand {
    /// Configure the URL of the gateway to use for publishing and sync
    GatewayUrl {
        /// The URL for a gateway API host that the owner key of this sphere is authorized to use
        url: Url,
    },

    /// If you are configuring your local sphere, the "counterpart" is the
    /// gateway's sphere DID. If you are configuring a gateway's sphere, the
    /// "counterpart" is the DID of your local sphere.
    Counterpart {
        /// The sphere identity (as a DID) of the counterpart
        did: String,
    },
}

/// Read local-only metadata configuration related to this sphere
#[derive(Debug, Subcommand)]
pub enum ConfigGetCommand {
    /// Read the configured gateway URL
    GatewayUrl,

    /// Read the configured counterpart DID
    Counterpart,
}

/// Manage the devices/keys that are allowed to access this sphere
#[derive(Debug, Subcommand)]
pub enum AuthCommand {
    /// Authorize a key to work on the sphere in the current directory
    Add {
        /// The DID of the key to authorize
        did: Did,

        /// An optional name to give the key; if one is not specified, a random
        /// one will be assigned
        #[clap(short = 'n', long)]
        name: Option<String>,
    },

    /// Print the name and DID for all keys that the owner has authorized
    /// to work on this sphere
    List {
        /// Output the list of authorized keys as formatted JSON
        #[clap(short = 'j', long)]
        as_json: bool,

        /// Format the authorizations as a tree based on ancestry
        #[clap(short = 't', long)]
        tree: bool,
    },

    /// Revoke authorization to work on the sphere from a specified key
    Revoke {
        /// The name of a key to revoke authorization for
        name: String,
    },

    /// Rotate key authority from one key to another
    Rotate {},
}

/// Follow and/or unfollow other spheres
#[derive(Debug, Subcommand)]
pub enum FollowCommand {
    /// Follow a sphere, assigning it a personalized nickname
    Add {
        /// A personalized nickname for the sphere you are following
        name: Option<String>,

        /// The sphere ID that you wish to follow
        #[clap(short = 'i', long)]
        sphere_id: Option<Did>,
    },

    /// Unfollow a sphere, either by nickname or by sphere ID
    Remove {
        /// A short name of a sphere that you wish to unfollow. If you follow
        /// the same sphere by multiple names, this will only remove the name
        /// you specify
        #[clap(short = 'n', long)]
        by_name: Option<String>,

        /// The sphere ID that you wish to unfollow. If you follow this sphere
        /// by multiple names, all of them will be removed at once.
        #[clap(short = 'i', long)]
        by_sphere_id: Option<Did>,
    },

    /// Rename a sphere that you currently follow to something new
    Rename {
        /// The current nickname of a sphere that you follow
        from: String,

        /// The preferred nickname to rename the sphere to
        #[clap(short = 't', long)]
        to: Option<String>,
    },

    /// Show a list of all the spheres that you follow
    List {
        /// Output the list of peers as formatted JSON
        #[clap(short = 'j', long)]
        as_json: bool,
    },
}