regent-sdk 0.8.2

Multi-paradigm configuration management system as a library
Documentation
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
//! Line in file management attribute
//!
//! This module provides the `LineInFileBlockExpectedState` type for ensuring specific lines
//! are present or absent in files. Useful for configuration files, environment files, etc.
//!
//! # Examples
//!
//! ## Rust API
//!
//! ```no_run
//! use regent_sdk::state::attribute::utilities::lineinfile::{LineInFileBlockExpectedState, LineExpectedState};
//! use regent_sdk::{Attribute, ExpectedState, Privilege};
//!
//! // Ensure a line exists in /etc/environment
//! let env_line = LineInFileBlockExpectedState::builder("/etc/environment")
//!     .with_state(LineExpectedState::Present)
//!     .with_line("KEY=value")
//!     .with_create(true)
//!     .build()
//!     .unwrap();
//!
//! let expected_state = ExpectedState::new()
//!     .with_attribute(Attribute::lineinfile(env_line, Privilege::WithSudo, None))
//!     .build();
//! ```
//!
//! ## YAML API
//!
//! ```yaml
//! Attributes:
//!   - Detail: !LineInFile
//!       FilePath: /etc/environment
//!       State: !Present
//!       Line: "KEY=value"
//!       Create: true
//!       Privilege: !WithSudo
//! ```

use std::time::Duration;

use crate::error::RegentError;
use crate::hosts::managed_host::InternalApiCallOutcome;
use crate::hosts::managed_host::{AssessCompliance, ReachCompliance, Timeout};
use crate::hosts::properties::HostProperties;
use crate::secrets::SecretProvidersPool;
use crate::state::Check;
use crate::state::attribute::HostHandler;
use crate::state::attribute::Privilege;
use crate::state::attribute::Remediation;
use crate::state::compliance::AttributeComplianceAssessment;
use crate::state::expected_state::Parameter;
use serde::{Deserialize, Serialize};

/// Desired state of a line in a file
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum LineExpectedState {
    /// Line should exist in the file
    Present,
    /// Line should not exist in the file
    Absent,
}

/// Configuration for managing a line in a file
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "PascalCase")]
pub struct LineInFileBlockExpectedState {
    /// Absolute path to the managed file.
    file_path: String,
    /// Line to ensure is present or absent. Default state: Present.
    state: Option<LineExpectedState>,
    /// The exact line to insert, or the replacement when regexp/search_string matches.
    line: Option<Parameter<String>>,
    /// Regex that selects the line(s) to replace (last match) or delete. Mutually exclusive with SearchString.
    regexp: Option<String>,
    /// Fixed-string alternative to Regexp for matching whole lines. Mutually exclusive with Regexp.
    search_string: Option<String>,
    /// Insert the line after the last line matching this regex, or after "EOF" / "BOF". Mutually exclusive with InsertBefore.
    insert_after: Option<String>,
    /// Insert the line before the last line matching this regex, or before "BOF". Mutually exclusive with InsertAfter.
    insert_before: Option<String>,
    /// Use regexp capture groups (\\1, \\2, …) in Line as sed replacement. Requires Regexp.
    backrefs: Option<bool>,
    /// When using InsertAfter/InsertBefore, match the first occurrence instead of the last.
    firstmatch: Option<bool>,
    /// Create the file if it does not exist (default: false).
    create: Option<bool>,
}

impl LineInFileBlockExpectedState {
    pub fn builder(file_path: &str) -> LineInFileBlockExpectedState {
        LineInFileBlockExpectedState {
            file_path: file_path.to_string(),
            state: None,
            line: None,
            regexp: None,
            search_string: None,
            insert_after: None,
            insert_before: None,
            backrefs: None,
            firstmatch: None,
            create: None,
        }
    }

    pub fn with_state(&mut self, state: LineExpectedState) -> &mut Self {
        self.state = Some(state);
        self
    }

    pub fn with_line(&mut self, line: &str) -> &mut Self {
        self.line = Some(Parameter::Clear(line.to_string()));
        self
    }

    pub fn with_regexp(&mut self, regexp: &str) -> &mut Self {
        self.regexp = Some(regexp.to_string());
        self
    }

    pub fn with_search_string(&mut self, s: &str) -> &mut Self {
        self.search_string = Some(s.to_string());
        self
    }

    pub fn with_insert_after(&mut self, pattern: &str) -> &mut Self {
        self.insert_after = Some(pattern.to_string());
        self
    }

    pub fn with_insert_before(&mut self, pattern: &str) -> &mut Self {
        self.insert_before = Some(pattern.to_string());
        self
    }

    pub fn with_backrefs(&mut self, backrefs: bool) -> &mut Self {
        self.backrefs = Some(backrefs);
        self
    }

    pub fn with_firstmatch(&mut self, firstmatch: bool) -> &mut Self {
        self.firstmatch = Some(firstmatch);
        self
    }

    pub fn with_create(&mut self, create: bool) -> &mut Self {
        self.create = Some(create);
        self
    }

    pub fn build(&self) -> Result<LineInFileBlockExpectedState, RegentError> {
        self.check()?;
        Ok(self.clone())
    }
}

impl Check for LineInFileBlockExpectedState {
    fn check(&self) -> Result<(), RegentError> {
        if self.file_path.is_empty() {
            return Err(RegentError::IncoherentExpectedState(
                "FilePath cannot be empty.".to_string(),
            ));
        }
        if self.regexp.is_some() && self.search_string.is_some() {
            return Err(RegentError::IncoherentExpectedState(
                "Regexp and SearchString are mutually exclusive.".to_string(),
            ));
        }
        if self.insert_after.is_some() && self.insert_before.is_some() {
            return Err(RegentError::IncoherentExpectedState(
                "InsertAfter and InsertBefore are mutually exclusive.".to_string(),
            ));
        }
        if self.backrefs.unwrap_or(false) && self.regexp.is_none() {
            return Err(RegentError::IncoherentExpectedState(
                "Backrefs requires Regexp.".to_string(),
            ));
        }
        if self.line.is_none() && self.regexp.is_none() && self.search_string.is_none() {
            return Err(RegentError::IncoherentExpectedState(
                "At least one of Line, Regexp, or SearchString must be set.".to_string(),
            ));
        }
        Ok(())
    }
}

impl Timeout for LineInFileBlockExpectedState {
    fn default_timeout(&self) -> Duration {
        Duration::from_secs(1)
    }
}

impl<Handler: HostHandler> AssessCompliance<Handler> for LineInFileBlockExpectedState {
    async fn assess_compliance(
        &self,
        host_handler: &mut Handler,
        _host_properties: &Option<HostProperties>,
        privilege: &Privilege,
        optional_secret_provider: &Option<SecretProvidersPool>,
    ) -> Result<AttributeComplianceAssessment, RegentError> {
        if !host_handler
            .is_this_command_available("sed", privilege)
            .await
            .unwrap()
        {
            return Err(RegentError::FailedDryRunEvaluation(
                "sed is not available on this host".to_string(),
            ));
        }

        let state = self.state.as_ref().unwrap_or(&LineExpectedState::Present);
        let backrefs = self.backrefs.unwrap_or(false);
        let firstmatch = self.firstmatch.unwrap_or(false);

        let file_exists = host_handler
            .run_command(&format!("test -f {}", self.file_path), &Privilege::None)
            .await
            .unwrap()
            .return_code
            == 0;

        if !file_exists {
            if self.create.unwrap_or(false) {
                if let LineExpectedState::Absent = state {
                    return Ok(AttributeComplianceAssessment::Compliant);
                }
                return Ok(AttributeComplianceAssessment::NonCompliant(vec![
                    Remediation::LineInFile(LineInFileApiCall {
                        file_path: self.file_path.clone(),
                        line_content: self.line.clone(),
                        regexp: None,
                        api_call: LineInFileModuleInternalApiCall::CreateFile,
                        privilege: privilege.clone(),
                    }),
                ]));
            }
            return Err(RegentError::FailedDryRunEvaluation(format!(
                "{}: file not found (set Create: true to create it)",
                self.file_path
            )));
        }

        let line_content: Option<String> = match &self.line {
            Some(param) => Some(
                param
                    .clone()
                    .inner_raw(optional_secret_provider)
                    .await
                    .unwrap(),
            ),
            None => None,
        };

        match state {
            LineExpectedState::Absent => {
                assess_absent(self, host_handler, privilege, &line_content).await
            }
            LineExpectedState::Present => {
                assess_present(
                    self,
                    host_handler,
                    privilege,
                    &line_content,
                    backrefs,
                    firstmatch,
                )
                .await
            }
        }
    }
}

async fn assess_absent<Handler: HostHandler>(
    block: &LineInFileBlockExpectedState,
    host_handler: &mut Handler,
    privilege: &Privilege,
    line_content: &Option<String>,
) -> Result<AttributeComplianceAssessment, RegentError> {
    if let Some(ref regexp) = block.regexp {
        let matches = grep_lines(host_handler, regexp, &block.file_path, false).await;
        if matches.is_empty() {
            return Ok(AttributeComplianceAssessment::Compliant);
        }
        return Ok(AttributeComplianceAssessment::NonCompliant(vec![
            Remediation::LineInFile(LineInFileApiCall {
                file_path: block.file_path.clone(),
                line_content: None,
                regexp: None,
                api_call: LineInFileModuleInternalApiCall::DeleteByRegexp(regexp.clone()),
                privilege: privilege.clone(),
            }),
        ]));
    }

    // search_string or exact line: delete by line numbers
    let needle = block.search_string.as_deref().or(line_content.as_deref());

    if let Some(text) = needle {
        let fixed = block.search_string.is_some() || block.regexp.is_none();
        let matches = if fixed {
            grep_exact_line(host_handler, text, &block.file_path).await
        } else {
            grep_lines(host_handler, text, &block.file_path, false).await
        };
        if matches.is_empty() {
            return Ok(AttributeComplianceAssessment::Compliant);
        }
        return Ok(AttributeComplianceAssessment::NonCompliant(vec![
            Remediation::LineInFile(LineInFileApiCall {
                file_path: block.file_path.clone(),
                line_content: None,
                regexp: None,
                api_call: LineInFileModuleInternalApiCall::DeleteLines(matches),
                privilege: privilege.clone(),
            }),
        ]));
    }

    Ok(AttributeComplianceAssessment::Compliant)
}

async fn assess_present<Handler: HostHandler>(
    block: &LineInFileBlockExpectedState,
    host_handler: &mut Handler,
    privilege: &Privilege,
    line_content: &Option<String>,
    backrefs: bool,
    firstmatch: bool,
) -> Result<AttributeComplianceAssessment, RegentError> {
    // ── regexp path ──────────────────────────────────────────────────────────
    if let Some(ref regexp) = block.regexp {
        let matches = grep_lines(host_handler, regexp, &block.file_path, false).await;

        if !matches.is_empty() {
            let target = if firstmatch {
                *matches.first().unwrap()
            } else {
                *matches.last().unwrap()
            };

            if backrefs {
                // Always replace: can't know if current value already equals the
                // backref-expanded replacement without running sed.
                return Ok(AttributeComplianceAssessment::NonCompliant(vec![
                    Remediation::LineInFile(LineInFileApiCall {
                        file_path: block.file_path.clone(),
                        line_content: block.line.clone(),
                        regexp: Some(regexp.clone()),
                        api_call: LineInFileModuleInternalApiCall::ReplaceWithBackrefs {
                            line_number: target,
                            regexp: regexp.clone(),
                        },
                        privilege: privilege.clone(),
                    }),
                ]));
            }

            if let Some(expected) = &line_content {
                let current = get_line(host_handler, target, &block.file_path).await;
                if current.as_deref() == Some(expected.as_str()) {
                    return Ok(AttributeComplianceAssessment::Compliant);
                }
                return Ok(AttributeComplianceAssessment::NonCompliant(vec![
                    Remediation::LineInFile(LineInFileApiCall {
                        file_path: block.file_path.clone(),
                        line_content: block.line.clone(),
                        regexp: None,
                        api_call: LineInFileModuleInternalApiCall::ReplaceLine(target),
                        privilege: privilege.clone(),
                    }),
                ]));
            }

            return Ok(AttributeComplianceAssessment::Compliant);
        }

        // No match
        if backrefs {
            // No match + backrefs → leave file unchanged (Ansible behaviour)
            return Ok(AttributeComplianceAssessment::Compliant);
        }
        // Fall through to insert
    }
    // ── search_string path ───────────────────────────────────────────────────
    else if let Some(ref search) = block.search_string {
        let matches = grep_exact_line(host_handler, search, &block.file_path).await;

        if !matches.is_empty() {
            if let Some(expected) = &line_content {
                let target = if firstmatch {
                    *matches.first().unwrap()
                } else {
                    *matches.last().unwrap()
                };
                let current = get_line(host_handler, target, &block.file_path).await;
                if current.as_deref() == Some(expected.as_str()) {
                    return Ok(AttributeComplianceAssessment::Compliant);
                }
                return Ok(AttributeComplianceAssessment::NonCompliant(vec![
                    Remediation::LineInFile(LineInFileApiCall {
                        file_path: block.file_path.clone(),
                        line_content: block.line.clone(),
                        regexp: None,
                        api_call: LineInFileModuleInternalApiCall::ReplaceLine(target),
                        privilege: privilege.clone(),
                    }),
                ]));
            }
            return Ok(AttributeComplianceAssessment::Compliant);
        }
        // Fall through to insert
    }
    // ── exact line path ──────────────────────────────────────────────────────
    else if let Some(line) = &line_content {
        let matches = grep_exact_line(host_handler, line, &block.file_path).await;
        if !matches.is_empty() {
            return Ok(AttributeComplianceAssessment::Compliant);
        }
        // Fall through to insert
    }

    // ── insert ───────────────────────────────────────────────────────────────
    let insert_call = determine_insert_position(
        host_handler,
        &block.insert_after,
        &block.insert_before,
        firstmatch,
        &block.file_path,
    )
    .await;

    Ok(AttributeComplianceAssessment::NonCompliant(vec![
        Remediation::LineInFile(LineInFileApiCall {
            file_path: block.file_path.clone(),
            line_content: block.line.clone(),
            regexp: None,
            api_call: insert_call,
            privilege: privilege.clone(),
        }),
    ]))
}

async fn determine_insert_position<Handler: HostHandler>(
    host_handler: &mut Handler,
    insert_after: &Option<String>,
    insert_before: &Option<String>,
    firstmatch: bool,
    file_path: &str,
) -> LineInFileModuleInternalApiCall {
    if let Some(pattern) = insert_after {
        return match pattern.as_str() {
            "BOF" => LineInFileModuleInternalApiCall::InsertTop,
            "EOF" | "" => LineInFileModuleInternalApiCall::InsertBottom,
            _ => {
                let matches = grep_lines(host_handler, pattern, file_path, false).await;
                if matches.is_empty() {
                    LineInFileModuleInternalApiCall::InsertBottom
                } else {
                    let n = if firstmatch {
                        *matches.first().unwrap()
                    } else {
                        *matches.last().unwrap()
                    };
                    LineInFileModuleInternalApiCall::InsertAfterLine(n)
                }
            }
        };
    }

    if let Some(pattern) = insert_before {
        return match pattern.as_str() {
            "BOF" => LineInFileModuleInternalApiCall::InsertTop,
            _ => {
                let matches = grep_lines(host_handler, pattern, file_path, false).await;
                if matches.is_empty() {
                    LineInFileModuleInternalApiCall::InsertBottom
                } else {
                    let n = if firstmatch {
                        *matches.first().unwrap()
                    } else {
                        *matches.last().unwrap()
                    };
                    LineInFileModuleInternalApiCall::InsertBeforeLine(n)
                }
            }
        };
    }

    LineInFileModuleInternalApiCall::InsertBottom
}

// ── helpers ──────────────────────────────────────────────────────────────────

async fn grep_lines<Handler: HostHandler>(
    host_handler: &mut Handler,
    pattern: &str,
    file_path: &str,
    fixed: bool,
) -> Vec<u64> {
    let flag = if fixed { "-nF" } else { "-n" };
    let result = host_handler
        .run_command(
            &format!("grep {} '{}' {}", flag, pattern, file_path),
            &Privilege::None,
        )
        .await
        .unwrap();
    if result.return_code != 0 {
        return Vec::new();
    }
    result
        .stdout
        .lines()
        .filter_map(|l| l.split(':').next()?.parse::<u64>().ok())
        .collect()
}

async fn grep_exact_line<Handler: HostHandler>(
    host_handler: &mut Handler,
    line: &str,
    file_path: &str,
) -> Vec<u64> {
    let result = host_handler
        .run_command(
            &format!("grep -nxF '{}' {}", line, file_path),
            &Privilege::None,
        )
        .await
        .unwrap();
    if result.return_code != 0 {
        return Vec::new();
    }
    result
        .stdout
        .lines()
        .filter_map(|l| l.split(':').next()?.parse::<u64>().ok())
        .collect()
}

async fn get_line<Handler: HostHandler>(
    host_handler: &mut Handler,
    line_number: u64,
    file_path: &str,
) -> Option<String> {
    let result = host_handler
        .run_command(
            &format!("sed -n '{}p' {}", line_number, file_path),
            &Privilege::None,
        )
        .await
        .unwrap();
    if result.return_code == 0 {
        Some(result.stdout.trim_end_matches('\n').to_string())
    } else {
        None
    }
}

async fn get_line_count<Handler: HostHandler>(
    host_handler: &mut Handler,
    file_path: &str,
    privilege: &Privilege,
) -> u64 {
    host_handler
        .run_command(&format!("wc -l < {}", file_path), privilege)
        .await
        .unwrap()
        .stdout
        .trim()
        .parse::<u64>()
        .unwrap_or(0)
}

/// Escape content for use inside a sed `i\`, `a\`, or `c\` command (backslash only).
fn escape_sed_text(s: &str) -> String {
    s.replace('\\', "\\\\")
}

/// Escape a string for use as a sed address pattern (between `/…/`).
fn escape_sed_pattern(s: &str) -> String {
    s.replace('/', "\\/")
}

/// Escape a sed replacement string where backreferences must be preserved.
fn escape_sed_replacement_backrefs(s: &str) -> String {
    s.replace('/', "\\/")
}

// ── API call types ────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum LineInFileModuleInternalApiCall {
    InsertTop,
    InsertBottom,
    InsertAfterLine(u64),
    InsertBeforeLine(u64),
    ReplaceLine(u64),
    ReplaceWithBackrefs { line_number: u64, regexp: String },
    DeleteLines(Vec<u64>),
    DeleteByRegexp(String),
    CreateFile,
}

impl std::fmt::Display for LineInFileModuleInternalApiCall {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LineInFileModuleInternalApiCall::InsertTop => write!(f, "insert line at top"),
            LineInFileModuleInternalApiCall::InsertBottom => write!(f, "insert line at bottom"),
            LineInFileModuleInternalApiCall::InsertAfterLine(n) => {
                write!(f, "insert line after line {}", n)
            }
            LineInFileModuleInternalApiCall::InsertBeforeLine(n) => {
                write!(f, "insert line before line {}", n)
            }
            LineInFileModuleInternalApiCall::ReplaceLine(n) => write!(f, "replace line {}", n),
            LineInFileModuleInternalApiCall::ReplaceWithBackrefs { line_number, .. } => {
                write!(f, "replace line {} using backrefs", line_number)
            }
            LineInFileModuleInternalApiCall::DeleteLines(ns) => {
                write!(f, "delete lines {:?}", ns)
            }
            LineInFileModuleInternalApiCall::DeleteByRegexp(p) => {
                write!(f, "delete lines matching /{}/", p)
            }
            LineInFileModuleInternalApiCall::CreateFile => write!(f, "create file with line"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LineInFileApiCall {
    file_path: String,
    line_content: Option<Parameter<String>>,
    regexp: Option<String>,
    pub api_call: LineInFileModuleInternalApiCall,
    privilege: Privilege,
}

impl LineInFileApiCall {
    pub fn display(&self) -> String {
        format!("{} in {}", self.api_call, self.file_path)
    }
}

impl Timeout for LineInFileApiCall {
    fn default_timeout(&self) -> Duration {
        Duration::from_secs(1)
    }
}

impl<Handler: HostHandler> ReachCompliance<Handler> for LineInFileApiCall {
    async fn call(
        &self,
        host_handler: &mut Handler,
        _host_properties: &Option<HostProperties>,
        optional_secret_provider: &Option<SecretProvidersPool>,
    ) -> Result<InternalApiCallOutcome, RegentError> {
        let line: Option<String> = match &self.line_content {
            Some(p) => Some(p.clone().inner_raw(optional_secret_provider).await.unwrap()),
            None => None,
        };

        let cmd: String = match &self.api_call {
            LineInFileModuleInternalApiCall::InsertBottom => {
                let l = line.unwrap_or_default();
                format!(
                    "printf '%s\\n' '{}' >> {}",
                    escape_sed_text(&l),
                    self.file_path
                )
            }
            LineInFileModuleInternalApiCall::InsertTop => {
                let l = line.unwrap_or_default();
                let count = get_line_count(host_handler, &self.file_path, &self.privilege).await;
                if count == 0 {
                    format!(
                        "printf '%s\\n' '{}' > {}",
                        escape_sed_text(&l),
                        self.file_path
                    )
                } else {
                    format!("sed -i '1i\\{}' {}", escape_sed_text(&l), self.file_path)
                }
            }
            LineInFileModuleInternalApiCall::InsertAfterLine(n) => {
                let l = line.unwrap_or_default();
                format!(
                    "sed -i '{}a\\{}' {}",
                    n,
                    escape_sed_text(&l),
                    self.file_path
                )
            }
            LineInFileModuleInternalApiCall::InsertBeforeLine(n) => {
                let l = line.unwrap_or_default();
                format!(
                    "sed -i '{}i\\{}' {}",
                    n,
                    escape_sed_text(&l),
                    self.file_path
                )
            }
            LineInFileModuleInternalApiCall::ReplaceLine(n) => {
                let l = line.unwrap_or_default();
                format!(
                    "sed -i '{}c\\{}' {}",
                    n,
                    escape_sed_text(&l),
                    self.file_path
                )
            }
            LineInFileModuleInternalApiCall::ReplaceWithBackrefs {
                line_number,
                regexp,
            } => {
                let replacement = line.unwrap_or_default();
                format!(
                    "sed -i '{} s/{}/{}/' {}",
                    line_number,
                    escape_sed_pattern(regexp),
                    escape_sed_replacement_backrefs(&replacement),
                    self.file_path
                )
            }
            LineInFileModuleInternalApiCall::DeleteLines(numbers) => {
                let parts: Vec<String> = numbers.iter().map(|n| format!("{}d", n)).collect();
                format!("sed -i '{}' {}", parts.join(";"), self.file_path)
            }
            LineInFileModuleInternalApiCall::DeleteByRegexp(pattern) => {
                format!(
                    "sed -i '/{}/d' {}",
                    escape_sed_pattern(pattern),
                    self.file_path
                )
            }
            LineInFileModuleInternalApiCall::CreateFile => {
                let l = line.unwrap_or_default();
                if l.is_empty() {
                    format!("touch {}", self.file_path)
                } else {
                    format!(
                        "printf '%s\\n' '{}' > {}",
                        escape_sed_text(&l),
                        self.file_path
                    )
                }
            }
        };

        let result = host_handler
            .run_command(cmd.as_str(), &self.privilege)
            .await
            .unwrap();

        if result.return_code == 0 {
            Ok(InternalApiCallOutcome::Success(None))
        } else {
            Ok(InternalApiCallOutcome::Failure(format!(
                "RC: {}, STDOUT: {}, STDERR: {}",
                result.return_code, result.stdout, result.stderr
            )))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parsing_lineinfile_module_block_from_yaml_str() {
        let raw = "---
- FilePath: /etc/hosts
  Line: '192.168.1.10 myhost'
  State: !Present

- FilePath: /etc/hosts
  Line: '192.168.1.10 myhost'
  State: !Present
  Insertafter: '^127\\.0\\.0\\.1'

- FilePath: /etc/hosts
  Line: '# managed block'
  State: !Present
  Insertbefore: BOF

- FilePath: /etc/sysctl.conf
  Regexp: '^net\\.ipv4\\.ip_forward'
  Line: 'net.ipv4.ip_forward = 1'
  State: !Present

- FilePath: /etc/sysctl.conf
  Regexp: '^(net\\.ipv4\\.ip_forward)\\s*=.*'
  Line: '\\1 = 1'
  Backrefs: true
  State: !Present

- FilePath: /etc/hosts
  Regexp: '^192\\.168\\.1\\.10'
  State: !Absent

- FilePath: /etc/motd
  Line: 'welcome'
  Create: true
  State: !Present
        ";
        let _: Vec<LineInFileBlockExpectedState> = yaml_serde::from_str(raw).unwrap();
    }

    #[test]
    fn check_rejects_empty_file_path() {
        let result = LineInFileBlockExpectedState::builder("").build();
        assert!(result.is_err());
    }

    #[test]
    fn check_rejects_regexp_and_search_string_together() {
        let result = LineInFileBlockExpectedState::builder("/etc/hosts")
            .with_regexp("pattern")
            .with_search_string("literal")
            .build();
        assert!(result.is_err());
    }

    #[test]
    fn check_rejects_insert_after_and_insert_before_together() {
        let result = LineInFileBlockExpectedState::builder("/etc/hosts")
            .with_line("foo")
            .with_insert_after("^bar")
            .with_insert_before("^baz")
            .build();
        assert!(result.is_err());
    }

    #[test]
    fn check_rejects_backrefs_without_regexp() {
        let result = LineInFileBlockExpectedState::builder("/etc/hosts")
            .with_line("foo")
            .with_backrefs(true)
            .build();
        assert!(result.is_err());
    }

    #[test]
    fn check_rejects_nothing_set() {
        let result = LineInFileBlockExpectedState::builder("/etc/hosts").build();
        assert!(result.is_err());
    }

    #[test]
    fn check_accepts_valid_configurations() {
        // line only
        assert!(
            LineInFileBlockExpectedState::builder("/f")
                .with_line("hello")
                .build()
                .is_ok()
        );
        // regexp + line
        assert!(
            LineInFileBlockExpectedState::builder("/f")
                .with_regexp("^foo")
                .with_line("foo = 1")
                .build()
                .is_ok()
        );
        // regexp + line + backrefs
        assert!(
            LineInFileBlockExpectedState::builder("/f")
                .with_regexp("^(foo).*")
                .with_line("\\1 = 1")
                .with_backrefs(true)
                .build()
                .is_ok()
        );
        // regexp absent
        assert!(
            LineInFileBlockExpectedState::builder("/f")
                .with_regexp("^foo")
                .with_state(LineExpectedState::Absent)
                .build()
                .is_ok()
        );
    }
}