use std::ffi::OsString;
use std::path::PathBuf;
use clap::builder::NonEmptyStringValueParser;
use clap::{Args, Parser, Subcommand};
use crumbkit::commands::{COMMANDS_LIST, CommandsList};
use crumbkit::help::{Effects, Example, Exit, Goal, Group, Guide, Leaf};
use crumbkit::schemas::{SCHEMAS_LIST, SCHEMAS_SHOW, SchemasList, SchemasShow};
use crumbkit::{Call, DryRun, NoInput, ViewFlags};
use crumbkit_usage::{SELF_USAGE, SelfUsage};
use starmap::Name;
use starmap_resolve::WorkspaceName;
use crate::check::CHECK;
use crate::diff::DIFF;
use crate::explain::EXPLAIN;
use crate::init::INIT;
#[derive(Debug, Parser)]
#[command(name = "starmap", version)]
pub struct Cli {
#[command(subcommand)]
pub(crate) verb: Verb,
#[command(flatten)]
pub(crate) view: ViewFlags,
#[command(flatten)]
pub(crate) no_input: NoInput,
}
pub const GUIDE: Guide = Guide::new()
.groups(&[Group::new(
&[],
&[
Goal::new("Write and check starmap.toml", &["init", "check"]),
Goal::new("Run a program with its environment", &["run"]),
Goal::new("Read the variables", &["explain", "diff"]),
Goal::new("Explore commands", &["commands", "schemas", "self"]),
],
)])
.root_verbs(
&["init", "check", "explain", "diff", "run"],
"starmap acts on one file, starmap.toml, so its commands need no resource word.",
)
.leaves(&[
Leaf::new(&["init"])
.examples(&[
Example::new(
&[],
"Write starmap.toml with a guess for each variable that src reads",
),
Example::new(&["--dry-run"], "Print the guesses and write nothing"),
])
.effects(Effects::additive())
.document(INIT),
Leaf::new(&["check"])
.examples(&[
Example::new(&[], "Check starmap.toml in the current directory"),
Example::new(&["--json"], "Print the result of the check as JSON"),
])
.effects(Effects::read_only())
.document(CHECK),
Leaf::new(&["explain"])
.examples(&[
Example::new(&[], "Explain every variable and its value in this shell"),
Example::new(&["SHELF_MIRROR_TOKEN"], "Explain one variable"),
Example::new(
&["SHELF_MIRROR_TOKEN", "--json"],
"Print one variable as JSON, where a withheld value has no text",
),
])
.effects(Effects::read_only())
.document(EXPLAIN),
Leaf::new(&["diff"])
.examples(&[Example::new(
&["main"],
"List each variable that changed since the spec on main",
)])
.effects(Effects::read_only())
.document(DIFF),
Leaf::new(&["run"])
.examples(&[
Example::new(
&["--", "cargo", "run"],
"Run the app with the environment that starmap.toml declares",
),
Example::new(
&["--set", "SHELF_DEBUG=true", "--", "cargo", "run"],
"Run the app with one setting changed for this run",
),
])
.effects(Effects::additive())
.passes_on_exit("the program after --"),
Leaf::new(&["commands", "list"])
.examples(&[Example::new(&[], "List every command")])
.effects(Effects::read_only())
.document(COMMANDS_LIST),
Leaf::new(&["schemas", "list"])
.examples(&[Example::new(&[], "List every document that starmap prints")])
.effects(Effects::read_only())
.document(SCHEMAS_LIST),
Leaf::new(&["schemas", "show"])
.examples(&[Example::new(
&["starmap/init/v1"],
"Print the JSON Schema of the document that init prints",
)])
.effects(Effects::read_only())
.document(SCHEMAS_SHOW),
Leaf::new(&["self", "usage"])
.examples(&[Example::new(&[], "Report the findings of the last 7 days")])
.effects(Effects::read_only())
.document(SELF_USAGE),
])
.exits(&[Exit::range(
3,
255,
"starmap run passes on the exit code of the program it runs. The program's exit codes 0, 1 and 2 pass on as well, and are then the same numbers as starmap's own success, failure and refused command line.",
)
.commands(&[&["run"]])]);
#[derive(Debug, Subcommand)]
pub(crate) enum Verb {
Init(InitArgs),
Check(CheckArgs),
Explain(ExplainArgs),
Diff(DiffArgs),
Run(RunArgs),
Commands {
#[command(subcommand)]
command: CommandsCommand,
},
Schemas {
#[command(subcommand)]
command: SchemasCommand,
},
#[command(name = "self")]
SelfCommand {
#[command(subcommand)]
command: SelfCommand,
},
}
#[derive(Debug, Subcommand)]
pub(crate) enum CommandsCommand {
List(CommandsList),
}
#[derive(Debug, Subcommand)]
pub(crate) enum SchemasCommand {
List(SchemasList),
Show(SchemasShow),
}
#[derive(Debug, Subcommand)]
pub(crate) enum SelfCommand {
Usage(SelfUsage),
}
#[derive(Debug, Args)]
pub(crate) struct SpecFile {
#[arg(long, value_name = "FILE", default_value = SpecFile::DEFAULT)]
pub(crate) spec: PathBuf,
}
impl SpecFile {
pub(crate) const DEFAULT: &str = "starmap.toml";
pub(crate) fn call(path: &'static [&'static str], file: &str) -> Call {
let call = Call::new(path);
if file == Self::DEFAULT {
call
} else {
call.value("spec", file)
}
}
}
#[derive(Debug, Args)]
pub(crate) struct InitArgs {
#[arg(long, value_name = "FILE", default_value = SpecFile::DEFAULT)]
pub(crate) spec: PathBuf,
#[command(flatten)]
pub(crate) dry_run: DryRun,
}
#[derive(Debug, Args)]
pub(crate) struct CheckArgs {
#[command(flatten)]
pub(crate) file: SpecFile,
}
#[derive(Debug, Args)]
pub(crate) struct ExplainArgs {
#[arg(value_name = "NAME")]
pub(crate) names: Vec<Name>,
#[command(flatten)]
pub(crate) file: SpecFile,
}
#[derive(Debug, Args)]
pub(crate) struct DiffArgs {
#[arg(value_name = "REVISION", value_parser = NonEmptyStringValueParser::new())]
pub(crate) revision: String,
#[command(flatten)]
pub(crate) file: SpecFile,
}
#[derive(Debug, Args)]
pub(crate) struct RunArgs {
#[command(flatten)]
pub(crate) file: SpecFile,
#[arg(long, value_name = "NAME")]
pub(crate) workspace: Option<WorkspaceName>,
#[arg(long, value_name = "NAME=VALUE", value_parser = parse_assignment)]
pub(crate) set: Vec<Assignment>,
#[arg(value_name = "PROGRAM", required = true, last = true)]
pub(crate) program: Vec<OsString>,
}
#[derive(Debug, Clone)]
pub(crate) struct Assignment {
pub(crate) name: Name,
pub(crate) value: String,
}
fn parse_assignment(text: &str) -> Result<Assignment, String> {
let (name, value) = text
.split_once('=')
.ok_or_else(|| "write the value as NAME=VALUE".to_owned())?;
let name = name.parse::<Name>().map_err(|error| error.to_string())?;
Ok(Assignment {
name,
value: value.to_owned(),
})
}