n4n5 1.6.4

n4n5's utility crate
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
//! To see all subcommands, run:
//! ```shell
//! n4n5 sync
//! ```
//!
use std::{
    fs::{File, create_dir_all, write},
    io,
    path::PathBuf,
    process::Command,
    thread,
};

use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};

use crate::{
    commands::gh::lib::Gh,
    config::Config,
    config_path, config_sub_path,
    errors::GeneralError,
    get_config_path, get_config_sub_path,
    utils::{input_no, input_path},
};

use super::{movies::Movies, utils::music::MusicCliCommand};

/// Movies configuration
#[derive(Deserialize, Serialize, Default)]
pub struct SyncCliCommand {
    /// Path to the movies file
    pub file_paths: Vec<String>,

    /// Path to the folder where to save the files
    pub save_folder_path: Option<String>,

    /// Programs configuration
    pub programs: Option<ProgramsConfig>,
}

/// Programs configuration
#[derive(Deserialize, Serialize, Default)]
pub struct ProgramsConfig {
    /// Path to the cargo programs
    pub cargo_programs: Option<String>,
    /// Path to the vscode extensions
    pub vscode_extensions: Option<String>,
    /// Path to the nix programs
    pub nix: Option<String>,
}

/// Sync subcommands
#[derive(Subcommand, Debug, Clone)]
pub enum SyncSubcommand {
    /// save settings
    Settings(SettingsCommand),

    /// sync movies
    Movies {
        /// print as json
        #[arg(short = 'j', long = "json")]
        print_json: bool,
    },

    /// sync programs
    Programs,

    /// sync music
    Music,

    /// sync all
    All,
}

/// settings subcommand
#[derive(Parser, Debug, Clone)]
#[command(arg_required_else_help = true)]
pub struct SettingsCommand {
    /// settings subcommand
    #[command(subcommand)]
    pub action: SettingsAction,
}

/// settings actions
#[derive(Subcommand, Debug, Clone)]
pub enum SettingsAction {
    /// add a file to save
    Add,

    /// add all files to save
    All,
}

impl SyncSubcommand {
    /// invoke subcommand
    /// # Errors
    /// Error if error in subcommand
    pub fn invoke(self, config: &mut Config) -> Result<(), GeneralError> {
        match self {
            Self::All => SyncCliCommand::sync_all(config),
            Self::Music => MusicCliCommand::sync_music(config, Some(true)),
            Self::Movies { print_json } => Movies::full_sync_movies(config, print_json),
            Self::Programs => {
                SyncCliCommand::pre_sync_programs(config)?;
                SyncCliCommand::sync_programs(config)
            }
            Self::Settings(settings) => match settings.action {
                SettingsAction::Add => SyncCliCommand::add_file(config),
                SettingsAction::All => {
                    SyncCliCommand::pre_save_files(config)?;
                    SyncCliCommand::save_files(config)
                }
            },
        }
    }
}

impl SyncCliCommand {
    /// Get the home path
    /// # Errors
    /// Returns an error if the home directory cannot be found
    fn get_home_path() -> Result<(PathBuf, String), GeneralError> {
        let path_buf = home::home_dir().ok_or(GeneralError::new("Cannot find home directory"))?;
        let path = path_buf
            .to_str()
            .ok_or(GeneralError::new("Cannot convert home directory to string"))?
            .to_string();
        Ok((path_buf, path))
    }

    /// Pre Save the files to the specified folder
    /// # Errors
    /// Returns an error if the config cannot be saved
    fn pre_save_files(config: &mut Config) -> Result<(), GeneralError> {
        config_path!(
            config,
            sync,
            SyncCliCommand,
            save_folder_path,
            "settings folder"
        );
        Ok(())
    }

    /// Save the files to the specified folder
    /// # Errors
    /// Returns an error if the file cannot be saved
    fn save_files(config: &Config) -> Result<(), GeneralError> {
        let folder_path = get_config_path!(
            config,
            sync,
            SyncCliCommand,
            save_folder_path,
            "settings folder"
        )?;
        let files_path = match &config.config_data.sync {
            Some(settings) => settings.file_paths.as_ref(),
            None => &Vec::new(),
        };

        let (home_pathbuf, _) = SyncCliCommand::get_home_path()?;
        let files_path = files_path
            .iter()
            .map(
                |path| match PathBuf::from(path).strip_prefix(&home_pathbuf) {
                    Ok(path) => path.to_path_buf(),
                    Err(_) => PathBuf::from(path),
                },
            )
            .collect::<Vec<PathBuf>>();
        println!(
            "Saving {} files to '{}'",
            files_path.len(),
            folder_path.display()
        );
        for file_path_to_save in files_path {
            let input_path = home_pathbuf.join(&file_path_to_save);
            let mut file_input = File::open(&input_path)
                .map_err(|e| (format!("Unable to open '{}'", input_path.display()), e))?;
            let save_path = folder_path.join(&file_path_to_save);
            println!("- '{}' to '{}'", input_path.display(), save_path.display());
            if let Some(parent) = save_path.parent() {
                create_dir_all(parent).map_err(|e| {
                    (
                        format!("Unable to create parent dir for '{}'", parent.display()),
                        e,
                    )
                })?;
            }
            let mut file = File::create(&save_path)?;

            io::copy(&mut file_input, &mut file)?;
        }
        Ok(())
    }

    /// Add a file to the list of files to save
    /// # Errors
    /// Returns an error if the file path is invalid
    fn add_file(config: &mut Config) -> Result<(), GeneralError> {
        println!("Please enter the path to the file to add:");
        let (_file_path, path_string) = input_path()?;
        config.update(|config_data| {
            if let Some(local_config) = config_data.sync.as_mut() {
                local_config.file_paths.push(path_string);
            } else {
                config_data.sync = Some(SyncCliCommand {
                    file_paths: vec![path_string],
                    ..Default::default()
                });
            }
        })?;
        Ok(())
    }

    /// Sync the cargo programs
    /// # Errors
    /// Returns an error if the command fails
    fn pre_sync_programs_cargo(config: &mut Config) -> Result<(), GeneralError> {
        if !config.use_input {
            return Ok(());
        }
        if let Some(sync) = &config.config_data.sync
            && let Some(programs) = &sync.programs
            && programs.cargo_programs.is_none()
            && input_no("No cargo programs path found, do you want to add one?")?
        {
            return Ok(());
        }

        config_sub_path!(
            config,
            sync,
            SyncCliCommand,
            programs,
            ProgramsConfig,
            cargo_programs,
            "cargo programs"
        );
        Ok(())
    }
    /// Sync the cargo programs
    /// # Errors
    /// Returns an error if the command fails
    fn sync_programs_cargo(config: &Config) -> Result<(), GeneralError> {
        let cargo_path = get_config_sub_path!(
            config,
            sync,
            SyncCliCommand,
            programs,
            ProgramsConfig,
            cargo_programs,
            "cargo programs"
        )?;
        let cargo_programs = Command::new("sh")
            .arg("-c")
            .arg("cargo install --list | grep -v ':$' | sed 's/^ *//'")
            .output()?;
        let cargo_programs = String::from_utf8_lossy(&cargo_programs.stdout).to_string();
        write(&cargo_path, cargo_programs)?;
        println!("Saved cargo programs to {}", cargo_path.display());
        Ok(())
    }

    /// Pre Sync the nix-env programs
    /// # Errors
    /// Returns an error if the command fails
    fn pre_sync_programs_nix(config: &mut Config) -> Result<(), GeneralError> {
        if !config.use_input {
            return Ok(());
        }
        if let Some(sync) = &config.config_data.sync
            && let Some(programs) = &sync.programs
            && programs.nix.is_none()
            && input_no("No nix programs path found, do you want to add one?")?
        {
            return Ok(());
        }

        config_sub_path!(
            config,
            sync,
            SyncCliCommand,
            programs,
            ProgramsConfig,
            nix,
            "nix programs"
        );
        Ok(())
    }

    /// Sync the nix-env programs
    /// # Errors
    /// Returns an error if the command fails
    fn sync_programs_nix(config: &Config) -> Result<(), GeneralError> {
        let nix_path = get_config_sub_path!(
            config,
            sync,
            SyncCliCommand,
            programs,
            ProgramsConfig,
            nix,
            "nix programs"
        )?;
        let nix_programs = Command::new("sh")
            .arg("-c")
            .arg("nix-env --query | cut -d'-' -f 1")
            .output()?;
        let nix_programs = String::from_utf8_lossy(&nix_programs.stdout).to_string();
        write(&nix_path, nix_programs)?;
        println!("Saved nix programs to {}", nix_path.display());
        Ok(())
    }

    /// Pre Sync the vscode extensions
    /// # Errors
    /// Returns an error if the command fails
    fn pre_sync_programs_vscode(config: &mut Config) -> Result<(), GeneralError> {
        if !config.use_input {
            return Ok(());
        }
        if let Some(sync) = &config.config_data.sync
            && let Some(programs) = &sync.programs
            && programs.vscode_extensions.is_none()
            && input_no("No vscode extensions path found, do you want to add one?")?
        {
            return Ok(());
        }
        config_sub_path!(
            config,
            sync,
            SyncCliCommand,
            programs,
            ProgramsConfig,
            vscode_extensions,
            "vscode extensions"
        );
        Ok(())
    }

    /// Sync the vscode extensions
    /// # Errors
    /// Returns an error if the command fails
    fn sync_programs_vscode(config: &Config) -> Result<(), GeneralError> {
        let vscode_path = get_config_sub_path!(
            config,
            sync,
            SyncCliCommand,
            programs,
            ProgramsConfig,
            vscode_extensions,
            "vscode extensions"
        )?;
        let vscode_extensions = Command::new("sh")
            .arg("-c")
            .arg("code --list-extensions")
            .output()?;
        let vscode_extensions = String::from_utf8_lossy(&vscode_extensions.stdout).to_string();
        write(&vscode_path, vscode_extensions)?;
        println!("Saved vscode extensions to {}", vscode_path.display());
        Ok(())
    }

    /// Pre Sync the programs
    /// # Errors
    /// Returns an error if any of the subcommands fails
    fn pre_sync_programs(config: &mut Config) -> Result<(), GeneralError> {
        SyncCliCommand::pre_sync_programs_cargo(config)?;
        SyncCliCommand::pre_sync_programs_nix(config)?;
        SyncCliCommand::pre_sync_programs_vscode(config)?;
        Ok(())
    }

    /// Sync the programs
    /// # Errors
    /// Returns an error if any of the subcommands fails
    fn sync_programs(config: &Config) -> Result<(), GeneralError> {
        println!("Syncing programs");
        SyncCliCommand::sync_programs_cargo(config)?;
        SyncCliCommand::sync_programs_nix(config)?;
        SyncCliCommand::sync_programs_vscode(config)?;
        Ok(())
    }

    /// Sync all
    /// # Errors
    /// Returns an error if any of the subcommands fails
    fn sync_all(config: &mut Config) -> Result<(), GeneralError> {
        config.use_input = false;
        if config.debug > 1 {
            println!("Syncing all");
        }

        if config.config_data.movies.is_some() {
            Movies::pre_sync_movies(config)?;
        }
        if config.config_data.sync.is_some() {
            SyncCliCommand::pre_save_files(config)?;
            SyncCliCommand::pre_sync_programs(config)?;
        }
        if config.config_data.gh.is_some() {
            Gh::pre_sync_github(config)?;
        }

        // real sync
        thread::scope(|s| {
            if config.config_data.movies.is_some() {
                s.spawn(|| Movies::sync_movies(config, false));
            }
            if config.config_data.sync.is_some() {
                s.spawn(|| SyncCliCommand::save_files(config));
                s.spawn(|| SyncCliCommand::sync_programs(config));
            }
            if config.config_data.gh.is_some() {
                s.spawn(|| Gh::save_pulls(config));
                s.spawn(|| Gh::save_projects(config, false));
            }
        });
        Ok(())
    }
}