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
use prettytable::{cell, format, row, Table};
use snafu::{ensure, OptionExt, ResultExt};
use std::fs;
use std::{path::PathBuf, process::ExitStatus};
pub mod cli;
mod config;
pub mod error;
use config::Config;
pub mod defaults;
mod macros;
use defaults::*;
pub mod script;
use error::*;
use scrawl;
use script::Script;

// Creates a Result type that return PierError by default
pub type Result<T, E = PierError> = ::std::result::Result<T, E>;

/// Main library interface
#[derive(Debug, Default)]
pub struct Pier {
    config: Config,
    pub path: PathBuf,
    verbose: bool,
}

impl Pier {
    /// Wrapper to write the configuration to path.
    pub fn write(&self) -> Result<()> {
        self.config.write(&self.path)?;

        Ok(())
    }

    pub fn config_init(&mut self, new_path: Option<PathBuf>) -> Result<()> {
        self.path = new_path
            .unwrap_or(fallback_path().unwrap_or(xdg_config_home!("pier/config.toml").unwrap()));

        ensure!(!self.path.exists(), ConfigInitFileAlreadyExists {
            path: &self.path.as_path()
        });

        if let Some(parent_dir) = &self.path.parent() {
            if !parent_dir.exists() {
                fs::create_dir(parent_dir).context(CreateDirectory)?;
            }
        };

        &self.add_script(Script {
            alias: String::from("hello-pier"),
            command: String::from("echo Hello, Pier!"),
            description: Some(String::from("This is an example command.")),
            reference: None,
            tags: None,
        }, false);

        self.write()?;

        Ok(())
    }

    pub fn new() -> Self {
        Pier::default()
    }

    /// Create new pier directly from path.
    pub fn from_file(path: PathBuf, verbose: bool) -> Result<Self> {
        let pier = Self {
            config: Config::from(&path)?,
            verbose,
            path,
        };
        Ok(pier)
    }
    /// Create new pier from what might be a path, otherwise use the first existing default path.
    pub fn from(input_path: Option<PathBuf>, verbose: bool) -> Result<Self> {
        let path = match input_path {
            Some(path) => path,
            None => fallback_path()?,
        };

        let pier = Pier::from_file(path, verbose)?;

        Ok(pier)
    }

    /// Fetches a script that matches the alias
    pub fn fetch_script(&self, alias: &str) -> Result<&Script> {
        ensure!(!self.config.scripts.is_empty(), NoScriptsExists);

        let script = self
            .config
            .scripts
            .get(&alias.to_string())
            .context(AliasNotFound {
                alias: &alias.to_string(),
            })?;

        Ok(script)
    }

    /// Edits a script that matches the alias
    pub fn edit_script(&mut self, alias: &str) -> Result<&Script> {
        ensure!(!self.config.scripts.is_empty(), NoScriptsExists);

        let mut script =
            self.config
                .scripts
                .get_mut(&alias.to_string())
                .context(AliasNotFound {
                    alias: &alias.to_string(),
                })?;

        script.command = open_editor(Some(&script.command))?;

        println!("Edited {}", &alias);

        Ok(script)
    }

    /// Removes a script that matches the alias
    pub fn remove_script(&mut self, alias: &str) -> Result<()> {
        ensure!(!self.config.scripts.is_empty(), NoScriptsExists);

        self.config
            .scripts
            .remove(&alias.to_string())
            .context(AliasNotFound {
                alias: &alias.to_string(),
            })?;

        println!("Removed {}", &alias);

        Ok(())
    }

    /// Adds a script that matches the alias
    pub fn add_script(&mut self, script: Script, force: bool) -> Result<()> {
        if !force {
            ensure!(
                !&self.config.scripts.contains_key(&script.alias),
                AliasAlreadyExists {
                    alias: script.alias
                }
            );
        }

        println!("Added {}", &script.alias);

        self.config.scripts.insert(script.alias.to_string(), script);

        Ok(())
    }

    /// Prints only the aliases in current config file that matches tags.
    pub fn list_aliases(&self, tags: Option<Vec<String>>) -> Result<()> {
        ensure!(!self.config.scripts.is_empty(), NoScriptsExists);

        for (alias, script) in &self.config.scripts {
            match (&tags, &script.tags) {
                (Some(list_tags), Some(script_tags)) => {
                    for tag in list_tags {
                        if script_tags.contains(tag) {
                            println!("{}", alias);

                            continue;
                        }
                    }
                }
                (None, _) => {
                    println!("{}", alias);

                    continue;
                }
                _ => (),
            };
        }

        Ok(())
    }

    /// Copy an alias a script that matches the alias
    pub fn copy_script(&mut self, from_alias: &str, new_alias: &str) -> Result<()> {
        ensure!(
            !&self.config.scripts.contains_key(&new_alias.to_string()),
            AliasAlreadyExists { alias: new_alias }
        );

        // TODO: refactor the line below.
        let script = self
            .config
            .scripts
            .get(&from_alias.to_string())
            .context(AliasNotFound {
                alias: &from_alias.to_string(),
            })?
            .clone();

        println!(
            "Copy from alias {} to new alias {}",
            &from_alias.to_string(),
            &new_alias.to_string()
        );

        self.config.scripts.insert(new_alias.to_string(), script);

        Ok(())
    }

    /// Move a script that matches the alias to another alias
    pub fn move_script(&mut self, from_alias: &str, new_alias: &str, force: bool) -> Result<()> {
        if !force {
            ensure!(
                !&self.config.scripts.contains_key(&new_alias.to_string()),
                AliasAlreadyExists { alias: new_alias }
            );
        }

        let script = self
            .config
            .scripts
            .remove(&from_alias.to_string())
            .context(AliasNotFound {
                alias: &from_alias.to_string(),
            })?
            .clone();

        println!(
            "Move from alias {} to new alias {}",
            &from_alias.to_string(),
            &new_alias.to_string()
        );

        self.config.scripts.insert(new_alias.to_string(), script);

        Ok(())
    }

    /// Prints a terminal table of the scripts in current config file that matches tags.
    pub fn list_scripts(
        &self,
        tags: Option<Vec<String>>,
        cmd_full: bool,
        cmd_width: Option<usize>,
    ) -> Result<()> {
        let width = match (cmd_width, self.config.default.command_width) {
            (Some(width), _) => width,
            (None, Some(width)) => width,
            (None, None) => FALLBACK_COMMAND_DISPLAY_WIDTH,
        };
        ensure!(!self.config.scripts.is_empty(), NoScriptsExists);

        let mut table = Table::new();
        // table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
        table.set_format(*format::consts::FORMAT_DEFAULT);
        table.set_titles(row!["Alias", "Tag(s)", "Description", "Command"]);

        for (alias, script) in &self.config.scripts {

            match (&tags, &script.tags, &script.description) {
                (Some(list_tags), Some(script_tags), Some(description)) => {
                    for tag in list_tags {
                        if script_tags.contains(tag) {
                            table.add_row(row![
                                &alias,
                                script_tags.join(", "),
                                description,
                                script.display_command(cmd_full, width)
                            ]);

                            continue;
                        }
                    }
                }
                (Some(list_tags), Some(script_tags), None) => {
                    for tag in list_tags {
                        if script_tags.contains(tag) {
                            table.add_row(row![
                                &alias,
                                script_tags.join(", "),
                                "",
                                script.display_command(cmd_full, width)
                            ]);

                            continue;
                        }
                    }
                }
                (None, Some(script_tags), Some(description)) => {
                    table.add_row(row![
                        &alias,
                        script_tags.join(", "),
                        description,
                        script.display_command(cmd_full, width)
                    ]);

                    continue;
                }
                (None, Some(script_tags), None) => {
                    table.add_row(row![
                        &alias,
                        script_tags.join(", "),
                        "",
                        script.display_command(cmd_full, width)

                    ]);

                    continue;
                }
                (None, None, Some(description)) => {
                    table.add_row(row![
                        &alias,
                        "",
                        description,
                        script.display_command(cmd_full, width)
                    ]);

                    continue;
                }
                (None, None, None) => {
                    table.add_row(row![&alias, "", "", script.display_command(cmd_full, width)]);

                    continue;
                }
                _ => (),
            };
        }

        table.printstd();

        Ok(())
    }

    /// Runs a script and print stdout and stderr of the command.
    pub fn run_script(&self, alias: &str, args: Vec<String>) -> Result<ExitStatus> {
        let script = self.fetch_script(alias)?;
        let interpreter = match self.config.default.interpreter {
            Some(ref interpreter) => interpreter.clone(),
            None => fallback_shell(),
        };

        if self.verbose {
            println!("Starting script \"{}\"", alias);
            println!("-------------------------");
        };

        let cmd = match script.has_shebang() {
            true => script.run_with_shebang(args)?,
            false => script.run_with_cli_interpreter(&interpreter, args)?,
        };

        let stdout = String::from_utf8_lossy(&cmd.stdout);
        let stderr = String::from_utf8_lossy(&cmd.stderr);

        if stdout.len() > 0 {
            println!("{}", stdout);
        };
        if stderr.len() > 0 {
            eprintln!("{}", stderr);
        };

        if self.verbose {
            println!("-------------------------");
            println!("Script complete");
        };

        Ok(cmd.status)
    }
}

pub fn open_editor(content: Option<&str>) -> Result<String> {
    let edited_text = scrawl::editor::new()
        .contents(match content {
            Some(txt) => txt,
            None => "",
        })
        .open()
        .context(EditorError)?;

    Ok(edited_text)
}