please-install 1.2.0

A unified interface package manager for many OSes
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
use std::{env, fmt::Display, fs::File, io::Read, path::PathBuf};

use clap::{ArgAction, Parser, Subcommand};
use eyre::Result;
use toml::Table;

use crate::pls_command::PlsCommand;
use crate::vendors::Vendor;


#[derive(Debug, Parser)]
#[command(about, author, name = "please", version)]
pub struct Params {
    /// skip settings
    #[arg(short = 'x', long, global = true, action = ArgAction::SetTrue)]
    pub skip_settings: bool,

    /// configuration file
    #[arg(short, long, global = true)]
    pub config: Option<String>,

    /// dry run (do not actually execute commands)
    #[arg(short = 'n', long, global = true, action = ArgAction::SetTrue)]
    pub dry_run: bool,

    /// assume yes for all prompts
    #[arg(short, long, global = true, action = ArgAction::SetTrue)]
    pub yes: bool,

    #[cfg(not(target_os = "windows"))]
    /// run as root (in case user must be sudoer)
    #[arg(long, global = true, action = ArgAction::SetTrue)]
    pub su: bool,

    /// set the installer command
    #[arg(long, global = true)]
    pub vendor: Option<Vendor>,

    #[command(subcommand)]
    pub cmd: Cmd,

    pub default_vendor: Option<Vendor>,
}

#[derive(Clone, Debug, PartialEq, Subcommand)]
pub enum Cmd {
    /// install package(s)
    #[command()]
    Install {
        /// package(s) to be installed
        #[arg(name = "PACKAGE")]
        args: Vec<String>,
    },

    /// reinstall known installed package(s)
    #[command()]
    ReinstallAll,

    /// remove package(s)
    #[command(alias = "uninstall")]
    Remove {
        /// package(s) to be removed
        #[arg(name = "PACKAGE")]
        args: Vec<String>,
    },

    /// upgrade package(s)
    #[command()]
    Upgrade {
        /// package(s) to be upgraded
        #[arg(name = "PACKAGE")]
        args: Vec<String>,
    },

    /// search for package(s)
    #[command()]
    Search {
        /// text to be searched
        #[arg(name = "QUERY")]
        args: String,

        /// paginate results
        #[arg(short, long, action = ArgAction::SetTrue)]
        paginate: bool,

        #[arg(skip)]
        pager: Option<String>,
    },

    /// get info for a package
    #[command()]
    Info {
        /// package for which to get info
        #[arg(name = "PACKAGE")]
        args: String,
    },

    /// update database
    #[command()]
    Update,

    /// list installed packages
    #[command()]
    List {
        /// paginate results
        #[arg(short, long, action = ArgAction::SetTrue)]
        paginate: bool,

        #[arg(skip)]
        pager: Option<String>,
    },

    /// list available vendors
    #[command()]
    ListVendors,

    /// move package to another vendor
    #[command()]
    Move {

        #[command(subcommand)]
        args: FromToken,
    },

    #[cfg(target_os = "linux")]
    /// enable service
    #[command()]
    Enable {
        /// service in user space
        #[arg(short, long, action = ArgAction::SetTrue)]
        user: bool,

        /// service to enable
        #[arg(name = "SERVICE")]
        service: String,
    },

    #[cfg(target_os = "linux")]
    /// disable service
    #[command()]
    Disable {
        /// service in user space
        #[arg(short, long, action = ArgAction::SetTrue)]
        user: bool,

        /// service to disable
        #[arg(name = "SERVICE")]
        service: String,
    },

    #[cfg(target_os = "linux")]
    /// start service
    #[command()]
    Start {
        /// service in user space
        #[arg(short, long, action = ArgAction::SetTrue)]
        user: bool,

        /// service to start
        #[arg(name = "SERVICE")]
        service: String,
    },

    #[cfg(target_os = "linux")]
    /// stop service
    #[command()]
    Stop {
        /// service in user space
        #[arg(short, long, action = ArgAction::SetTrue)]
        user: bool,

        /// service to stop
        #[arg(name = "SERVICE")]
        service: String,
    },

    #[cfg(target_os = "linux")]
    /// restart service
    #[command()]
    Restart {
        /// service in user space
        #[arg(short, long, action = ArgAction::SetTrue)]
        user: bool,

        /// service to restart
        #[arg(name = "SERVICE")]
        service: String,
    },

    #[cfg(target_os = "linux")]
    /// status of service
    #[command()]
    Status {
        /// service in user space
        #[arg(short, long, action = ArgAction::SetTrue)]
        user: bool,

        /// service to check status
        #[arg(name = "SERVICE")]
        service: String,
    },

    #[cfg(target_os = "linux")]
    /// reload daemon
    #[command()]
    Reload {
        /// service in user space
        #[arg(short, long, action = ArgAction::SetTrue)]
        user: bool,
    },
}


#[derive(Clone, Debug, PartialEq, Subcommand)]
pub enum FromToken {

    #[command()]
    From {
        /// original vendor from which the packages will be moved from
        #[arg(name = "ORIGIN")]
        origin: Vendor,

        #[command(subcommand)]
        to: ToToken,
    }
}

#[derive(Clone, Debug, PartialEq, Subcommand)]
pub enum ToToken {

    #[command()]
    To {
        /// destination vendor
        #[arg(name = "DESTINATION")]
        destination: Vendor,
    }
}

impl Params {
    pub fn config(mut self) -> Self {
        if self.skip_settings {
            return self;
        }

        let config = match &self.config {
            Some(config) => PathBuf::from(config),
            None => {
                let config_home = match env::var(XDG_CONFIG_HOME) {
                    Ok(config_home) => PathBuf::from(config_home),
                    Err(_) => PathBuf::from(env!["HOME"]).join(CONFIG_HOME),
                };
                config_home.join("please.toml")
            }
        };
        if config.exists() {
            let _ = self.load(config);
        }

        self
    }

    pub fn actual_vendor(&self) -> Result<Vendor> {
        match self.vendor.or(self.default_vendor) {
            Some(vendor) => Ok(vendor),
            None => Vendor::new(),
        }
    }

    fn load(&mut self, config: PathBuf) -> Result<()> {
        let mut file = File::open(config)?;
        let mut content = String::new();
        file.read_to_string(&mut content)?;
        let mut defaults: Table = content.parse()?;
        let cmd = self.cmd.to_string();
        let bind = defaults.clone();
        if let Some(value) = bind.get(cmd.as_str()).and_then(|value| value.as_table()) {
            for (k, v) in value.iter() {
                defaults.insert(k.to_string(), v.clone());
            }
        }

        match &self.cmd {
            Cmd::Search { args, .. } => {
                if defaults.get("pager").is_some() {
                    self.cmd = Cmd::Search {
                        pager: defaults
                            .get("pager")
                            .and_then(|pager| pager.as_str())
                            .map(|pager| pager.to_owned())
                            .filter(|pager| !pager.is_empty())
                            .map(|pager| pager.replace("$args", args)),
                        paginate: true,
                        args: args.to_owned(),
                    }
                }
            }
            Cmd::List { .. } => {
                if defaults.get("pager").is_some() {
                    self.cmd = Cmd::List {
                        pager: defaults
                            .get("pager")
                            .and_then(|pager| pager.as_str())
                            .map(|pager| pager.to_owned())
                            .filter(|pager| !pager.is_empty()),
                        paginate: true,
                    }
                }
            }
            _ => (),
        }

        if defaults.get("assume-yes").and_then(|yes| yes.as_bool()).unwrap_or_default() {
            self.yes = true;
        }
        if defaults.get("su").and_then(|yes| yes.as_bool()).unwrap_or_default() {
            self.su = true;
        }
        if self.vendor.is_none() {
            if let Some(vendor) = defaults.get("vendor").and_then(|vendor| vendor.as_str()) {
                let vendor: Vendor = vendor.try_into()?;
                self.default_vendor = Some(vendor);
            }
        }

        Ok(())
    }
}

impl Cmd {
    pub fn args(&self) -> Vec<String> {
        match self {
            Cmd::Install { args } => args.clone(),
            Cmd::Remove { args } => args.clone(),
            Cmd::Upgrade { args } => args.clone(),
            Cmd::Search { args, .. } => vec![args.to_string()],
            Cmd::Info { args } => vec![args.to_string()],
            _ => Vec::new(),
        }
    }

    #[cfg(target_os = "linux")]
    pub fn use_su(&self) -> Option<bool> {
        match self {
            Cmd::Enable { user, .. } => Some(!*user),
            Cmd::Disable { user, .. } => Some(!*user),
            Cmd::Start { user, .. } => Some(!*user),
            Cmd::Stop { user, .. } => Some(!*user),
            Cmd::Restart { user, .. } => Some(!*user),
            Cmd::Reload { user, .. } => Some(!*user),
            Cmd::Status { user, .. } => Some(!*user),
            _ => None,
        }
    }

    #[cfg(not(target_os = "linux"))]
    pub fn use_su(&self) -> Option<bool> {
        None
    }
}

impl Display for Cmd {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Cmd::Install { .. } => write!(f, "install"),
            Cmd::ReinstallAll => write!(f, "reinstall-all"),
            Cmd::Remove { .. } => write!(f, "remove"),
            Cmd::Upgrade { .. } => write!(f, "upgrade"),
            Cmd::Search { .. } => write!(f, "search"),
            Cmd::Info { .. } => write!(f, "info"),
            Cmd::Update => write!(f, "update"),
            Cmd::List { .. } => write!(f, "list"),
            Cmd::ListVendors => write!(f, "list-vendors"),
            Cmd::Move { .. } => write!(f, "move"),
            #[cfg(target_os = "linux")]
            Cmd::Enable { user, service } if *user => write!(f, "enable --user {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Enable { service, .. } => write!(f, "enable {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Disable { user, service } if *user => write!(f, "disable --user {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Disable { service, .. } => write!(f, "disable {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Start { user, service } if *user => write!(f, "start --user {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Start { service, .. } => write!(f, "start {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Stop { user, service } if *user => write!(f, "stop --user {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Stop { service, .. } => write!(f, "stop --user {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Restart { user, service } if *user => write!(f, "restart --user {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Restart { service, .. } => write!(f, "restart {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Status { user, service } if *user => write!(f, "status --user {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Status { service, .. } => write!(f, "status {service}"),
            #[cfg(target_os = "linux")]
            Cmd::Reload { user } if *user => write!(f, "daemon-reload --user"),
            #[cfg(target_os = "linux")]
            Cmd::Reload { .. } => write!(f, "daemon-reload"),
        }
    }
}

impl From<&Cmd> for PlsCommand {
    fn from(value: &Cmd) -> Self {
        match value {
            Cmd::Install {..} => PlsCommand::Install,
            Cmd::ReinstallAll => PlsCommand::ReinstallAll,
            Cmd::Remove {..} => PlsCommand::Remove,
            Cmd::Upgrade {args} if args.is_empty() => PlsCommand::UpgradeAll,
            Cmd::Upgrade {..} => PlsCommand::Upgrade,
            Cmd::Search {..} => PlsCommand::Search,
            Cmd::Info {..} => PlsCommand::Info,
            Cmd::Update => PlsCommand::Update,
            Cmd::List { .. } |
            Cmd::ListVendors => PlsCommand::List,
            Cmd::Move { args: FromToken::From { origin, to: ToToken::To { destination } } } => PlsCommand::Move {
                origin: origin.clone(),
                destination: destination.clone(),
            },
            #[cfg(target_os = "linux")]
            Cmd::Enable { user, service } => PlsCommand::Enable { user: *user, service: service.clone() },
            #[cfg(target_os = "linux")]
            Cmd::Disable { user, service } => PlsCommand::Disable { user: *user, service: service.clone() },
            #[cfg(target_os = "linux")]
            Cmd::Start { user, service } => PlsCommand::Start { user: *user, service: service.clone() },
            #[cfg(target_os = "linux")]
            Cmd::Stop { user, service } => PlsCommand::Stop { user: *user, service: service.clone() },
            #[cfg(target_os = "linux")]
            Cmd::Restart { user, service } => PlsCommand::Restart { user: *user, service: service.clone() },
            #[cfg(target_os = "linux")]
            Cmd::Status { user, service } => PlsCommand::Status { user: *user, service: service.clone() },
            #[cfg(target_os = "linux")]
            Cmd::Reload { user } => PlsCommand::Reload { user: *user },
        }
    }
}

#[cfg(target_os = "windows")]
const XDG_CONFIG_HOME: &str = "APPDATA";
#[cfg(not(target_os = "windows"))]
const XDG_CONFIG_HOME: &str = "XDG_CONFIG_HOME";

#[cfg(target_os = "windows")]
const CONFIG_HOME: &str = "AppData";
#[cfg(not(target_os = "windows"))]
const CONFIG_HOME: &str = ".config";