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
315
316
317
318
319
320
321
322
323
324
325
326
//! High level user invoked workflows for nomad.

use std::{collections::HashSet, hash::Hash, io::Write};

use anyhow::{Context, Result};

use crate::{
    git_binary::GitBinary,
    git_ref::GitRef,
    renderer::{add_newline_if_spinners_are_visible, Renderer},
    types::{Branch, Host, NomadRef, Remote, User},
};

/// A boundary type that separates the CLI interface from high level nomad workflows.
#[derive(Debug, PartialEq, Eq)]
pub enum Workflow<'a> {
    Sync {
        user: User<'a>,
        host: Host<'a>,
        remote: Remote<'a>,
    },
    Ls {
        printer: LsPrinter,
        user: User<'a>,
        fetch_remote: Option<Remote<'a>>,
        host_filter: Filter<Host<'a>>,
        branch_filter: Filter<Branch<'a>>,
    },
    Purge {
        user: User<'a>,
        remote: Remote<'a>,
        host_filter: Filter<Host<'a>>,
    },
    Completions(clap_complete::Shell),
}

impl Workflow<'_> {
    /// Imperatively execute the workflow.
    pub fn execute(self, renderer: &mut impl Renderer, git: &GitBinary) -> Result<()> {
        match self {
            Self::Sync { user, host, remote } => sync(renderer, git, &user, &host, &remote),
            Self::Ls {
                printer,
                user,
                fetch_remote,
                host_filter,
                branch_filter,
            } => ls(
                renderer,
                git,
                printer,
                &user,
                fetch_remote,
                host_filter,
                branch_filter,
            ),
            Self::Purge {
                user,
                remote,
                host_filter,
            } => purge(renderer, git, &user, &remote, host_filter),
            Self::Completions(shell) => print_completions(renderer, shell),
        }
    }
}

/// Declarative representation of a limited filter function.
#[derive(Debug, PartialEq, Eq)]
pub enum Filter<T: PartialEq + Eq + Hash> {
    /// Everything.
    All,
    /// Only the specified values.
    Allow(HashSet<T>),
    /// Everything except the specified values.
    Deny(HashSet<T>),
}

impl<T: PartialEq + Eq + Hash> Filter<T> {
    pub fn contains(&self, t: &T) -> bool {
        match self {
            Self::All => true,
            Self::Allow(hash_set) => hash_set.contains(t),
            Self::Deny(hash_set) => !hash_set.contains(t),
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LsPrinter {
    Grouped,
    Ref,
    Commit,
}

impl LsPrinter {
    pub fn print_host(self, output: &mut dyn Write, host: &Host) -> Result<()> {
        match self {
            Self::Grouped => writeln!(output, "{}", host.0).context("printing grouped host"),
            Self::Ref | Self::Commit => Ok(()),
        }
    }

    pub fn print_ref(self, output: &mut dyn Write, ref_: &GitRef) -> Result<()> {
        match self {
            Self::Grouped => writeln!(output, "  {} -> {}", ref_.name, ref_.commit_id)
                .context("printing ref and commit"),
            Self::Ref => writeln!(output, "{}", ref_.name).context("printing ref"),
            Self::Commit => writeln!(output, "{}", ref_.commit_id).context("printing commit"),
        }
    }
}

/// Synchronize current local branches with nomad managed refs in the given remote.
fn sync(
    renderer: &mut impl Renderer,
    git: &GitBinary,
    user: &User,
    host: &Host,
    remote: &Remote,
) -> Result<()> {
    git.push_nomad_refs(renderer, user, host, remote)?;
    git.fetch_nomad_refs(renderer, user, remote)?;
    let remote_nomad_refs = git.list_nomad_refs(renderer, user, remote)?.collect();
    let snapshot = git.snapshot(renderer, user)?;
    git.prune_nomad_refs(
        renderer,
        remote,
        snapshot
            .prune_deleted_branches(host, &remote_nomad_refs)
            .into_iter(),
    )?;

    if git.is_output_allowed() {
        add_newline_if_spinners_are_visible(renderer)?;

        ls(
            renderer,
            git,
            LsPrinter::Grouped,
            user,
            None,
            Filter::All,
            Filter::All,
        )?
    }

    Ok(())
}

/// List all nomad managed refs organized by host.
///
/// Does not respect [`GitBinary::is_output_allowed`] because output is the whole point of this
/// command.
fn ls(
    renderer: &mut impl Renderer,
    git: &GitBinary,
    printer: LsPrinter,
    user: &User,
    fetch_remote: Option<Remote>,
    host_filter: Filter<Host>,
    branch_filter: Filter<Branch>,
) -> Result<()> {
    if let Some(remote) = fetch_remote {
        git.fetch_nomad_refs(renderer, user, &remote)?;
    }

    let snapshot = git.snapshot(renderer, user)?;

    for (host, branches) in snapshot.sorted_hosts_and_branches() {
        if !host_filter.contains(&host) {
            continue;
        }

        renderer.writer(|w| {
            printer.print_host(w, &host)?;

            for NomadRef { ref_, branch, .. } in branches {
                if branch_filter.contains(&branch) {
                    printer.print_ref(w, &ref_)?;
                }
            }

            Ok(())
        })?;
    }

    Ok(())
}

/// Delete nomad managed refs returned by `to_prune`.
fn purge(
    renderer: &mut impl Renderer,
    git: &GitBinary,
    user: &User,
    remote: &Remote,
    host_filter: Filter<Host>,
) -> Result<()> {
    git.fetch_nomad_refs(renderer, user, remote)?;
    let snapshot = git.snapshot(renderer, user)?;
    let prune = snapshot.prune_by_hosts(|h| host_filter.contains(h));
    git.prune_nomad_refs(renderer, remote, prune.into_iter())?;
    Ok(())
}

/// Use [`clap_complete`] to emit shell syntax for tab-completions
fn print_completions(
    renderer: &mut impl Renderer,
    gen: impl clap_complete::Generator,
) -> Result<()> {
    let mut cmd = crate::build_cli(None, None);
    let bin_name = cmd.get_name().to_string();
    renderer.writer(|writer| {
        clap_complete::generate(gen, &mut cmd, bin_name, writer);
        Ok(())
    })
}

#[cfg(test)]
mod test {
    use crate::{
        git_testing::GitRemote,
        renderer::test::{MemoryRenderer, NoRenderer},
        workflow::sync,
    };

    use super::{Filter, LsPrinter, Workflow};

    #[test]
    fn ls_one_host() {
        let remote = GitRemote::init(None);

        let clone = remote.clone("user0", "host0");
        let commit_id = clone.current_commit();

        sync(
            &mut NoRenderer,
            &clone.git,
            &clone.user,
            &clone.host,
            &clone.remote,
        )
        .unwrap();

        for (printer, expected) in [
            (
                LsPrinter::Grouped,
                format!(
                    "{}\n  refs/nomad/{}/master -> {}\n",
                    clone.host.0, clone.host.0, commit_id.0
                ),
            ),
            (
                LsPrinter::Ref,
                format!("refs/nomad/{}/master\n", clone.host.0),
            ),
            (LsPrinter::Commit, format!("{}\n", commit_id.0)),
        ] {
            let mut renderer = MemoryRenderer::new();

            Workflow::Ls {
                printer,
                user: clone.user.clone(),
                fetch_remote: Some(clone.remote.clone()),
                host_filter: Filter::All,
                branch_filter: Filter::All,
            }
            .execute(&mut renderer, &clone.git)
            .unwrap();

            assert_eq!(renderer.as_str(), expected);
        }
    }

    /// Exercise `LsPrinter::Grouped` with a bunch of `Filter::Deny`s.
    #[test]
    fn ls_two_hosts() {
        let remote = GitRemote::init(None);

        let host0 = remote.clone("user0", "host0");
        let host1 = remote.clone("user0", "host1");

        sync(
            &mut NoRenderer,
            &host0.git,
            &host0.user,
            &host0.host,
            &host0.remote,
        )
        .unwrap();

        sync(
            &mut NoRenderer,
            &host1.git,
            &host1.user,
            &host1.host,
            &host1.remote,
        )
        .unwrap();

        let mut renderer = MemoryRenderer::new();
        Workflow::Ls {
            printer: LsPrinter::Grouped,
            user: host1.user,
            fetch_remote: Some(host1.remote),
            host_filter: Filter::Deny([host0.host].into()),
            branch_filter: Filter::Deny([host1.git.current_branch(&mut renderer).unwrap()].into()),
        }
        .execute(&mut renderer, &host1.git)
        .unwrap();

        assert_eq!(renderer.as_str(), "host1\n");
    }

    #[test]
    fn filter_does_filtering() {
        for (filter, expected) in [
            (Filter::All, vec!["foo", "bar"]),
            (Filter::Allow(["foo"].into()), vec!["foo"]),
            (Filter::Deny(["foo"].into()), vec!["bar"]),
        ] {
            let mut got = vec!["foo", "bar"];
            got.retain(|i| filter.contains(i));
            assert_eq!(got, expected);
        }
    }
}