1use std::io::{self};
2use std::path::PathBuf;
3
4use clap::builder::styling;
5use clap::{
6 ColorChoice, CommandFactory, FromArgMatches, Parser, Subcommand, ValueHint
7};
8use clap_complete::{Shell, generate};
9use colored::Colorize;
10use zoi_common::Runnable;
11
12use crate::pkg::lock;
13use crate::{cmd, utils};
14
15const BRANCH: &str = "Production";
18const STATUS: &str = "Release";
20const NUMBER: &str = "1.27.1";
22const PKG_SOURCE_HELP: &str =
24 "Package identifier (e.g. @repo/name, #git@repo/name, path, or URL)";
25
26#[derive(Parser)]
31#[command(name = "zoi", author, about, long_about = None, disable_version_flag = true,
32 trailing_var_arg = true,
33 color = ColorChoice::Auto,
34 arg_required_else_help = true,
35)]
36pub struct Cli {
37 #[command(subcommand)]
39 command: Option<Commands>,
40
41 #[arg(
43 short = 'v',
44 long = "version",
45 help = "Print detailed version information"
46 )]
47 version_flag: bool,
48
49 #[arg(
51 short = 'y',
52 long,
53 help = "Automatically answer yes to all prompts",
54 global = true
55 )]
56 yes: bool,
57
58 #[arg(
60 long = "root",
61 help = "Operate on a different root directory",
62 global = true,
63 value_hint = ValueHint::DirPath
64 )]
65 pub root: Option<std::path::PathBuf>,
66
67 #[arg(
69 long = "offline",
70 help = "Do not attempt to connect to the network",
71 global = true
72 )]
73 pub offline: bool,
74
75 #[arg(
77 long = "pkg-dir",
78 help = "Additional directory to search for .zpa archives",
79 global = true,
80 value_hint = ValueHint::DirPath
81 )]
82 pub pkg_dirs: Vec<std::path::PathBuf>
84}
85
86#[derive(clap::ValueEnum, Clone, Debug, Copy, PartialEq, Eq)]
88pub enum SetupScope {
89 User,
91 System
93}
94
95#[derive(clap::ValueEnum, Clone, Debug, Copy)]
97pub enum InstallScope {
98 User,
100 System,
102 Project
104}
105
106#[derive(Subcommand)]
108enum Commands {
109 #[command(hide = true)]
111 GenerateCompletions {
112 #[arg(value_enum)]
114 shell: Shell
115 },
116
117 #[command(hide = true)]
119 Complete {
120 #[arg(value_enum)]
122 shell: Shell,
123 index: usize,
125 words: Vec<String>
127 },
128
129 #[command(
131 alias = "v",
132 long_about = "Displays the version number, build status, branch, and \
133 commit hash. This is the same output provided by the -v \
134 and --version flags."
135 )]
136 Version,
137
138 #[command(long_about = "Displays the full application name, description, \
140 author, license, and homepage information.")]
141 About,
142
143 #[command(long_about = "Detects and displays key system details, \
145 including the OS, CPU architecture, Linux \
146 distribution (if applicable), and available \
147 package managers.")]
148 Info,
149
150 #[command(
152 alias = "dl",
153 long_about = "Downloads the binary archive (.zpa) or source bundle \
154 (.zsa) for a package to the local cache or a specified \
155 directory."
156 )]
157 Download {
158 #[arg(value_name = "PACKAGE", required = true, help = PKG_SOURCE_HELP)]
160 package: String,
161
162 #[arg(long, group = "type")]
164 archive: bool,
165
166 #[arg(long, group = "type")]
168 source: bool,
169
170 #[arg(short, long)]
172 output_dir: Option<PathBuf>
173 },
174
175 #[command(
177 alias = "sy",
178 long_about = "Clones the official package database from GitLab to \
179 Zoi's user data directory. If the database already \
180 exists, it verifies the remote URL and pulls the latest \
181 changes."
182 )]
183 Sync {
184 #[command(subcommand)]
186 command: Option<SyncCommands>,
187
188 #[arg(short, long)]
190 verbose: bool,
191
192 #[arg(long)]
194 fallback: bool,
195
196 #[arg(long = "no-pm")]
198 no_package_managers: bool,
199
200 #[arg(long)]
203 force: bool,
204
205 #[arg(long)]
208 local: bool,
209
210 #[arg(long)]
213 frozen: bool,
214
215 #[arg(long, value_enum, conflicts_with = "local")]
217 scope: Option<SetupScope>
218 },
219
220 Migrate(cmd::migrate::MigrateCommand),
222
223 #[command(alias = "ls")]
225 List {
226 #[arg(short, long)]
228 all: bool,
229 #[arg(short, long)]
231 outdated: bool,
232 #[arg(long)]
234 registry: Option<String>,
235 #[arg(long)]
237 repo: Option<String>,
238 #[arg(short = 't', long = "type")]
240 package_type: Option<String>,
241 #[arg(short = 'm', long)]
243 foreign: bool,
244 #[arg(long, hide = true)]
246 names: bool,
247 #[arg(long, hide = true)]
249 completion: bool
250 },
251
252 Show {
254 #[arg(value_name = "ALL_PACKAGES", help = PKG_SOURCE_HELP)]
256 package_name: String,
257 #[arg(long)]
259 raw: bool,
260 #[arg(long)]
262 purl: bool
263 },
264
265 Pin {
267 #[arg(value_name = "INST_PACKAGES", help = PKG_SOURCE_HELP)]
269 package: String,
270 version: String
272 },
273
274 Provides {
276 term: String
278 },
279
280 Tree {
282 #[arg(value_name = "ALL_PACKAGES", required = true, help = PKG_SOURCE_HELP)]
284 packages: Vec<String>
285 },
286
287 Unpin {
289 #[arg(value_name = "INST_PACKAGES", help = PKG_SOURCE_HELP)]
291 package: String
292 },
293
294 #[command(
296 alias = "m",
297 long_about = "Changes whether a package is considered explicitly installed or a dependency. Explicit packages are not removed by 'autoremove', while dependencies are if no other package requires them.",
298 group(clap::ArgGroup::new("mode").required(true).args(["as_dependency", "as_explicit"]))
299 )]
300 Mark {
301 #[arg(value_name = "INST_PACKAGES", required = true, help = PKG_SOURCE_HELP)]
303 packages: Vec<String>,
304
305 #[arg(long, aliases = ["asdeps"])]
307 as_dependency: bool,
308
309 #[arg(long, aliases = ["asexpl"], conflicts_with = "as_dependency")]
311 as_explicit: bool
312 },
313
314 #[command(
316 alias = "up",
317 arg_required_else_help = true,
318 group(clap::ArgGroup::new("target").required(true).args(["package_names", "all"]))
319 )]
320 Update {
321 #[arg(value_name = "INST_PACKAGES", help = PKG_SOURCE_HELP)]
323 package_names: Vec<String>,
324
325 #[arg(long, conflicts_with = "package_names")]
327 all: bool,
328
329 #[arg(long)]
331 dry_run: bool,
332 #[arg(long)]
334 explain: bool,
335 #[arg(long, requires = "dry_run")]
337 plan_json: bool,
338 #[arg(long, short)]
340 verbose: bool,
341 #[arg(long, requires = "all")]
344 interactive: bool
345 },
346
347 #[command(aliases = ["i", "in", "add"])]
350 Install(cmd::install::args::InstallArgs),
351
352 #[command(alias = "u")]
354 Use {
355 #[arg(value_name = "ALL_PACKAGES", required = true, help = PKG_SOURCE_HELP)]
357 packages: Vec<String>,
358
359 #[arg(short, long)]
361 global: bool
362 },
363
364 #[command(
366 aliases = ["un", "rm", "remove"],
367 long_about = "Removes one or more packages' files from the Zoi store and deletes their symlinks from the bin directory. This command will fail if a package was not installed by Zoi."
368 )]
369 Uninstall(cmd::uninstall::args::UninstallArgs),
370
371 #[command(long_about = "Execute a command from zoi.yaml. If no command \
373 is specified, it will launch an interactive \
374 prompt to choose one.")]
375 Run {
376 cmd_alias: Option<String>,
378 args: Vec<String>
380 },
381
382 #[command(long_about = "Checks for required packages and runs setup \
384 commands for a defined environment. If no \
385 environment is specified, it launches an \
386 interactive prompt.")]
387 Env {
388 env_alias: Option<String>,
390
391 #[arg(long, value_enum, hide = true)]
393 export_shell: Option<Shell>
394 },
395
396 #[command(
398 alias = "develop",
399 long_about = "Loads the project configuration from zoi.yaml, ensures \
400 all required packages are installed locally, sets up \
401 environment variables (PATH, LD_LIBRARY_PATH, etc.), \
402 and drops you into a subshell."
403 )]
404 Dev {
405 #[arg(short, long)]
407 run: Option<String>,
408 #[arg(long)]
410 repo: Option<String>
411 },
412
413 #[command(
415 alias = "ug",
416 long_about = "Upgrades Zoi to the latest version. By default, it \
417 attempts a delta upgrade (bsdiff) to minimize download \
418 size. If the delta upgrade is unavailable or fails, it \
419 automatically falls back to a full download."
420 )]
421 Upgrade {
422 #[arg(long)]
424 force: bool,
425
426 #[arg(long)]
428 tag: Option<String>,
429
430 #[arg(long)]
432 branch: Option<String>
433 },
434
435 Autoremove {
438 #[arg(long)]
440 dry_run: bool
441 },
442
443 Why {
445 #[arg(value_name = "INST_PACKAGES", help = PKG_SOURCE_HELP)]
447 package_name: String
448 },
449
450 #[command(alias = "owns")]
452 Owner {
453 #[arg(value_hint = ValueHint::FilePath)]
455 path: std::path::PathBuf
456 },
457
458 Files {
460 #[arg(value_name = "INST_PACKAGES", help = PKG_SOURCE_HELP)]
462 package: String
463 },
464
465 History {
467 #[arg(long, conflicts_with = "export")]
470 verify: bool,
471 #[arg(long, value_hint = ValueHint::FilePath, conflicts_with = "verify")]
474 export: Option<std::path::PathBuf>,
475 #[arg(long, requires = "export")]
477 ndjson: bool
478 },
479
480 #[command(
482 alias = "s",
483 long_about = "Searches for a case-insensitive term in the name, \
484 description, and tags of all available packages in the \
485 database. Filter by repo, type, or tags."
486 )]
487 Search {
488 search_term: String,
490 #[arg(long)]
492 registry: Option<String>,
493 #[arg(long)]
495 repo: Option<String>,
496 #[arg(long = "type")]
498 package_type: Option<String>,
499 #[arg(short = 't', long = "tag", value_delimiter = ',', num_args = 1..)]
501 tags: Option<Vec<String>>,
502 #[arg(long, default_value = "name")]
504 sort: String,
505 #[arg(short, long)]
507 files: bool,
508 #[arg(short = 'i', long)]
510 interactive: bool
511 },
512
513 #[command(alias = "svc")]
515 Service(cmd::service::ServiceCommand),
516
517 #[command(
520 long_about = "If a shell is provided, it installs completion scripts. If 'hook' is provided, it outputs shell-specific hook scripts for auto-activation. If packages are provided via --package/-p, it enters a temporary subshell with those packages available in PATH.",
521 arg_required_else_help = true,
522 group(clap::ArgGroup::new("shell_action").required(true).args(["shell", "hook", "packages"]).multiple(true))
523 )]
524 Shell {
525 #[arg(value_enum)]
527 shell: Option<Shell>,
528 #[arg(long)]
530 hook: bool,
531 #[arg(long, value_enum, default_value = "user")]
533 scope: SetupScope,
534 #[arg(short, long = "package", value_name = "ALL_PACKAGES")]
536 packages: Vec<String>,
537 #[arg(short, long)]
540 run: Option<String>,
541 #[arg(long, short)]
543 verbose: bool
544 },
545
546 #[command(
548 alias = "x",
549 long_about = "Resolves a package and its dependencies, installs them \
550 if needed, then runs the requested binary directly. By \
551 default runs the first binary the package provides. \
552 Uses bwrap for sandboxed packages."
553 )]
554 Exec {
555 #[arg(value_name = "ALL_SOURCES", help = PKG_SOURCE_HELP)]
557 source: String,
558
559 #[arg(long)]
562 bin: Option<String>,
563
564 #[arg(long, short)]
566 verbose: bool,
567
568 #[arg(value_name = "ARGS")]
570 args: Vec<String>
571 },
572
573 Clone {
575 #[arg(value_name = "ALL_PACKAGES", required = true, help = PKG_SOURCE_HELP)]
577 package: String,
578 #[arg(value_name = "LOCATION")]
580 location: Option<String>
581 },
582
583 Cache {
585 #[command(subcommand)]
587 command: CacheCommands
588 },
589
590 #[command(alias = "tx")]
592 Transaction {
593 #[command(subcommand)]
595 command: TransactionCommands
596 },
597
598 #[command(alias = "reg")]
600 Registry(cmd::registry::RegistryCommand),
601
602 Home(cmd::home::HomeCommand),
604
605 System(cmd::system::SystemCommand),
607
608 #[command(
610 aliases = ["repositories"],
611 long_about = "Manages the list of package repositories used by Zoi.\n\nCommands:\n- add (alias: a): Add an official repo by name or clone from a git URL.\n- remove|rm: Remove a repo from active list (repo rm <name>).\n- list|ls: Show active repositories by default; use 'list all' to show all available repositories.\n- git: Manage cloned git repositories (git ls, git rm <repo-name>)."
612 )]
613 Repo(cmd::repo::RepoCommand),
614
615 #[command(long_about = "Manage opt-in anonymous telemetry used to \
617 understand package popularity. Default is \
618 disabled.")]
619 Telemetry {
620 #[arg(value_enum)]
622 action: TelemetryAction
623 },
624
625 Create {
627 #[arg(value_name = "ALL_SOURCES", help = PKG_SOURCE_HELP)]
629 source: String,
630 app_name: Option<String>
632 },
633
634 #[command(
636 alias = "dg",
637 long_about = "Interactively choose and install an older version of a \
638 package from the local store or archive cache. This is \
639 useful if a recent update has introduced bugs or \
640 compatibility issues."
641 )]
642 Downgrade {
643 #[arg(value_name = "INST_PACKAGES", help = PKG_SOURCE_HELP)]
645 package: String
646 },
647
648 #[command(alias = "ext")]
650 Extension(ExtensionCommand),
651
652 Rollback {
654 #[arg(value_name = "INST_PACKAGES", help = PKG_SOURCE_HELP)]
656 package: String
657 },
658
659 Man {
661 #[arg(value_name = "ALL_PACKAGES", help = PKG_SOURCE_HELP)]
663 package_name: String,
664 #[arg(long)]
666 upstream: bool,
667 #[arg(long)]
669 raw: bool,
670 #[arg(long)]
673 page: Option<String>
674 },
675
676 #[command(alias = "pkg")]
678 Package(cmd::package::PackageCommand),
679
680 Pgp(cmd::pgp::PgpCommand),
682
683 Helper(cmd::helper::HelperCommand),
685
686 Doctor,
688
689 Audit {
691 #[arg(short, long)]
694 all: bool,
695 #[arg(long)]
697 registry: Option<String>,
698 #[arg(long)]
700 repo: Option<String>
701 },
702
703 #[command(external_subcommand)]
705 External(Vec<String>)
706}
707
708#[derive(clap::Parser, Debug)]
710pub struct ExtensionCommand {
711 #[command(subcommand)]
713 pub command: ExtensionCommands
714}
715
716#[derive(clap::Subcommand, Debug)]
718pub enum ExtensionCommands {
719 Add {
721 #[arg(required = true)]
723 name: String
724 },
725 Remove {
727 #[arg(required = true)]
729 name: String
730 }
731}
732
733#[derive(clap::Subcommand, Clone)]
735pub enum SyncCommands {
736 Add {
738 url: String
740 },
741 Remove {
743 handle: String
745 },
746 #[command(alias = "ls")]
748 List,
749 Set {
751 url: String
753 }
754}
755
756#[derive(clap::Subcommand)]
758pub enum CacheCommands {
759 Add {
761 #[arg(required = true)]
763 files: Vec<std::path::PathBuf>
764 },
765 #[command(alias = "clean")]
767 Clear {
768 #[arg(long)]
770 dry_run: bool
771 },
772 #[command(alias = "ls")]
774 List,
775 Mirror {
777 #[command(subcommand)]
779 command: CacheMirrorCommands
780 }
781}
782
783#[derive(clap::Subcommand)]
785pub enum CacheMirrorCommands {
786 Add {
788 url: String
790 },
791 Remove {
793 url: String
795 },
796 #[command(alias = "ls")]
798 List
799}
800
801#[derive(clap::Subcommand)]
803pub enum TransactionCommands {
804 #[command(alias = "ls")]
806 List,
807 Show {
809 id: String
811 },
812 Files {
814 id: String
816 },
817 #[command(alias = "rollback")]
820 Undo {
821 id: Option<String>,
823
824 #[arg(long)]
826 yes: bool
827 }
828}
829
830#[derive(clap::ValueEnum, Clone)]
832enum TelemetryAction {
833 Status,
835 Enable,
837 Disable
839}
840
841pub fn run() -> anyhow::Result<()> {
848 let styles = styling::Styles::styled()
849 .header(
850 styling::AnsiColor::Yellow.on_default() | styling::Effects::BOLD
851 )
852 .usage(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
853 .literal(styling::AnsiColor::Green.on_default())
854 .placeholder(styling::AnsiColor::Cyan.on_default());
855
856 let commit: &str = option_env!("ZOI_COMMIT_HASH").unwrap_or("dev");
857 let cmd = Cli::command().styles(styles.clone());
858 let matches = cmd.clone().get_matches();
859 let cli = match Cli::from_arg_matches(&matches) {
860 Ok(cli) => cli,
861 Err(err) => {
862 err.print()?;
863 return Err(anyhow::anyhow!("Failed to parse arguments"));
864 }
865 };
866
867 if let Some(root) = cli.root {
868 crate::pkg::sysroot::set_sysroot(root);
869 }
870
871 let config = crate::pkg::config::read_config().unwrap_or_default();
872
873 let is_offline = cli.offline || config.offline_mode;
874 crate::pkg::offline::set_offline(is_offline);
875
876 let mut all_pkg_dirs = cli.pkg_dirs;
877 for dir in config.pkg_dirs {
878 let path = std::path::PathBuf::from(dir);
879 if !all_pkg_dirs.contains(&path) {
880 all_pkg_dirs.push(path);
881 }
882 }
883 crate::pkg::pkgdir::set_pkg_dirs(all_pkg_dirs);
884
885 utils::check_path();
886
887 if let Err(e) = crate::pkg::pgp::ensure_builtin_keys() {
888 eprintln!(
889 "{}: Failed to ensure builtin PGP keys: {}",
890 "Warning".yellow(),
891 e
892 );
893 }
894
895 let plugin_manager = crate::pkg::plugin::PluginManager::new()?;
896 if let Err(e) = plugin_manager.load_all(cli.yes) {
897 eprintln!("{}: Failed to load plugins: {}", "Warning".yellow(), e);
898 }
899
900 if cli.version_flag {
901 cmd::version::run(BRANCH, STATUS, NUMBER, commit);
902 return Ok(());
903 }
904
905 if let Some(command) = cli.command {
906 let needs_lock = matches!(
907 command,
908 Commands::Install { .. }
909 | Commands::Uninstall { .. }
910 | Commands::Update { .. }
911 | Commands::Autoremove { .. }
912 | Commands::Rollback { .. }
913 | Commands::Package(_)
914 );
915
916 let _lock_guard = if needs_lock {
917 Some(lock::acquire_lock()?)
918 } else {
919 None
920 };
921
922 let result = match command {
923 Commands::GenerateCompletions { shell } => {
924 let mut cmd = Cli::command();
925 let bin_name = cmd.get_name().to_string();
926 generate(shell, &mut cmd, bin_name, &mut io::stdout());
927 Ok(())
928 }
929 Commands::Complete {
930 shell,
931 index,
932 words
933 } => cmd::complete::run(shell, index, &words),
934 Commands::Version => {
935 cmd::version::run(BRANCH, STATUS, NUMBER, commit);
936 Ok(())
937 }
938 Commands::About => {
939 cmd::about::run(BRANCH, STATUS, NUMBER, commit);
940 Ok(())
941 }
942 Commands::Info => cmd::info::run(BRANCH, STATUS, NUMBER, commit),
943 Commands::Sync {
944 command,
945 verbose,
946 fallback,
947 no_package_managers,
948 force,
949 local,
950 frozen,
951 scope
952 } => {
953 if let Some(cmd) = command {
954 match cmd {
955 SyncCommands::Add { url } => {
956 cmd::sync::add_registry(&url)
957 }
958 SyncCommands::Remove { handle } => {
959 cmd::sync::remove_registry(&handle)
960 }
961 SyncCommands::List => cmd::sync::list_registries(),
962 SyncCommands::Set { url } => {
963 cmd::sync::set_registry(&url)
964 }
965 }
966 } else if local {
967 plugin_manager.trigger_hook("on_pre_sync", None)?;
968 let res =
969 cmd::sync::run_local(verbose, fallback, force, frozen);
970 plugin_manager.trigger_hook_nonfatal("on_post_sync", None);
971 res
972 } else {
973 plugin_manager.trigger_hook("on_pre_sync", None)?;
974 let res = cmd::sync::run(
975 verbose,
976 fallback,
977 no_package_managers,
978 force,
979 scope
980 );
981 plugin_manager.trigger_hook_nonfatal("on_post_sync", None);
982 res
983 }
984 }
985 Commands::Migrate(args) => cmd::migrate::run(args),
986 Commands::List {
987 all,
988 outdated,
989 registry,
990 repo,
991 package_type,
992 foreign,
993 names,
994 completion
995 } => cmd::list::run(
996 all,
997 outdated,
998 registry.as_deref(),
999 repo.as_deref(),
1000 package_type.as_deref(),
1001 foreign,
1002 names,
1003 completion
1004 ),
1005 Commands::Show {
1006 package_name,
1007 raw,
1008 purl
1009 } => cmd::show::run(&package_name, raw, purl),
1010 Commands::Pin { package, version } => {
1011 cmd::pin::run(&package, &version)
1012 }
1013 Commands::Provides { term } => cmd::provides::run(&term),
1014 Commands::Tree { packages } => cmd::tree::run(&packages),
1015 Commands::Unpin { package } => cmd::unpin::run(&package),
1016 Commands::Mark {
1017 packages,
1018 as_dependency,
1019 as_explicit
1020 } => cmd::mark::run(&packages, as_dependency, as_explicit),
1021 Commands::Update {
1022 package_names,
1023 all,
1024 dry_run,
1025 explain,
1026 plan_json,
1027 verbose,
1028 interactive
1029 } => cmd::update::run(
1030 all,
1031 &package_names,
1032 cli.yes,
1033 dry_run,
1034 explain,
1035 plan_json,
1036 verbose,
1037 interactive
1038 )
1039 .map_err(|e| cmd::ux::with_failure_hint("update", e)),
1040 Commands::Install(args) => args
1041 .run(cli.yes)
1042 .map_err(|e| cmd::ux::with_failure_hint("install", e)),
1043 Commands::Use { packages, global } => {
1044 cmd::use_cmd::run(&packages, global)
1045 }
1046 Commands::Uninstall(args) => args
1047 .run(cli.yes)
1048 .map_err(|e| cmd::ux::with_failure_hint("uninstall", e)),
1049 Commands::Run { cmd_alias, args } => {
1050 cmd::run::run(cmd_alias.as_deref(), &args)
1051 }
1052 Commands::Env {
1053 env_alias,
1054 export_shell
1055 } => cmd::env::run(env_alias.as_deref(), export_shell),
1056 Commands::Dev { run, repo } => cmd::dev::run(run, repo),
1057 Commands::Upgrade { force, tag, branch } => {
1058 match cmd::upgrade::run(
1059 BRANCH, STATUS, NUMBER, force, tag, branch
1060 ) {
1061 Ok(()) => {
1062 println!(
1063 "\n{}",
1064 "Zoi upgraded successfully! Please restart your \
1065 shell for changes to take effect."
1066 .green()
1067 );
1068 println!(
1069 "\n{}: https://github.com/zillowe/zoi/blob/main/CHANGELOG.md",
1070 "Changelog".cyan().bold()
1071 );
1072 println!(
1073 "\n{}: To update shell completions, run 'zoi \
1074 shell <your-shell>'.",
1075 "Hint".cyan().bold()
1076 );
1077 }
1078 Err(e) if e.to_string() == "already_on_latest" => {}
1079 Err(e) if e.to_string() == "managed_by_package_manager" => {
1080 }
1081 Err(e) => return Err(e)
1082 }
1083 Ok(())
1084 }
1085 Commands::Autoremove { dry_run } => {
1086 cmd::autoremove::run(cli.yes, dry_run)
1087 }
1088 Commands::Why { package_name } => cmd::why::run(&package_name),
1089 Commands::Owner { path } => cmd::owner::run(&path),
1090 Commands::Files { package } => cmd::files::run(&package),
1091 Commands::History {
1092 verify,
1093 export,
1094 ndjson
1095 } => cmd::history::run(verify, export, ndjson),
1096 Commands::Search {
1097 search_term,
1098 registry,
1099 repo,
1100 package_type,
1101 tags,
1102 sort,
1103 files,
1104 interactive
1105 } => cmd::search::run(
1106 &search_term,
1107 registry.as_deref(),
1108 repo.as_deref(),
1109 package_type.as_deref(),
1110 tags,
1111 &sort,
1112 files,
1113 interactive
1114 ),
1115 Commands::Service(args) => cmd::service::run(args),
1116 Commands::Shell {
1117 shell,
1118 hook,
1119 scope,
1120 packages,
1121 run,
1122 verbose
1123 } => {
1124 let target_shell = shell
1125 .or_else(crate::pkg::utils::get_current_shell)
1126 .unwrap_or(Shell::Bash);
1127 if hook {
1128 cmd::shell::print_hook(target_shell)
1129 } else if !packages.is_empty() {
1130 cmd::shell::enter_ephemeral_shell(
1131 &packages,
1132 run,
1133 verbose,
1134 Some(&plugin_manager)
1135 )
1136 } else {
1137 cmd::shell::run(target_shell, scope)
1138 }
1139 }
1140 Commands::Exec {
1141 source,
1142 bin,
1143 verbose,
1144 args
1145 } => cmd::exec::run(&source, bin, &args, verbose),
1146 Commands::Download {
1147 package,
1148 archive: _,
1149 source,
1150 output_dir
1151 } => {
1152 let download_type = if source {
1153 cmd::download::DownloadType::Source
1154 } else {
1155 cmd::download::DownloadType::Archive
1156 };
1157 cmd::download::run(&package, download_type, output_dir)
1158 }
1159 Commands::Clone { package, location } => {
1160 cmd::clone::run(&package, location, cli.yes)
1161 }
1162 Commands::Cache { command } => match command {
1163 CacheCommands::Add { files } => cmd::cache::add(&files),
1164 CacheCommands::Clear { dry_run } => cmd::cache::clear(dry_run),
1165 CacheCommands::List => cmd::cache::list(),
1166 CacheCommands::Mirror { command } => match command {
1167 CacheMirrorCommands::Add { url } => {
1168 cmd::cache::add_mirror(&url)
1169 }
1170 CacheMirrorCommands::Remove { url } => {
1171 cmd::cache::remove_mirror(&url)
1172 }
1173 CacheMirrorCommands::List => cmd::cache::list_mirrors()
1174 }
1175 },
1176 Commands::Transaction { command } => match command {
1177 TransactionCommands::List => cmd::transaction::list(),
1178 TransactionCommands::Show { id } => cmd::transaction::show(&id),
1179 TransactionCommands::Files { id } => {
1180 cmd::transaction::files(&id)
1181 }
1182 TransactionCommands::Undo { id, yes } => {
1183 cmd::transaction::undo(id.as_deref(), yes, cli.yes)
1184 }
1185 },
1186 Commands::Repo(args) => cmd::repo::run(args),
1187 Commands::Registry(args) => cmd::registry::run(args),
1188 Commands::Home(args) => cmd::home::run(args),
1189 Commands::System(args) => cmd::system::run(args, cli.yes),
1190 Commands::Telemetry { action } => {
1191 use cmd::telemetry::{TelemetryCommand, run};
1192 let cmd = match action {
1193 TelemetryAction::Status => TelemetryCommand::Status,
1194 TelemetryAction::Enable => TelemetryCommand::Enable,
1195 TelemetryAction::Disable => TelemetryCommand::Disable
1196 };
1197 run(cmd)
1198 }
1199 Commands::Create { source, app_name } => cmd::create::run(
1200 cmd::create::CreateCommand { source, app_name },
1201 cli.yes,
1202 Some(&plugin_manager)
1203 ),
1204 Commands::Downgrade { package } => {
1205 cmd::downgrade::run(&package, cli.yes, Some(&plugin_manager))
1206 }
1207 Commands::Extension(args) => {
1208 cmd::extension::run(args, cli.yes, Some(&plugin_manager))
1209 }
1210 Commands::Rollback { package } => {
1211 cmd::rollback::run(&package, cli.yes, Some(&plugin_manager))
1212 }
1213 Commands::Man {
1214 package_name,
1215 upstream,
1216 raw,
1217 page
1218 } => cmd::man::run(&package_name, upstream, raw, page.as_deref()),
1219 Commands::Package(args) => cmd::package::run(args),
1220 Commands::Pgp(args) => cmd::pgp::run(args),
1221 Commands::Helper(args) => cmd::helper::run(args),
1222 Commands::Doctor => cmd::doctor::run(),
1223 Commands::Audit {
1224 all,
1225 registry,
1226 repo
1227 } => cmd::audit::run(all, registry, repo.as_deref()),
1228 Commands::External(args) => {
1229 let (cmd_name, cmd_args) =
1230 if let Some((first, rest)) = args.split_first() {
1231 (first, rest.to_vec())
1232 } else {
1233 return Err(anyhow::anyhow!("No command specified"));
1234 };
1235
1236 match plugin_manager.run_command(cmd_name, cmd_args) {
1237 Ok(true) => Ok(()),
1238 Ok(false) => {
1239 let mut shadow_cmd = Cli::command().styles(styles);
1240 shadow_cmd =
1241 shadow_cmd.allow_external_subcommands(false);
1242
1243 let err = shadow_cmd
1244 .clone()
1245 .try_get_matches_from(std::env::args())
1246 .err()
1247 .unwrap_or_else(|| {
1248 shadow_cmd.error(
1249 clap::error::ErrorKind::InvalidSubcommand,
1250 format!(
1251 "unrecognized subcommand '{cmd_name}'"
1252 )
1253 )
1254 });
1255
1256 let plugin_cmds = plugin_manager.list_commands()?;
1257 if !plugin_cmds.is_empty() {
1258 eprintln!(
1259 "{}:",
1260 "Available Plugin Commands".cyan().bold()
1261 );
1262 for (pcmd, pdesc) in plugin_cmds {
1263 if pdesc.is_empty() {
1264 eprintln!(" {pcmd}");
1265 } else {
1266 eprintln!(
1267 " {:<12} {}",
1268 pcmd,
1269 pdesc.dimmed()
1270 );
1271 }
1272 }
1273 eprintln!();
1274 }
1275
1276 err.exit();
1277 }
1278 Err(e) => Err(e)
1279 }
1280 }
1281 };
1282
1283 if let Err(e) = result {
1284 eprintln!("Error: {e}");
1285 std::process::exit(1);
1286 }
1287 }
1288 Ok(())
1289}