up 0.18.1

up is a tool to help you keep your machine up to date.
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
//! Options passed to `up` commands.
mod paths;
pub(crate) mod start_time;

use crate::opts::paths::TempDir;
use crate::opts::start_time::StartTime;
use camino::Utf8PathBuf;
use clap::Parser;
use clap::ValueEnum;
use clap::ValueHint;
use clap::builder::styling::AnsiColor;
use clap::builder::styling::Styles;
use clap_complete::Shell;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use std::ffi::OsString;

/// The default fallback path inside a fallback repo to look for the up.yaml file in.
pub(crate) const FALLBACK_CONFIG_PATH: &str = "dotfiles/.config/up/up.yaml";
/// URL to use to find the latest version of up.
pub(crate) const LATEST_RELEASE_URL: &str =
    "https://api.github.com/repos/gibfahn/up/releases/latest";
#[cfg(target_os = "linux")]
/// URL to use to download the latest release of up for Linux.
pub(crate) const SELF_UPDATE_URL: &str =
    "https://github.com/gibfahn/up/releases/latest/download/up-linux";
#[cfg(target_os = "macos")]
/// URL to use to download the latest release of up for macOS.
pub(crate) const SELF_UPDATE_URL: &str =
    "https://github.com/gibfahn/up/releases/latest/download/up-darwin";

/// `up --help` terminal styling.
const STYLES: Styles = Styles::styled()
    .header(AnsiColor::Green.on_default().bold())
    .usage(AnsiColor::Green.on_default().bold())
    .literal(AnsiColor::Blue.on_default().bold())
    .placeholder(AnsiColor::Cyan.on_default());

/// Builds the Args struct from CLI input and from environment variable input.
#[must_use]
pub fn parse() -> Opts {
    Opts::parse()
}

// Don't complain about bare links in my clap document output.
#[allow(clippy::doc_markdown, rustdoc::bare_urls)]
/**
Up is a tool to help you manage your developer machine. `up run` runs the tasks defined in its
config directory. It handles linking configuration files into the right locations, and running
scripts to make sure the tools you need are installed and up to date. It is designed to complete
common bootstrapping tasks without dependencies, so you can bootstrap a new machine by:

❯ curl --create-dirs -Lo ~/bin/up https://github.com/gibfahn/up/releases/latest/download/up-$(uname) && chmod +x ~/bin/up

❯ ~/bin/up run --bootstrap --fallback-url https://github.com/gibfahn/dot

Running `up` without a subcommand runs `up run` with no parameters, which is useful for
post-bootstrapping, when you want to just run all your setup steps again, to make sure
everything is installed and up-to-date. For this reason it's important to make your up tasks
idempotent, so they skip if nothing is needed.

There are also a number of libraries built into up, that can be accessed directly as well as via
up task configs, e.g. `up link` to link dotfiles.

For debugging, run with `RUST_LIB_BACKTRACE=1` to show error/panic traces.
Logs from the latest run are available at `$TMPDIR/up/logs/up_<timestamp>.log` by default.
Parallel tasks are run with rayon, so you can control the number of threads used via `RAYON_NUM_THREADS`, e.g. `RAYON_NUM_THREADS=1 up` to run everything sequentially.
*/
#[derive(Debug, Clone, Parser)]
#[clap(version, styles = STYLES)]
pub struct Opts {
    /// Set the logging level explicitly (options: off, error, warn, info,
    /// debug, trace).
    #[clap(
        long,
        short = 'l',
        default_value = "up=info",
        env = "RUST_LOG",
        alias = "log-level"
    )]
    pub log: String,

    /**
    Temporary directory to use for logs, fifos, and other intermediate artifacts.
    */
    #[clap(long, env = "UP_TEMP_DIR", default_value_t, value_hint = ValueHint::DirPath, alias = "up-dir")]
    pub temp_dir: TempDir,

    /// Set the file logging level explicitly (options: off, error, warn, info,
    /// debug, trace).
    #[clap(long, default_value = "trace", env = "FILE_RUST_LOG")]
    pub file_log_level: String,

    /// Whether to color terminal output.
    #[clap(long, default_value = "auto", ignore_case = true, value_enum)]
    pub color: Color,

    /// Path to the up.yaml file for up.
    #[clap(long, short = 'c', default_value = "$XDG_CONFIG_HOME/up/up.yaml", value_hint = ValueHint::FilePath)]
    pub(crate) config: String,

    /**
    The timestamp where we started this action.

    Hidden as users shouldn't normally be setting this.
    */
    #[clap(long, hide(true), default_value_t)]
    pub start_time: StartTime,

    /// Clap subcommand to run.
    #[clap(subcommand)]
    pub(crate) cmd: Option<SubCommand>,
}

/// Settings for colouring output.
#[derive(Debug, ValueEnum, Clone)]
pub enum Color {
    /// Auto: Colour on if stderr isatty, else off.
    Auto,
    /// Always: Always enable colours.
    Always,
    /// Never: Never enable colours.
    Never,
}

/// Optional subcommand (e.g. the "link" in "up link").
#[derive(Debug, Clone, Parser)]
pub(crate) enum SubCommand {
    /**
    Run the update tasks.

    If you don't provide a subcommand this is the default action.
    If you want to pass Run args you will need to specify the subcommand.
    */
    Run(RunOptions),
    /// Symlink your dotfiles from a git repo to your home directory.
    Link(LinkOptions),
    /// Clone or update a repo at a path.
    Git(GitOptions),
    /// Set macOS defaults in plist files.
    Defaults(DefaultsOptions),
    /// Generate up config from current system state.
    Generate(GenerateOptions),
    /// Update the up CLI itself.
    Self_(UpdateSelfOptions),
    /// Generate various docs or completions for up.
    Doc(DocOptions),

    /// List available tasks.
    List(RunOptions),
    /**
    Runs a command in a fake tty.
    */
    Faketty(FakettyOptions),
}

/// Options passed to `up run`.
#[derive(Debug, Clone, Parser, Default)]
pub(crate) struct RunOptions {
    /// Run the bootstrap list of tasks in series first, then run the rest in
    /// parallel. Designed for first-time setup.
    #[clap(short, long)]
    pub(crate) bootstrap: bool,
    /// Keep going even if a bootstrap task fails.
    #[clap(short, long)]
    pub(crate) keep_going: bool,
    /// Fallback git repo URL to download to get the config.
    #[clap(short = 'f', long, value_hint = ValueHint::Url)]
    pub(crate) fallback_url: Option<String>,
    /// Fallback path inside the git repo to get the config.
    /// The default path assumes your `fallback_url` points to a dotfiles repo
    /// that is linked into ~.
    #[clap(
        short = 'p',
        long,
        default_value = FALLBACK_CONFIG_PATH,
        value_hint = ValueHint::FilePath
    )]
    pub(crate) fallback_path: Utf8PathBuf,
    /**
    Optionally pass one or more tasks to run. The default is to run all
    tasks. This option can be provided multiple times, or use a comma-separated list of values.

    EXAMPLES:

    ❯ up run --tasks=rust,apt --tasks=otherslowtask
    */
    #[clap(short = 't', long, value_delimiter = ',')]
    pub(crate) tasks: Option<Vec<String>>,

    /**
    Tasks stdout/stderr inherit from up's stdout/stderr.

    By default this is true if only one task is executed, and false otherwise.
    Piping multiple commands to the stdout/stderr of the process will cause task output to be interleaved, which is very confusing when many tasks are run.
    */
    #[clap(long)]
    pub(crate) console: Option<bool>,

    /**
    Optionally pass one or more tasks to exclude. The default is to exclude no
    tasks. Excluded tasks are not run even if specified in `--tasks` (excluding takes
    priority). This option can be provided multiple times. Tasks specified do not have to exist.

    EXAMPLES:

    ❯ up run --exclude-tasks=brew,slowtask --exclude-tasks=otherslowtask
    */
    #[clap(long, value_delimiter = ',')]
    pub(crate) exclude_tasks: Option<Vec<String>>,
}

/// Options passed to `up link`.
#[derive(Debug, Clone, Parser, Default, Serialize, Deserialize)]
pub(crate) struct LinkOptions {
    /// Path where your dotfiles are kept (hopefully in source control).
    #[clap(short = 'f', long = "from", default_value = "~/code/dotfiles", value_hint = ValueHint::DirPath)]
    pub(crate) from_dir: String,
    /// Path to link them to.
    #[clap(short = 't', long = "to", default_value = "~", value_hint = ValueHint::DirPath)]
    pub(crate) to_dir: String,
}

/// Options passed to `up git`.
#[derive(Debug, Clone, Default, Parser)]
pub struct GitOptions {
    /// URL of git repo to download.
    #[clap(long, value_hint = ValueHint::Url)]
    pub git_url: String,
    /// Path to download git repo to.
    #[clap(long, value_hint = ValueHint::DirPath)]
    pub git_path: Utf8PathBuf,
    /// Remote to set/update.
    #[clap(long, default_value = crate::tasks::git::DEFAULT_REMOTE_NAME)]
    pub remote: String,
    /// Branch to checkout when cloning/updating. Defaults to default branch for
    /// cloning, and current branch for updating.
    #[clap(long)]
    pub branch: Option<String>,
    /// Prune merged PR branches. Deletes local branches where the push branch
    /// has been merged into the upstream branch, and the push branch has now
    /// been deleted.
    #[clap(long)]
    pub prune: bool,
}

/// Options passed to `up generate`.
#[derive(Debug, Clone, Parser)]
pub(crate) struct GenerateOptions {
    /// Lib to generate.
    #[clap(subcommand)]
    pub(crate) lib: Option<GenerateLib>,
}

/// Options passed to `up schema`.
#[derive(Debug, Clone, Parser)]
pub(crate) struct SchemaOptions {
    /// Lib to generate. Defaults to writing to stdout.
    pub(crate) path: Option<Utf8PathBuf>,
}

/// Arguments for the `up generate manpages` subcommand.
#[derive(Debug, Clone, Parser)]
pub struct ManpagesOptions {
    /// Directory into which to write the generated manpages.
    #[clap(long, value_hint = ValueHint::DirPath)]
    pub(crate) output_dir: Utf8PathBuf,
}

/// Options passed to `up self`.
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
pub(crate) struct UpdateSelfOptions {
    /// URL to download update from.
    #[clap(long, default_value = SELF_UPDATE_URL, value_hint = ValueHint::Url)]
    pub(crate) url: String,
    /// Set to update self even if it seems to be a development install.
    /// Assumes a dev install when the realpath of the current binary is in a
    /// subdirectory of the cargo root path that the binary was originally built in.
    #[clap(long)]
    pub(crate) always_update: bool,
}

/// Options passed to `up doc`.
#[derive(Debug, Clone, Parser)]
pub(crate) struct DocOptions {
    /// Type of documentation to generate.
    #[clap(subcommand)]
    pub(crate) subcmd: DocSubcommand,
}

/// Subcommands supported by `up doc`.
#[derive(Debug, Clone, Parser)]
pub(crate) enum DocSubcommand {
    /**
    Generate shell completions to stdout.

    Completions are printed to stdout. They are designed to be written to a file.

    EXAMPLES:

    ❯ up doc completions zsh | sudo tee >/dev/null /usr/local/share/zsh/site-functions/_up
    */
    Completions(CompletionsOptions),
    /**
    Write the up task yaml schema.

    EXAMPLES:

    ❯ up doc schema --path /path/to/up-task-schema.json
    */
    Schema(SchemaOptions),
    /**
    Generate man pages for up and its subcommands.

    Manpages are generated into the output directory specified by `--output-dir`.

    EXAMPLES:

    ❯ up generate manpages --output-dir /usr/local/share/man/man1/
    */
    #[clap(visible_alias = "man")]
    Manpages(ManpagesOptions),
    /**
    Print a markdown file with documentation for up and its subcommands.

    Markdown file contents are written to the stdout.
    */
    Markdown,
}

/// Options passed to `up completions`.
#[derive(Debug, Clone, Parser)]
pub(crate) struct CompletionsOptions {
    /// Shell for which to generate completions.
    #[clap(value_enum)]
    pub(crate) shell: Shell,
}

impl Default for UpdateSelfOptions {
    fn default() -> Self {
        Self {
            url: SELF_UPDATE_URL.to_owned(),
            always_update: false,
        }
    }
}

/// Subcommands supported by `up generate`.
#[derive(Debug, Clone, Parser)]
pub(crate) enum GenerateLib {
    /// Generate a git repo.
    Git(GenerateGitConfig),
    /// Generate macOS defaults commands (not yet implemented).
    Defaults(GenerateDefaultsConfig),
}

/// Options passed to `up generate git`.
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
pub struct GenerateGitConfig {
    /// Path to yaml file to update.
    #[clap(long, value_hint = ValueHint::FilePath)]
    pub(crate) path: Utf8PathBuf,
    /// Paths to search within.
    #[clap(long, default_value = "~", value_hint = ValueHint::DirPath)]
    pub(crate) search_paths: Vec<Utf8PathBuf>,
    /// Exclude paths containing this value. e.g. '/tmp/' to exclude anything in
    /// a tmp dir.
    #[clap(long)]
    pub(crate) excludes: Option<Vec<String>>,
    /// Prune all repos for branches that have already been merged and deleted
    /// upstream.
    #[clap(long)]
    pub(crate) prune: bool,
    /// Order to save remotes, other remotes will be included after those listed here.
    #[clap(long)]
    pub(crate) remote_order: Vec<String>,
}

/// Options passed to `up generate defaults`.
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
pub struct GenerateDefaultsConfig {
    /// Path to yaml file to update.
    #[clap(long, value_hint = ValueHint::FilePath)]
    pub(crate) path: Utf8PathBuf,
}

/// Options passed to `up defaults`.
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
pub struct DefaultsOptions {
    /// Read from the current host, same as `defaults -currentHost`.
    #[clap(long = "currentHost")]
    pub(crate) current_host: bool,
    /// Defaults action to take.
    #[clap(subcommand)]
    pub(crate) subcommand: DefaultsSubcommand,
}

/// Subcommands supported by `up defaults`.
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
pub enum DefaultsSubcommand {
    /// Read a defaults option and print it to the stdout as yaml.
    Read(DefaultsReadOptions),
    /**
    Write a yaml-encoded value to a defaults plist file.
    A domain, key, and value must be provided (you can optionally use `-g` to specify the global domain).
    */
    Write(DefaultsWriteOptions),
}

/// Options passed to `up defaults read`.
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
pub struct DefaultsReadOptions {
    /// Read from the global domain. If you set this, do not also pass a domain argument.
    #[clap(short = 'g', long = "globalDomain")]
    pub(crate) global_domain: bool,
    /**
    Defaults domain to print. Use `-` to read from stdin.
    */
    pub(crate) domain: Option<String>,
    /// Defaults key to print.
    pub(crate) key: Option<String>,
}

/// Options passed to `up defaults write`.
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
pub struct DefaultsWriteOptions {
    /// Read from the global domain. If you set this, do not also pass a domain argument.
    #[clap(short = 'g', long = "globalDomain")]
    pub(crate) global_domain: bool,
    /// Defaults domain to write to.
    pub(crate) domain: String,
    /// Defaults key to write to.
    pub(crate) key: String,
    /**
    Value to write (as a yaml string).

    If you want to append to an existing array or dictionary, use `...` as an array value, or `...:...` as a dictionary entry, to represent the existing items in the array.
    If there are duplicates, the first entry will be preserved.

    So if the array contained `["a", "foo", "b", "bar", "c"]`, and you write `["foo", "...", "bar", "baz"]`, you would end up with `["foo", "a", "b", "bar", "c", "baz"]`

    Similarly if the dict contained `{"a": 1, "foo": 2, "b": 3, "bar": 4, "c": 5}`, and you write `{"foo": 6 "...":"...", "bar": 7, "baz": 8}`, you would end up with `{"a": 1, "foo": 6, "b": 3, "bar": 4, "c": 5, "baz": 8}`
    */
    pub(crate) value: Option<String>,
}

/// Options supported by the `up faketty` subcommand.
#[derive(Debug, Parser, Default, Clone)]
pub struct FakettyOptions {
    /// The program to run.
    #[clap(
        num_args(1..),
        value_parser(clap::builder::OsStringValueParser::new()),
        trailing_var_arg(true),
    )]
    pub(crate) program: Vec<OsString>,
}