shep 0.25.1

The shep binary: a process manager that keeps a flock of long-running processes alive on macOS, Linux and Windows, with logs, watch and cron restarts, and webhook alerts
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! `shep set <address> <value>` and `shep unset <address>`: one write for any
//! settings in the flock, and the undo for it.
//!
//! The address is `copy`'s and `get`'s, and each kind is written through the
//! request the shepherd already answers: [`Request::SetSheepField`],
//! [`Request::SetSheepDogSettings`] or [`Request::SetDogConfig`], so everything
//! those refuse this refuses.
//!
//! The order is `copy`'s: resolve, read, plan, print the change list, ask,
//! write. Nothing is sent until the operator has seen every key it touches.

use std::collections::BTreeSet;
use std::fmt;

use serde_json::{Map, Value as Json};
use shep_client::Client;
use shep_core::config::DogTable;
use shep_core::paths::ShepPaths;
#[cfg(doc)]
use shep_core::protocol::Request;
use toml_edit::DocumentMut;

use self::plan::{field_changes, plan_dog_table, plan_section};
use self::schema::{dog_schema, known_dogs};
use self::value::value_of;
use crate::cli::SetArgs;
use crate::commands::copy;
use crate::commands::copy::CopyRefusal;
use crate::commands::copy::address::{self, ConfigPath, FlockNames};
use crate::commands::copy::plan::Change;
use crate::commands::settings_read;
use crate::exit::ExitCode;
use crate::lookout::probe_sheep_dogs::sheep_schema;
use crate::output::Streams;

#[cfg(test)]
mod defaults_routing_tests;
pub(crate) mod dog_wide;
#[cfg(all(test, unix))]
mod dog_wide_flow_tests;
mod edit;
mod flow;
#[cfg(test)]
mod flow_stale_tests;
#[cfg(test)]
mod inline_section_tests;
mod plan;
mod schema;
mod unset;
#[cfg(test)]
mod unset_plan_tests;
pub(crate) mod value;

use self::flow::run;
pub(crate) use self::schema::{asked_schema_serving, builtin_schema_serving, dog_standard};
pub(crate) use self::unset::unset;

/// Which of the two verbs this run is, for the words it prints.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Verb {
    /// `shep set`.
    Set,
    /// `shep unset`.
    Unset,
}

impl Verb {
    /// The verb as typed, which is also the `command` its envelope carries.
    const fn as_str(self) -> &'static str {
        match self {
            Self::Set => "set",
            Self::Unset => "unset",
        }
    }
}

/// The verb and the two flags a run was given.
#[derive(Debug, Clone, Copy)]
struct Flags {
    verb: Verb,
    /// `--yes`: write without asking.
    yes: bool,
    /// `--dry-run`: print the changes and write nothing.
    dry_run: bool,
}

/// Why a write stopped before it wrote anything.
///
/// Every variant carries the sentence the operator reads, because the reason
/// is the remedy: an unknown name names `shep kv set`, `env` names `shep
/// import env`, and a schema refusal names the key path and the rule it
/// breaks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum WriteRefusal {
    /// An address or a value is malformed for the path it was aimed at.
    Usage(String),
    /// No sheep and no dog in this flock answers that name.
    Unknown(String),
    /// A key this verb never writes, in the reason the shepherd gives for it.
    Refused(String),
    /// The dog's own schema turned the value down.
    Invalid(String),
    /// The dog itself refused the write, in its own words.
    ByDog(String),
    /// The holder moved between the read the plan was built from and the write.
    Stale(String),
}

impl WriteRefusal {
    /// The sentence the operator reads.
    fn message(&self) -> &str {
        match self {
            Self::Usage(message)
            | Self::Unknown(message)
            | Self::Refused(message)
            | Self::Invalid(message)
            | Self::ByDog(message)
            | Self::Stale(message) => message,
        }
    }

    /// The code that reports it.
    ///
    /// [`ExitCode::InvalidConfig`] for a refused key and for a schema refusal
    /// is the code the shepherd's own refusal of the same write carries.
    ///
    /// A holder that moved is [`ExitCode::Failure`]: nothing is invalid, only
    /// that the two are no longer the pair this plan described.
    const fn code(&self) -> ExitCode {
        match self {
            Self::Usage(_) => ExitCode::Usage,
            Self::Unknown(_) => ExitCode::NotFound,
            Self::Refused(_) | Self::Invalid(_) | Self::ByDog(_) => ExitCode::InvalidConfig,
            Self::Stale(_) => ExitCode::Failure,
        }
    }

    /// Writes the refusal and hands back the code it wrote.
    fn report(&self, streams: &mut Streams<'_>) -> ExitCode {
        streams.fail(self.code(), self.message())
    }
}

impl fmt::Display for WriteRefusal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.message())
    }
}

impl core::error::Error for WriteRefusal {}

/// The request a plan turns into.
///
/// One per address shape, which is one per request the shepherd already
/// answers. A dog's section is the whole text rather than one key:
/// [`Request::SetDogConfig`] replaces the section, and the text is the one
/// this verb read, edited in place, so the comments the operator wrote inside
/// it survive the write.
#[derive(Debug, Clone)]
pub(super) enum Write {
    /// One `AppConfig` field, through [`Request::SetSheepField`].
    SheepField {
        /// The field name.
        key: String,
        /// The new value.
        value: Json,
    },
    /// One dog's whole table on one sheep, through
    /// [`Request::SetSheepDogSettings`].
    ///
    /// The whole table and not the one key: that request replaces a dog's
    /// table, so the merged result is what carries the keys this write left
    /// alone.
    SheepDogTable {
        /// The dog's name.
        dog: String,
        /// The table the sheep holds afterwards.
        table: DogTable,
        /// The table the sheep held when this plan was built, which is what
        /// [`flow::holder_unchanged`] checks the sheep still holds.
        before: Json,
    },
    /// One dog's whole table on one sheep, removed through
    /// [`Request::SetSheepDogSettings`] with no table.
    DropSheepDogTable {
        /// The dog's name.
        dog: String,
        /// The table the sheep held when this plan was built, which is what
        /// [`flow::holder_unchanged`] checks the sheep still holds.
        before: Json,
    },
    /// One `AppConfig` field's override, dropped through
    /// [`Request::ResetSheepField`].
    ResetSheepField {
        /// The field name.
        key: String,
    },
    /// One dog's whole section, through [`Request::SetDogConfig`].
    DogSection {
        /// The section text, edited in place from the text the shepherd
        /// answered.
        toml: String,
        /// The text the shepherd answered, which is what [`flow::holder_unchanged`]
        /// checks it still answers.
        before: String,
    },
}

/// What one `shep set` would write: the keys it changes, the one request that
/// writes them, and what the value has to pass before any of it is sent.
#[derive(Debug)]
pub(super) struct Plan {
    /// One entry per key the write touches, in dotted-key order.
    changes: Vec<Change>,
    write: Write,
    /// The table the write leaves behind, as JSON: the dog's table on the
    /// sheep, or the whole section. Empty for a sheep's own field, which is
    /// one value and not a table, and for a dog's table removed whole, which
    /// leaves nothing.
    table: Map<String, Json>,
    /// The schema `table` is checked against, `None` when no dog published one
    /// for it.
    schema: Option<Json>,
    /// The property paths this run chose, under `table`. Empty says the write
    /// replaces the table whole, so every key in it is in play.
    touched: BTreeSet<Vec<String>>,
}

/// What one run carries to whichever holder the address names: the operator's
/// address and value, and the flock and the files the address was resolved
/// against.
pub(super) struct Ask<'a> {
    /// The flags the operator typed: `--yes`, `--dry-run`.
    flags: Flags,
    /// The value, already read as TOML.
    value: Json,
    /// The names the running flock carries.
    flock: &'a FlockNames,
    /// `$SHEP_HOME`, for a dog's schema and `shep.toml`'s dog list.
    paths: &'a ShepPaths,
}

/// `shep set <address> <value>`.
///
/// # Errors
/// The [`ExitCode`] already reported to `streams`: a malformed address, a name
/// no sheep or dog answers, a key this verb never writes, a value the dog's
/// schema turns down, a refusal to write with no terminal to ask on, or a
/// write the shepherd refused.
pub(crate) async fn set(
    client: &Client,
    streams: &mut Streams<'_>,
    paths: &ShepPaths,
    args: &SetArgs,
) -> ExitCode {
    let value = match value_of(&args.value) {
        Ok(value) => value,
        Err(refusal) => return refusal.report(streams),
    };
    let flock = match copy::flock_names(client, streams).await {
        Ok(names) => names,
        Err(code) => return code,
    };
    let segments = match settings_read::segments(&args.address) {
        Ok(segments) => segments,
        Err(refusal) => return refusal.report(streams),
    };
    let ask = Ask {
        flags: Flags {
            verb: Verb::Set,
            yes: args.yes,
            dry_run: args.dry_run,
        },
        value,
        flock: &flock,
        paths,
    };
    // Before any sheep name: a sheep named like a dog does not take the dog's
    // retired defaults address.
    if let Some(defaults) = defaults_in(&segments, &flock, paths) {
        return WriteRefusal::Refused(address::retired_defaults(&defaults)).report(streams);
    }
    if let Some(name) = address::longest_in(&segments, &flock.sheep) {
        return set_sheep(client, streams, &name, &segments, &ask).await;
    }
    set_dog(client, streams, &segments, &ask).await
}

/// One sheep's field, or one key inside one of its dog tables.
async fn set_sheep(
    client: &Client,
    streams: &mut Streams<'_>,
    name: &str,
    segments: &[String],
    ask: &Ask<'_>,
) -> ExitCode {
    let config = match copy::read_config(client, streams, name).await {
        Ok(config) => config,
        Err(code) => return code,
    };
    let path = match path_in(name, segments, ask.flock, &config) {
        Ok(path) => path,
        Err(refusal) => return refusal.report(streams),
    };
    let plan = match &path {
        ConfigPath::Field { name: field } => Plan {
            changes: field_changes(field, &config, &ask.value),
            write: Write::SheepField {
                key: field.clone(),
                value: ask.value.clone(),
            },
            table: Map::new(),
            schema: None,
            touched: BTreeSet::new(),
        },
        ConfigPath::Env => {
            return CopyRefusal::Refused(address::ENV_NOT_WRITTEN.to_string()).report(streams);
        }
        ConfigPath::Dog { dog, keys } => {
            let standard = dog_standard(dog, ask.paths);
            if let Some((_, standard)) = &standard
                && let Some(refusal) =
                    dog_wide::dog_wide_refusal(standard, name, dog, keys, &ask.value)
            {
                return refusal.report(streams);
            }
            let plan = match plan_dog_table(name, dog, keys, &config, &ask.value) {
                Ok(plan) => plan,
                Err(refusal) => return refusal.report(streams),
            };
            // The schema lookout builds this table's rows from: the dog's one
            // schema without its dog-wide keys for a dog that follows the
            // standard, the per-sheep half of two schemas otherwise.
            let schema = match standard {
                Some((root, standard)) => Some(standard.sheep_form(&root)),
                None => dog_schema(dog, ask.paths).and_then(|root| sheep_schema(&root)),
            };
            Plan { schema, ..plan }
        }
    };
    run(
        client,
        streams,
        ask.paths,
        name,
        &path.dotted(),
        &plan,
        ask.flags,
    )
    .await
}

/// Where in sheep `name`'s settings the address points: one of its fields, or
/// a key inside one of its dog tables.
///
/// # Errors
/// The refusal [`address::path_of`] gives for a path no write reaches.
fn path_in(
    name: &str,
    segments: &[String],
    flock: &FlockNames,
    config: &Json,
) -> Result<ConfigPath, CopyRefusal> {
    let depth = name.split('.').count();
    let written = address::Written {
        sheep: name.to_string(),
        segments: segments[depth..].to_vec(),
    };
    // The same sheep on both sides of `path_of`: `copy` resolves a dotted dog
    // name against the two tables it is copying between, and this verb has the
    // one it is writing.
    let dogs = address::DogNames::new(flock, config, &Json::Null);
    address::path_of(&written, &dogs, config, config)
}

/// The dog an address names, and the section of `dogs.toml` it holds now.
pub(super) struct DogTarget {
    /// The dog's name, dotted where the dog is.
    name: String,
    /// The section text the shepherd answered, `None` for a dog the flock runs
    /// with no section yet.
    section: Option<String>,
}

impl DogTarget {
    /// The section as a document to edit in place, empty for a dog with no
    /// section.
    ///
    /// # Errors
    /// The [`ExitCode`] already reported to `streams`, for text that is not
    /// TOML.
    fn document(&self, streams: &mut Streams<'_>) -> Result<DocumentMut, ExitCode> {
        let Some(section) = &self.section else {
            return Ok(DocumentMut::new());
        };
        section
            .parse::<DocumentMut>()
            .map_err(|_| streams.fail(ExitCode::InvalidConfig, &unparsable(&self.name)))
    }

    /// The segments of the address after the dog's name.
    fn rest<'a>(&self, segments: &'a [String]) -> &'a [String] {
        &segments[self.name.split('.').count()..]
    }
}

/// One dog's own section of `dogs.toml`.
async fn set_dog(
    client: &Client,
    streams: &mut Streams<'_>,
    segments: &[String],
    ask: &Ask<'_>,
) -> ExitCode {
    let target = match resolve_dog(client, streams, segments, ask.flock, ask.paths).await {
        Ok(target) => target,
        Err(code) => return code,
    };
    let doc = match target.document(streams) {
        Ok(doc) => doc,
        Err(code) => return code,
    };
    let rest = target.rest(segments);
    let keys = settings_read::dog_steps(doc.as_table(), rest);
    match plan_section(doc, &target.name, &keys, &ask.value) {
        Ok(plan) => {
            let plan = Plan {
                schema: dog_schema(&target.name, ask.paths),
                ..plan
            };
            run(
                client,
                streams,
                ask.paths,
                &target.name,
                &rest.join("."),
                &plan,
                ask.flags,
            )
            .await
        }
        Err(refusal) => refusal.report(streams),
    }
}

/// The retired defaults address `segments` spell, if they spell one.
///
/// Read off the dogs the flock runs and the ones the shepherd knows, before
/// any sheep name or dog's own section is walked. `pub(crate)`: every verb
/// that takes an address refuses it. An address with an empty segment spells
/// none, so each verb reports it as the malformed address it is.
pub(crate) fn defaults_in(
    segments: &[String],
    flock: &FlockNames,
    paths: &ShepPaths,
) -> Option<address::DefaultsAddress> {
    if segments.iter().any(String::is_empty) {
        return None;
    }
    let mut dogs = known_dogs(paths);
    dogs.extend(flock.dogs.iter().cloned());
    address::defaults_of(segments, &dogs)
}

/// The dog an address names.
///
/// The name is the longest run of segments the shepherd answers a section for,
/// which is what tells `kelpie` and `kelpie.worker` apart, as [`shep get`](settings_read::get) applies it.
///
/// A dog the flock runs with no section yet is the one case with nothing to
/// read, and a write creates it.
///
/// A section is not what makes a dog known: the candidate has to be a name the
/// daemon will take a `Request::SetDogConfig` for.
///
/// # Errors
/// The [`ExitCode`] already reported to `streams`.
pub(super) async fn resolve_dog(
    client: &Client,
    streams: &mut Streams<'_>,
    segments: &[String],
    flock: &FlockNames,
    paths: &ShepPaths,
) -> Result<DogTarget, ExitCode> {
    let mut named: Option<String> = None;
    let mut sectioned: Option<String> = None;
    let known = known_dogs(paths);
    for name in settings_read::dog_candidates(segments) {
        let section = settings_read::read_dog_section(client, streams, &name).await?;
        let holds = flock.dogs.contains(&name) || known.contains(&name);
        if holds && named.is_none() {
            named = Some(name.clone());
        }
        if section.trim().is_empty() {
            continue;
        }
        sectioned = Some(name.clone());
        if !holds {
            // A shorter candidate may still be a dog this shepherd knows: a dog
            // adopted under the dotted name `kelpie.worker` is known while
            // `kelpie` is not.
            continue;
        }
        return Ok(DogTarget {
            name,
            section: Some(section),
        });
    }
    match (named, sectioned) {
        (Some(name), _) => Ok(DogTarget {
            name,
            section: None,
        }),
        (None, Some(name)) => Err(WriteRefusal::Unknown(no_known_dog(&name)).report(streams)),
        (None, None) => Err(WriteRefusal::Unknown(no_such_name(segments)).report(streams)),
    }
}

/// The sentence for a value that parses as TOML and has no place in the JSON the
/// shepherd carries.
///
/// `path` is where inside the value it sits, empty for the value itself.
pub(super) fn not_a_number(path: &str) -> String {
    let under = if path.is_empty() {
        String::new()
    } else {
        format!(" under `{path}`")
    };
    format!(
        "not a value `shep set` writes: TOML's `nan` and `inf`{under} have no place in the JSON \
         the shepherd carries, and the flock would be sent a null. Quote the text (`\"nan\"`) to \
         write it as a string"
    )
}
/// The sentence for a name no sheep is and no dog has a section for, which is
/// where the key/value store `shep set` used to write went.
pub(super) fn no_such_name(segments: &[String]) -> String {
    format!(
        "no sheep or dog named `{}`: `shep flock` lists the flock and `shep dogs` lists the dogs. \
         The key/value store `shep set` used to write lives at `shep kv set`",
        segments.join(".")
    )
}

/// The sentence for a `dogs.toml` section whose name this shepherd knows no dog
/// by.
///
/// `shep disable` and a hand edit both leave a section behind, and the daemon
/// guards [`Request::SetDogConfig`] on its own known set for the same reason
/// this refusal exists: writing the section of a dog nothing runs configures
/// nothing. The remedy is the daemon's own, so the sentence is too.
pub(super) fn no_known_dog(name: &str) -> String {
    format!(
        "nothing was written: `dogs.toml` has a `[{name}]` section, but no dog this shepherd knows \
         is named `{name}`. `shep adopt` or `shep enable` makes one known, and `shep dogs` lists \
         what is"
    )
}

/// The sentence for a `dogs.toml` section this verb cannot read as TOML.
///
/// The file is named rather than the settings, because the file is the thing
/// that is broken: the same text [`settings_read::get`] prints is what does not
/// parse, and `shep get` is how the operator sees it.
pub(super) fn unparsable(name: &str) -> String {
    format!(
        "`{name}`'s section of `dogs.toml` does not parse: nothing was written, and `shep get \
         {name}` reads the same text"
    )
}
#[cfg(test)]
mod tests;
#[cfg(test)]
mod value_tests;