| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | mod admin_cmd; |
| 13 | mod config_cmd; |
| 14 | mod context; |
| 15 | mod group_cmd; |
| 16 | mod hook_cmd; |
| 17 | mod key_cmd; |
| 18 | mod prompt; |
| 19 | mod repo_cmd; |
| 20 | #[cfg(test)] |
| 21 | mod testutil; |
| 22 | mod token_cmd; |
| 23 | mod user_cmd; |
| 24 | |
| 25 | use std::path::PathBuf; |
| 26 | use std::process::ExitCode; |
| 27 | |
| 28 | use clap::{Parser, Subcommand}; |
| 29 | |
| 30 | |
| 31 | #[derive(Debug, Parser)] |
| 32 | #[command(name = "fabrica", version, about = "A self-hosted git server.")] |
| 33 | struct Cli { |
| 34 | |
| 35 | #[arg(long, global = true, value_name = "PATH")] |
| 36 | config: Option<PathBuf>, |
| 37 | |
| 38 | |
| 39 | #[arg(long, global = true, value_name = "LEVEL")] |
| 40 | log_level: Option<String>, |
| 41 | |
| 42 | |
| 43 | #[arg(long, global = true)] |
| 44 | json: bool, |
| 45 | |
| 46 | |
| 47 | #[command(subcommand)] |
| 48 | command: Command, |
| 49 | } |
| 50 | |
| 51 | |
| 52 | #[derive(Debug, Subcommand)] |
| 53 | enum Command { |
| 54 | |
| 55 | Config { |
| 56 | |
| 57 | #[command(subcommand)] |
| 58 | command: config_cmd::ConfigCommand, |
| 59 | }, |
| 60 | |
| 61 | User { |
| 62 | |
| 63 | #[command(subcommand)] |
| 64 | command: user_cmd::UserCommand, |
| 65 | }, |
| 66 | |
| 67 | Key { |
| 68 | |
| 69 | #[command(subcommand)] |
| 70 | command: key_cmd::KeyCommand, |
| 71 | }, |
| 72 | |
| 73 | Repo { |
| 74 | |
| 75 | #[command(subcommand)] |
| 76 | command: repo_cmd::RepoCommand, |
| 77 | }, |
| 78 | |
| 79 | Group { |
| 80 | |
| 81 | #[command(subcommand)] |
| 82 | command: group_cmd::GroupCommand, |
| 83 | }, |
| 84 | |
| 85 | Token { |
| 86 | |
| 87 | #[command(subcommand)] |
| 88 | command: token_cmd::TokenCommand, |
| 89 | }, |
| 90 | |
| 91 | Serve { |
| 92 | |
| 93 | |
| 94 | #[arg(long)] |
| 95 | detach: bool, |
| 96 | |
| 97 | #[arg(long)] |
| 98 | no_migrate: bool, |
| 99 | }, |
| 100 | |
| 101 | Migrate, |
| 102 | |
| 103 | Doctor { |
| 104 | |
| 105 | #[arg(long)] |
| 106 | quiet: bool, |
| 107 | }, |
| 108 | |
| 109 | #[command(hide = true)] |
| 110 | Hook { |
| 111 | |
| 112 | #[command(subcommand)] |
| 113 | command: hook_cmd::HookCommand, |
| 114 | }, |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | #[derive(Debug, Clone)] |
| 121 | pub struct ServeRequest { |
| 122 | |
| 123 | pub config_path: Option<PathBuf>, |
| 124 | |
| 125 | pub detach: bool, |
| 126 | |
| 127 | pub no_migrate: bool, |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | pub fn resolve_secret(config: &config::Config) -> anyhow::Result<String> { |
| 137 | context::ensure_secret(config) |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | pub fn run<F>(serve: F) -> ExitCode |
| 148 | where |
| 149 | F: FnOnce(ServeRequest) -> anyhow::Result<ExitCode>, |
| 150 | { |
| 151 | let cli = Cli::parse(); |
| 152 | match dispatch(&cli, serve) { |
| 153 | Ok(code) => code, |
| 154 | Err(err) => { |
| 155 | eprintln!("error: {err:#}"); |
| 156 | ExitCode::FAILURE |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | fn dispatch<F>(cli: &Cli, serve: F) -> anyhow::Result<ExitCode> |
| 163 | where |
| 164 | F: FnOnce(ServeRequest) -> anyhow::Result<ExitCode>, |
| 165 | { |
| 166 | match &cli.command { |
| 167 | Command::Serve { detach, no_migrate } => serve(ServeRequest { |
| 168 | config_path: cli.config.clone(), |
| 169 | detach: *detach, |
| 170 | no_migrate: *no_migrate, |
| 171 | }), |
| 172 | Command::Config { command } => config_cmd::run(command, cli.config.as_deref(), cli.json), |
| 173 | Command::User { command } => user_cmd::run(command, cli.config.as_deref(), cli.json), |
| 174 | Command::Key { command } => key_cmd::run(command, cli.config.as_deref(), cli.json), |
| 175 | Command::Repo { command } => repo_cmd::run(command, cli.config.as_deref(), cli.json), |
| 176 | Command::Group { command } => group_cmd::run(command, cli.config.as_deref(), cli.json), |
| 177 | Command::Token { command } => token_cmd::run(command, cli.config.as_deref(), cli.json), |
| 178 | Command::Migrate => admin_cmd::migrate(cli.config.as_deref()), |
| 179 | Command::Doctor { quiet } => admin_cmd::doctor(cli.config.as_deref(), *quiet), |
| 180 | Command::Hook { command } => Ok(hook_cmd::run(command)), |
| 181 | } |
| 182 | } |