gitops-agent 0.1.1

GitOps Agent - continuously monitors a remote git repository against local/any change, and performs actions (e.g. executes a provided command) - given a periodicity that is defined as a time intervals.
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
use std::collections::HashMap;
use std::io::{Error, Read as IoRead, Result};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

// For processing the command
use run_script::ScriptOptions;
use wait_timeout::ChildExt;

// Scheduler, and trait for .seconds(), .minutes(), etc.
use clokwerk::{Scheduler, TimeUnits};

use git2::Repository;

use crate::git::{self, CommitMetadata};

/// Repository configuration for monitoring a git repository.
/// Use `RepoBuilder` to construct instances.
#[derive(Debug, Clone)]
pub struct Repo {
    url: String,
    username: Option<String>,
    token: Option<String>,
    local_path: Option<String>,
    branch: String,
    command: Option<String>,
    delay: u16,
    verbosity: u8,
    exec_on_start: bool,
    exit_on_first_diff: bool,
    timeout: u64,
}

impl Repo {
    /// Create a new builder for constructing a Repo
    pub fn builder(url: impl Into<String>) -> RepoBuilder {
        RepoBuilder::new(url)
    }

    // Getters for all fields
    pub fn url(&self) -> &str {
        &self.url
    }

    pub fn set_url(&mut self, url: String) {
        self.url = url;
    }

    pub fn username(&self) -> Option<&str> {
        self.username.as_deref()
    }

    pub fn token(&self) -> Option<&str> {
        self.token.as_deref()
    }

    pub fn local_path(&self) -> Option<&str> {
        self.local_path.as_deref()
    }

    pub fn set_local_path(&mut self, path: String) {
        self.local_path = Some(path);
    }

    pub fn branch(&self) -> &str {
        &self.branch
    }

    pub fn command(&self) -> Option<&str> {
        self.command.as_deref()
    }

    #[allow(dead_code)]
    pub fn delay(&self) -> u16 {
        self.delay
    }

    pub fn verbosity(&self) -> u8 {
        self.verbosity
    }

    #[allow(dead_code)]
    pub fn exec_on_start(&self) -> bool {
        self.exec_on_start
    }

    pub fn exit_on_first_diff(&self) -> bool {
        self.exit_on_first_diff
    }

    pub fn timeout(&self) -> u64 {
        self.timeout
    }
}

/// Builder for constructing `Repo` instances with a fluent API.
#[derive(Debug, Clone)]
pub struct RepoBuilder {
    url: String,
    username: Option<String>,
    token: Option<String>,
    local_path: Option<String>,
    branch: String,
    command: Option<String>,
    delay: u16,
    verbosity: u8,
    exec_on_start: bool,
    exit_on_first_diff: bool,
    timeout: u64,
}

impl RepoBuilder {
    /// Create a new builder with the required URL
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            username: None,
            token: None,
            local_path: None,
            branch: String::from("main"),
            command: None,
            delay: 120,
            verbosity: 1,
            exec_on_start: false,
            exit_on_first_diff: false,
            timeout: 0,
        }
    }

    /// Set the username for authentication
    pub fn username(mut self, username: impl Into<String>) -> Self {
        let u = username.into();
        self.username = if u.is_empty() { None } else { Some(u) };
        self
    }

    /// Set the token for authentication
    pub fn token(mut self, token: impl Into<String>) -> Self {
        let t = token.into();
        self.token = if t.is_empty() { None } else { Some(t) };
        self
    }

    /// Set the local path for cloning
    pub fn local_path(mut self, path: impl Into<String>) -> Self {
        self.local_path = Some(path.into());
        self
    }

    /// Set the branch to monitor (default: "main")
    pub fn branch(mut self, branch: impl Into<String>) -> Self {
        self.branch = branch.into();
        self
    }

    /// Set the command to execute on changes (if not set, reads from .goa file)
    pub fn command(mut self, command: impl Into<String>) -> Self {
        let c = command.into();
        self.command = if c.is_empty() { None } else { Some(c) };
        self
    }

    /// Set the delay between checks in seconds (default: 120)
    pub fn delay(mut self, delay: u16) -> Self {
        self.delay = delay;
        self
    }

    /// Set the verbosity level (default: 1)
    pub fn verbosity(mut self, verbosity: u8) -> Self {
        self.verbosity = verbosity;
        self
    }

    /// Execute command on startup (default: false)
    pub fn exec_on_start(mut self, exec: bool) -> Self {
        self.exec_on_start = exec;
        self
    }

    /// Exit after first diff is processed (default: false)
    pub fn exit_on_first_diff(mut self, exit: bool) -> Self {
        self.exit_on_first_diff = exit;
        self
    }

    /// Set command timeout in seconds (0 = no timeout, default: 0)
    pub fn timeout(mut self, timeout: u64) -> Self {
        self.timeout = timeout;
        self
    }

    /// Build the Repo instance
    pub fn build(self) -> Repo {
        Repo {
            url: self.url,
            username: self.username,
            token: self.token,
            local_path: self.local_path,
            branch: self.branch,
            command: self.command,
            delay: self.delay,
            verbosity: self.verbosity,
            exec_on_start: self.exec_on_start,
            exit_on_first_diff: self.exit_on_first_diff,
            timeout: self.timeout,
        }
    }
}

impl Repo {
    pub fn clone_repo(&self) -> Result<()> {
        let local_path = self
            .local_path
            .as_ref()
            .ok_or_else(|| Error::other("local_path is not set"))?;

        // Some OS-specific non-sense with trailing / in paths
        let local_target = str::replace(local_path, "//", "/");
        match Repository::clone(&self.url, local_target) {
            Ok(_repo) => {
                if self.verbosity > 0 {
                    info!("cloned remote repo to {}", local_path);
                }
                Ok(())
            }
            Err(e) => {
                let msg = format!("goa error: failed to clone -> {}", e);
                Err(Error::other(msg))
            }
        }
    }

    pub fn spy_for_changes(&self) -> Result<()> {
        if self.verbosity > 0 {
            info!("checking for diffs every {} seconds", self.delay);
        }

        // Create a new scheduler
        let mut scheduler = Scheduler::new();
        let delay = self.delay as u32;
        let cloned_repo = Arc::new(Mutex::new(self.clone()));

        if self.exec_on_start {
            let repo_guard = cloned_repo
                .lock()
                .map_err(|e| Error::other(format!("Failed to acquire lock: {}", e)))?;
            match do_process_once(&repo_guard) {
                Ok(()) => {
                    if self.verbosity > 0 {
                        info!("exec on startup complete");
                    }
                }
                Err(e) => {
                    // exec_on_start failure is fatal - return the error
                    return Err(Error::other(format!("failed to exec on startup: {}", e)));
                }
            }
        }

        // Add the repo to scheduler
        scheduler.every(delay.seconds()).run(move || {
            match cloned_repo.lock() {
                Ok(repo_guard) => {
                    if let Err(e) = do_process(&repo_guard) {
                        eprintln!("goa error: unable to process repo: {}", e);
                    }
                }
                Err(e) => {
                    eprintln!("goa error: failed to acquire lock: {}", e);
                }
            }
        });

        // Manually run the scheduler in an event loop
        loop {
            scheduler.run_pending();
            thread::sleep(Duration::from_millis(10));
        }
    }
}

pub fn read_goa_file(goa_path: &str) -> String {
    if std::path::Path::new(goa_path).exists() {
        std::fs::read_to_string(goa_path).unwrap_or_else(|_| {
            String::from("echo 'failed to read .goa file'")
        })
    } else {
        String::from("echo 'no goa file found yet'")
    }
}

/// Execute a command with optional environment variables.
/// This is a public function for use by other modules (e.g., radicle).
pub fn execute_command(
    command: &str,
    working_dir: &str,
    env_vars: Option<HashMap<String, String>>,
    timeout: u64,
    verbosity: u8,
) -> Result<String> {
    if verbosity > 1 {
        info!("running -> {}", command);
        if timeout > 0 {
            info!("timeout -> {} seconds", timeout);
        }
    }

    if verbosity > 2 {
        debug!("path -> {}", working_dir);
    }

    // Use timeout-based execution if timeout is set
    if timeout > 0 {
        return execute_command_with_timeout(command, working_dir, env_vars, timeout, verbosity);
    }

    // No timeout - use run_script for simpler execution
    let mut options = ScriptOptions::new();
    options.working_directory = Some(PathBuf::from(working_dir));
    options.env_vars = env_vars;

    let args = vec![];

    let (code, output, error) = run_script::run(command, &args, &options)
        .map_err(|e| Error::other(format!("Failed to run script: {}", e)))?;

    if verbosity > 1 {
        info!("command status: {}", code);
        if !error.is_empty() {
            info!("command stderr:\n{}", error);
        }
    }

    // Print stderr but don't exit - many commands write progress to stderr
    if !error.is_empty() {
        eprintln!("{}", error);
    }

    // Return error only if command failed (non-zero exit code)
    if code != 0 {
        return Err(Error::other(format!("Command exited with code {}", code)));
    }

    Ok(output)
}

/// Execute a command with a timeout.
fn execute_command_with_timeout(
    command: &str,
    working_dir: &str,
    env_vars: Option<HashMap<String, String>>,
    timeout: u64,
    verbosity: u8,
) -> Result<String> {
    let timeout_duration = Duration::from_secs(timeout);

    let mut cmd = Command::new("sh");
    cmd.arg("-c")
        .arg(command)
        .current_dir(working_dir)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    if let Some(vars) = env_vars {
        for (key, value) in vars {
            cmd.env(key, value);
        }
    }

    let mut child = cmd
        .spawn()
        .map_err(|e| Error::other(format!("Failed to spawn command: {}", e)))?;

    match child.wait_timeout(timeout_duration) {
        Ok(Some(status)) => {
            let mut stdout = String::new();
            let mut stderr = String::new();

            if let Some(mut out) = child.stdout.take() {
                out.read_to_string(&mut stdout)
                    .map_err(|e| Error::other(format!("Failed to read stdout: {}", e)))?;
            }
            if let Some(mut err) = child.stderr.take() {
                err.read_to_string(&mut stderr)
                    .map_err(|e| Error::other(format!("Failed to read stderr: {}", e)))?;
            }

            let code = status.code().unwrap_or(-1);

            if verbosity > 1 {
                info!("command status: {}", code);
                if !stderr.is_empty() {
                    info!("command stderr:\n{}", stderr);
                }
            }

            // Print stderr but don't exit - many commands write progress to stderr
            if !stderr.is_empty() {
                eprintln!("{}", stderr);
            }

            // Return error only if command failed (non-zero exit code)
            if code != 0 {
                return Err(Error::other(format!("Command exited with code {}", code)));
            }

            Ok(stdout)
        }
        Ok(None) => {
            if verbosity > 0 {
                warn!("Command timed out after {} seconds, killing process", timeout);
            }
            child
                .kill()
                .map_err(|e| Error::other(format!("Failed to kill timed-out process: {}", e)))?;
            child.wait().ok();

            Err(Error::other(format!(
                "Command timed out after {} seconds",
                timeout
            )))
        }
        Err(e) => Err(Error::other(format!("Failed to wait on command: {}", e))),
    }
}

/// Get the effective command to execute: use configured command or read from .goa file
fn get_effective_command(repo: &Repo, local_path: &str) -> String {
    match repo.command() {
        Some(cmd) => cmd.to_string(),
        None => read_goa_file(&format!("{}/.goa", local_path)),
    }
}

pub fn do_process_once(repo: &Repo) -> Result<()> {
    let local_path = repo
        .local_path()
        .ok_or_else(|| Error::other("local_path is not set"))?;

    let local_repo = Repository::open(local_path).map_err(|e| {
        Error::other(format!("goa error: failed to open the cloned repo: {}", e))
    })?;

    // Get commit metadata (thread-safe, no global env var mutation)
    let metadata = git::get_last_commit_metadata(&local_repo, repo.branch(), repo.verbosity())
        .map_err(|e| Error::other(format!("branch '{}' not found: {}", repo.branch(), e)))?;

    // Determine the effective command: use configured command or read from .goa file
    let effective_command = get_effective_command(repo, local_path);
    if repo.verbosity() > 2 {
        debug!("effective command: {}", effective_command);
    }

    match do_task(repo, &effective_command, Some(&metadata)) {
        Ok(output) => {
            if repo.verbosity() > 0 {
                info!("command stdout: {}", output);
            } else {
                println!("{output}");
            }
        }
        Err(e) => {
            eprintln!("goa error: do_task error {}", e);
        }
    }
    Ok(())
}

pub fn do_process(repo: &Repo) -> Result<()> {
    let local_path = repo
        .local_path()
        .ok_or_else(|| Error::other("local_path is not set"))?;

    let local_repo = Repository::open(local_path).map_err(|e| {
        Error::other(format!("goa error: failed to open the cloned repo: {}", e))
    })?;

    if repo.verbosity() > 1 {
        info!("checking for diffs at origin/{}!", repo.branch());
    }

    match git::is_diff(&local_repo, "origin", repo.branch(), repo.verbosity()) {
        Ok(commit) => {
            match git::do_merge(&local_repo, repo.branch(), commit, repo.verbosity()) {
                Ok(metadata) => {
                    // Determine the effective command: use configured command or read from .goa file
                    let effective_command = get_effective_command(repo, local_path);
                    if repo.verbosity() > 2 {
                        debug!("effective command: {}", effective_command);
                    }

                    match do_task(repo, &effective_command, Some(&metadata)) {
                        Ok(output) => {
                            if repo.verbosity() > 0 {
                                info!("command stdout: {}", output);
                            } else {
                                println!("{output}");
                            }

                            if repo.exit_on_first_diff() {
                                // Intentional exit after first diff processed
                                std::process::exit(0);
                            }
                        }
                        Err(e) => {
                            eprintln!("goa error: do_task error {}", e);
                        }
                    }
                }
                Err(e) => {
                    eprintln!("goa error: do_merge error {}", e);
                }
            }
        }
        Err(e) => {
            // There were no diffs, so we move right along
            if repo.verbosity() > 1 {
                debug!("{}", e);
            }
        }
    }

    Ok(())
}

/// Execute a command with optional commit metadata passed as env vars to child process.
/// This is thread-safe as env vars are only set in the child process, not globally.
/// If timeout > 0, the command will be killed after the specified number of seconds.
fn do_task(repo: &Repo, command: &str, metadata: Option<&CommitMetadata>) -> Result<String> {
    let local_path = repo
        .local_path()
        .ok_or_else(|| Error::other("local_path is not set"))?;

    if repo.verbosity() > 1 {
        info!("running -> {}", command);
        if repo.timeout() > 0 {
            info!("timeout -> {} seconds", repo.timeout());
        }
    }

    if repo.verbosity() > 2 {
        debug!("path -> {}", local_path);
    }

    // Use timeout-based execution if timeout is set
    if repo.timeout() > 0 {
        return do_task_with_timeout(repo, command, metadata, local_path);
    }

    // No timeout - use run_script for simpler execution
    let mut options = ScriptOptions::new();
    options.working_directory = Some(PathBuf::from(local_path));

    // Pass commit metadata as env vars to child process only (thread-safe)
    if let Some(meta) = metadata {
        options.env_vars = Some(meta.to_env_vars());
    }

    let args = vec![];

    // run the script and get the script execution output
    let (code, output, error) = run_script::run(command, &args, &options)
        .map_err(|e| Error::other(format!("Failed to run script: {}", e)))?;

    if repo.verbosity() > 1 {
        info!("command status: {}", code);
        info!("command stderr:\n{}", error);
    }

    if !error.is_empty() {
        eprintln!("{}", error);
        // Exit with the command's exit code - this is intentional behavior
        std::process::exit(code);
    }

    Ok(output)
}

/// Execute a command with a timeout. Kills the process if it exceeds the timeout.
fn do_task_with_timeout(
    repo: &Repo,
    command: &str,
    metadata: Option<&CommitMetadata>,
    local_path: &str,
) -> Result<String> {
    let timeout_duration = Duration::from_secs(repo.timeout());

    // Build the command using sh -c for shell interpretation
    let mut cmd = Command::new("sh");
    cmd.arg("-c")
        .arg(command)
        .current_dir(local_path)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    // Pass commit metadata as env vars
    if let Some(meta) = metadata {
        for (key, value) in meta.to_env_vars() {
            cmd.env(key, value);
        }
    }

    let mut child = cmd
        .spawn()
        .map_err(|e| Error::other(format!("Failed to spawn command: {}", e)))?;

    // Wait with timeout
    match child.wait_timeout(timeout_duration) {
        Ok(Some(status)) => {
            // Process completed within timeout
            let mut stdout = String::new();
            let mut stderr = String::new();

            if let Some(mut out) = child.stdout.take() {
                out.read_to_string(&mut stdout)
                    .map_err(|e| Error::other(format!("Failed to read stdout: {}", e)))?;
            }
            if let Some(mut err) = child.stderr.take() {
                err.read_to_string(&mut stderr)
                    .map_err(|e| Error::other(format!("Failed to read stderr: {}", e)))?;
            }

            let code = status.code().unwrap_or(-1);

            if repo.verbosity() > 1 {
                info!("command status: {}", code);
                info!("command stderr:\n{}", stderr);
            }

            if !stderr.is_empty() {
                eprintln!("{}", stderr);
                // Exit with the command's exit code - this is intentional behavior
                std::process::exit(code);
            }

            Ok(stdout)
        }
        Ok(None) => {
            // Timeout expired - kill the process
            if repo.verbosity() > 0 {
                warn!(
                    "Command timed out after {} seconds, killing process",
                    repo.timeout()
                );
            }
            child
                .kill()
                .map_err(|e| Error::other(format!("Failed to kill timed-out process: {}", e)))?;
            child.wait().ok(); // Clean up zombie process

            Err(Error::other(format!(
                "Command timed out after {} seconds",
                repo.timeout()
            )))
        }
        Err(e) => Err(Error::other(format!("Failed to wait on command: {}", e))),
    }
}

#[cfg(test)]
mod repos_tests {
    use super::*;
    use std::io::ErrorKind;

    #[test]
    fn test_creation_of_repo() {
        let repo = Repo::builder("file://.")
            .local_path(".")
            .branch("develop")
            .build();

        assert_eq!("develop", repo.branch());
    }

    #[test]
    fn test_builder_defaults() {
        let repo = Repo::builder("https://github.com/test/repo").build();

        assert_eq!("main", repo.branch());
        assert_eq!(120, repo.delay());
        assert_eq!(1, repo.verbosity());
        assert!(!repo.exec_on_start());
        assert!(!repo.exit_on_first_diff());
        assert_eq!(0, repo.timeout());
    }

    #[test]
    fn test_do_task() {
        let repo = Repo::builder("file://.")
            .local_path(".")
            .branch("develop")
            .command("echo hello")
            .verbosity(3)
            .build();

        let res = do_task(&repo, "echo hello", None);
        assert_eq!(String::from("hello\n"), res.unwrap());
    }

    #[test]
    fn test_do_process() -> Result<()> {
        let temp_dir = std::env::temp_dir();
        let mut local_path: String = temp_dir.into_os_string().into_string().unwrap();
        let tmp_dir_name = format!("/{}/", uuid::Uuid::new_v4());
        local_path.push_str(&tmp_dir_name);

        let repo = Repo::builder("https://github.com/kitplummer/goa_tester")
            .local_path(&local_path)
            .branch("main")
            .command("echo hello")
            .verbosity(2)
            .build();

        repo.clone_repo()?;

        assert_eq!(do_process(&repo)?, ());
        Ok(())
    }

    #[test]
    fn test_do_process_no_clone() -> Result<()> {
        let temp_dir = std::env::temp_dir();
        let mut local_path: String = temp_dir.into_os_string().into_string().unwrap();
        let tmp_dir_name = format!("/{}/", uuid::Uuid::new_v4());
        local_path.push_str(&tmp_dir_name);

        let mut repo = Repo::builder("https://github.com/kitplummer/goa_tester")
            .local_path(&local_path)
            .branch("main")
            .command("echo hello")
            .verbosity(2)
            .build();

        repo.clone_repo()?;
        repo.set_local_path(String::from("/blahdyblahblah"));
        let res = do_process(&repo).unwrap_err();
        assert_eq!(res.kind(), ErrorKind::Other);

        Ok(())
    }

    #[test]
    fn test_do_process_no_command() -> Result<()> {
        let temp_dir = std::env::temp_dir();
        let mut local_path: String = temp_dir.into_os_string().into_string().unwrap();
        let tmp_dir_name = format!("/{}/", uuid::Uuid::new_v4());
        local_path.push_str(&tmp_dir_name);
        println!("local_path: {:?}", local_path);

        let repo = Repo::builder("https://github.com/kitplummer/goa_tester")
            .local_path(&local_path)
            .branch("main")
            .verbosity(3)
            .build();

        repo.clone_repo()?;

        assert_eq!(do_process(&repo)?, ());
        Ok(())
    }

    #[test]
    fn test_no_goa_file() {
        let res = read_goa_file("/blahdy/.goa");
        assert_eq!(res, String::from("echo 'no goa file found yet'"));
    }

    #[test]
    fn test_do_task_with_timeout() {
        let repo = Repo::builder("file://.")
            .local_path(".")
            .branch("develop")
            .command("echo hello")
            .verbosity(3)
            .timeout(5)
            .build();

        let res = do_task(&repo, "echo hello", None);
        assert_eq!(String::from("hello\n"), res.unwrap());
    }

    #[test]
    fn test_do_task_timeout_exceeded() {
        let repo = Repo::builder("file://.")
            .local_path(".")
            .branch("develop")
            .command("sleep 10")
            .verbosity(1)
            .timeout(1)
            .build();

        let res = do_task(&repo, "sleep 10", None);
        assert!(res.is_err());
        assert!(res.unwrap_err().to_string().contains("timed out"));
    }
}