radicle_cli/commands/
remote.rs

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
//! Remote Command implementation
#[path = "remote/add.rs"]
pub mod add;
#[path = "remote/list.rs"]
pub mod list;
#[path = "remote/rm.rs"]
pub mod rm;

use std::ffi::OsString;

use anyhow::anyhow;

use radicle::git::RefString;
use radicle::prelude::NodeId;
use radicle::storage::ReadStorage;

use crate::terminal as term;
use crate::terminal::args;
use crate::terminal::{Args, Context, Help};

pub const HELP: Help = Help {
    name: "remote",
    description: "Manage a repository's remotes",
    version: env!("RADICLE_VERSION"),
    usage: r#"
Usage

    rad remote [<option>...]
    rad remote list [--tracked | --untracked | --all] [<option>...]
    rad remote add (<did> | <nid>) [--name <string>] [<option>...]
    rad remote rm <name> [<option>...]

List options

    --tracked     Show all remotes that are listed in the working copy
    --untracked   Show all remotes that are listed in the Radicle storage
    --all         Show all remotes in both the Radicle storage and the working copy

Add options

    --name        Override the name of the remote that by default is set to the node alias
    --[no-]fetch  Fetch the remote from local storage (default: fetch)
    --[no-]sync   Sync the remote refs from the network (default: sync)

Options

    --help        Print help
"#,
};

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OperationName {
    Add,
    Rm,
    #[default]
    List,
}

#[derive(Debug)]
pub enum Operation {
    Add {
        id: NodeId,
        name: Option<RefString>,
        fetch: bool,
        sync: bool,
    },
    Rm {
        name: RefString,
    },
    List {
        option: ListOption,
    },
}

#[derive(Debug, Default)]
pub enum ListOption {
    All,
    #[default]
    Tracked,
    Untracked,
}

#[derive(Debug)]
pub struct Options {
    pub op: Operation,
}

impl Args for Options {
    fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
        use lexopt::prelude::*;

        let mut parser = lexopt::Parser::from_args(args);
        let mut op: Option<OperationName> = None;
        let mut id: Option<NodeId> = None;
        let mut name: Option<RefString> = None;
        let mut list_op: ListOption = ListOption::default();
        let mut fetch = true;
        let mut sync = true;

        while let Some(arg) = parser.next()? {
            match arg {
                Long("help") | Short('h') => {
                    return Err(args::Error::Help.into());
                }
                Long("name") | Short('n') => {
                    let value = parser.value()?;
                    let value = args::refstring("name", value)?;

                    name = Some(value);
                }
                Value(val) if op.is_none() => match val.to_string_lossy().as_ref() {
                    "a" | "add" => op = Some(OperationName::Add),
                    "l" | "list" => op = Some(OperationName::List),
                    "r" | "rm" => op = Some(OperationName::Rm),
                    unknown => anyhow::bail!("unknown operation '{}'", unknown),
                },

                // List options
                Long("all") if op.unwrap_or_default() == OperationName::List => {
                    list_op = ListOption::All;
                }
                Long("tracked") if op.unwrap_or_default() == OperationName::List => {
                    list_op = ListOption::Tracked;
                }
                Long("untracked") if op.unwrap_or_default() == OperationName::List => {
                    list_op = ListOption::Untracked;
                }

                // Add options
                Long("sync") if op == Some(OperationName::Add) => {
                    sync = true;
                }
                Long("no-sync") if op == Some(OperationName::Add) => {
                    sync = false;
                }
                Long("fetch") if op == Some(OperationName::Add) => {
                    fetch = true;
                }
                Long("no-fetch") if op == Some(OperationName::Add) => {
                    fetch = false;
                }
                Value(val) if op == Some(OperationName::Add) && id.is_none() => {
                    let nid = args::pubkey(&val)?;
                    id = Some(nid);
                }

                // Remove options
                Value(val) if op == Some(OperationName::Rm) && name.is_none() => {
                    let val = args::string(&val);
                    let val = RefString::try_from(val)
                        .map_err(|e| anyhow!("invalid remote name specified: {e}"))?;

                    name = Some(val);
                }
                _ => return Err(anyhow::anyhow!(arg.unexpected())),
            }
        }

        let op = match op.unwrap_or_default() {
            OperationName::Add => Operation::Add {
                id: id.ok_or(anyhow!(
                    "`DID` required, try running `rad remote add <did>`"
                ))?,
                name,
                fetch,
                sync,
            },
            OperationName::List => Operation::List { option: list_op },
            OperationName::Rm => Operation::Rm {
                name: name.ok_or(anyhow!("name required, see `rad remote`"))?,
            },
        };

        Ok((Options { op }, vec![]))
    }
}

pub fn run(options: Options, ctx: impl Context) -> anyhow::Result<()> {
    let (working, rid) = radicle::rad::cwd()
        .map_err(|_| anyhow!("this command must be run in the context of a repository"))?;
    let profile = ctx.profile()?;

    match options.op {
        Operation::Add {
            ref id,
            name,
            fetch,
            sync,
        } => {
            let proj = profile.storage.repository(rid)?.project()?;
            let branch = proj.default_branch();

            self::add::run(
                rid,
                id,
                name,
                Some(branch.clone()),
                &profile,
                &working,
                fetch,
                sync,
            )?
        }
        Operation::Rm { ref name } => self::rm::run(name, &working)?,
        Operation::List { option } => match option {
            ListOption::All => {
                let tracked = list::tracked(&working)?;
                let untracked = list::untracked(rid, &profile, tracked.iter())?;
                // Only include a blank line if we're printing both tracked and untracked
                let include_blank_line = !tracked.is_empty() && !untracked.is_empty();

                list::print_tracked(tracked.iter());
                if include_blank_line {
                    term::blank();
                }
                list::print_untracked(untracked.iter());
            }
            ListOption::Tracked => {
                let tracked = list::tracked(&working)?;
                list::print_tracked(tracked.iter());
            }
            ListOption::Untracked => {
                let tracked = list::tracked(&working)?;
                let untracked = list::untracked(rid, &profile, tracked.iter())?;
                list::print_untracked(untracked.iter());
            }
        },
    };
    Ok(())
}