summaryrefslogtreecommitdiffstats
path: root/src/actions/modify.rs
blob: 1b58b2d1d192abe59d14030c6e1aaf7b9e3509fb (plain)
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
use crate::backup::backup;
use crate::input;
use crate::utils::fileutil::write_cal;
use crate::KhResult;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct ModifyArgs {
  /// Rebuild index
  #[structopt(short = "n", long = "dry-run")]
  pub dry_run: bool,
  /// index path
  #[structopt(subcommand)]
  pub modify_cmd: ModifyCommand,
}

#[derive(Debug, StructOpt)]
pub enum ModifyCommand {
  /// Show agenda view
  #[structopt(name = "remove-xlicerror", author = "")]
  RemoveXlicerror,
}

pub fn do_modify(args: &ModifyArgs) -> KhResult<()> {
  info!("do_modify");

  match &args.modify_cmd {
    ModifyCommand::RemoveXlicerror => {
      let dry_run = args.dry_run;

      let khlines = input::default_input_khlines()?;
      for khline in khlines {
        let (cal, count_removed) = khline.to_cal()?.with_remove_property("X-LIC-ERROR");
        if count_removed > 0 {
          if !dry_run {
            info!("Modifying {}", cal.get_path_as_string().unwrap());

            let backup_path = backup(&khline).unwrap();
            info!("Backup written to {}", backup_path.display());
            write_cal(&cal)?
          } else {
            info!("Would modify {}", cal.get_path_as_string().unwrap());
          };
        }
      }
    }
  }

  Ok(())
}

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

  use crate::testutils::*;
  use assert_fs::prelude::*;
  use predicates::prelude::*;

  use crate::cli::CommandLine;
  use crate::cli::Command::Modify;
  use structopt::StructOpt;

  #[test]
  fn test_do_modify() {
    let testdir = prepare_testdir("testdir_with_xlicerror");

    let args = CommandLine::from_iter(&["khaleesi", "modify", "remove-xlicerror"]);
    if let Modify(x) = args.cmd {
      do_modify(&x).unwrap();
    }

    let expected = indoc!(
      "
      BEGIN:VCALENDAR
      PRODID:CommuniGate Pro 6.2.5
      VERSION:2.0
      BEGIN:VEVENT
      DTSTAMP:20180813T160004Z
      UID:1c441c1b-8ca7-4898-b670-49ce30a7137b
      SEQUENCE:2
      SUMMARY:some summary
      DTSTART:20161007T073000Z
      DTEND:20161007T160000Z
      LAST-MODIFIED:20161018T095049Z
      CREATED:20161018T094913Z
      PRIORITY:5
      END:VEVENT
      END:VCALENDAR
    "
    )
    .replace("\n", "\r\n");
    let predicate = predicate::str::similar(expected);
    testdir
      .child(".khaleesi/cal/xlicerror.ics")
      .assert(predicate);
  }

  #[test]
  fn test_do_modify_dry_run() {
    let testdir = prepare_testdir("testdir_with_xlicerror");

    let args = CommandLine::from_iter(&["khaleesi", "modify", "--dry-run", "remove-xlicerror"]);
    if let Modify(x) = args.cmd {
      do_modify(&x).unwrap();
    }

    let expected = indoc!("
      BEGIN:VCALENDAR
      PRODID:CommuniGate Pro 6.2.5
      VERSION:2.0
      BEGIN:VEVENT
      DTSTAMP:20180813T160004Z
      UID:1c441c1b-8ca7-4898-b670-49ce30a7137b
      SEQUENCE:2
      SUMMARY:some summary
      DTSTART:20161007T073000Z
      DTEND:20161007T160000Z
      LAST-MODIFIED:20161018T095049Z
      CREATED:20161018T094913Z
      PRIORITY:5
      X-LIC-ERROR;X-LIC-ERRORTYPE=VALUE-PARSE-ERROR:No value for SUMMARY property. Removing entire property:
      END:VEVENT
      END:VCALENDAR
    ");
    let predicate = predicate::str::similar(expected);
    testdir
      .child(".khaleesi/cal/xlicerror.ics")
      .assert(predicate);
  }

//  #[test]
//  fn test_do_modify_negative() {
//
//    let args = CommandLine::from_iter(&["khaleesi", "modify", "nonsense"]);
//    if let Modify(x) = args.cmd {
//      assert!(do_modify(&x).is_ok());
//    }
//  }
}