cli: Rework `rad sync` command

By default, `rad sync` will both fetch and announce now.

To only fetch or only announce, the `--fetch` or `--announce` flags
can be used.
This commit is contained in:
Alexis Sellier 2023-05-15 15:34:45 +02:00
parent fc951b82c6
commit 8202495e24
No known key found for this signature in database
3 changed files with 29 additions and 11 deletions

View File

@ -56,6 +56,6 @@ f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/heads/master
``` ```
``` ```
$ rad sync $ rad sync --announce
✓ Synced with 1 node(s) ✓ Synced with 1 node(s)
``` ```

View File

@ -11,7 +11,7 @@ Now let's run `rad sync`. This will announce the issue refs to the network and
wait for nodes to announce that they have fetched those refs. wait for nodes to announce that they have fetched those refs.
``` ```
$ rad sync $ rad sync --announce
✓ Synced with 2 node(s) ✓ Synced with 2 node(s)
``` ```
@ -19,7 +19,7 @@ If we try to sync again after the nodes have synced, we will get a timeout
after one second, since the nodes will not emit any message: after one second, since the nodes will not emit any message:
``` (fail) ``` (fail)
$ rad sync --timeout 1 $ rad sync --announce --timeout 1
✗ Syncing with 2 node(s).. ✗ Syncing with 2 node(s)..
! Seed z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk timed out.. ! Seed z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk timed out..
! Seed z6Mkux1aUQD2voWWukVb5nNUR7thrHveQG4pDQua8nVhib7Z timed out.. ! Seed z6Mkux1aUQD2voWWukVb5nNUR7thrHveQG4pDQua8nVhib7Z timed out..

View File

@ -21,16 +21,24 @@ Usage
rad sync [<rid>] [<option>...] rad sync [<rid>] [<option>...]
rad sync [<rid>] [--fetch] [--seed <nid>] [<option>...] rad sync [<rid>] [--fetch] [--seed <nid>] [<option>...]
rad sync [<rid>] [--announce] [<option>...]
By default, the current repository is synced. By default, the current repository is synchronized both ways.
When `--fetch` is specified, this command will fetch from The process begins by fetching changes from connected seeds,
all connected seeds. To instead specify a seed, use the followed by announcing local refs to peers, thereby prompting
`--seed <nid>` option in combination with `--fetch`. them to fetch from us.
When `--fetch` is specified, a seed may be given with the `--seed`
option.
When either `--fetch` or `--announce` are specified, this command
will only fetch or announce.
Options Options
--fetch, -f Fetch from seeds instead of having seeds fetch from us --fetch, -f Fetch from seeds
--announce, -a Announce refs to seeds
--seed <nid> Seed to fetch from (use with `--fetch`) --seed <nid> Seed to fetch from (use with `--fetch`)
--timeout <secs> How many seconds to wait while syncing --timeout <secs> How many seconds to wait while syncing
--verbose, -v Verbose output --verbose, -v Verbose output
@ -39,11 +47,12 @@ Options
"#, "#,
}; };
#[derive(Default, Debug)] #[derive(Default, Debug, PartialEq, Eq)]
pub enum SyncMode { pub enum SyncMode {
Fetch, Fetch,
#[default]
Announce, Announce,
#[default]
Both,
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -76,9 +85,12 @@ impl Args for Options {
let val = term::args::nid(&val)?; let val = term::args::nid(&val)?;
seed = Some(val); seed = Some(val);
} }
Long("fetch") | Short('f') => { Long("fetch") | Short('f') if mode == SyncMode::Both => {
mode = SyncMode::Fetch; mode = SyncMode::Fetch;
} }
Long("announce") | Short('a') if mode == SyncMode::Both => {
mode = SyncMode::Announce;
}
Long("timeout") | Short('t') => { Long("timeout") | Short('t') => {
let value = parser.value()?; let value = parser.value()?;
let secs = term::args::parse_value("timeout", value)?; let secs = term::args::parse_value("timeout", value)?;
@ -127,6 +139,12 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
match options.mode { match options.mode {
SyncMode::Announce => announce(rid, node, options.timeout), SyncMode::Announce => announce(rid, node, options.timeout),
SyncMode::Fetch => fetch(rid, profile, &mut node, options.seed), SyncMode::Fetch => fetch(rid, profile, &mut node, options.seed),
SyncMode::Both => {
fetch(rid, profile, &mut node, options.seed)?;
announce(rid, node, options.timeout)?;
Ok(())
}
} }
} }