| 1 |
|
| 2 |
|
| 3 |
mod format; |
| 4 |
mod identity; |
| 5 |
mod sync; |
| 6 |
mod workspace; |
| 7 |
|
| 8 |
use anyhow::{Context, Result}; |
| 9 |
use clap::{Parser, Subcommand}; |
| 10 |
use oxy_core::repo::Repo; |
| 11 |
use std::path::PathBuf; |
| 12 |
|
| 13 |
#[derive(Parser)] |
| 14 |
#[command(name = "oxy", version, about = "Oxygen: breathing fresh air into version control")] |
| 15 |
struct Cli { |
| 16 |
#[command(subcommand)] |
| 17 |
cmd: Cmd, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Subcommand)] |
| 21 |
enum Cmd { |
| 22 |
|
| 23 |
Init { path: Option<PathBuf> }, |
| 24 |
|
| 25 |
Clone { url: String, dest: Option<PathBuf> }, |
| 26 |
|
| 27 |
Status, |
| 28 |
|
| 29 |
Commit { |
| 30 |
#[arg(short, long)] |
| 31 |
message: String, |
| 32 |
|
| 33 |
#[arg(long = "depends-on")] |
| 34 |
depends_on: Vec<String>, |
| 35 |
}, |
| 36 |
|
| 37 |
Log, |
| 38 |
|
| 39 |
Diff, |
| 40 |
|
| 41 |
Describe { |
| 42 |
#[arg(short, long)] |
| 43 |
message: String, |
| 44 |
change: Option<String>, |
| 45 |
}, |
| 46 |
|
| 47 |
Amend { |
| 48 |
#[arg(short, long)] |
| 49 |
message: Option<String>, |
| 50 |
}, |
| 51 |
|
| 52 |
Abandon { change: String }, |
| 53 |
|
| 54 |
Show { change: String }, |
| 55 |
|
| 56 |
Checkout { |
| 57 |
changes: Vec<String>, |
| 58 |
|
| 59 |
#[arg(long)] |
| 60 |
discard: bool, |
| 61 |
}, |
| 62 |
|
| 63 |
Resolve, |
| 64 |
|
| 65 |
Undo, |
| 66 |
|
| 67 |
Checkpoint, |
| 68 |
|
| 69 |
Gc { |
| 70 |
|
| 71 |
#[arg(long)] |
| 72 |
now: bool, |
| 73 |
}, |
| 74 |
|
| 75 |
#[command(name = "op")] |
| 76 |
Op { |
| 77 |
#[command(subcommand)] |
| 78 |
cmd: OpCmd, |
| 79 |
}, |
| 80 |
|
| 81 |
Remote { |
| 82 |
#[command(subcommand)] |
| 83 |
cmd: RemoteCmd, |
| 84 |
}, |
| 85 |
|
| 86 |
Fetch { remote: Option<String> }, |
| 87 |
|
| 88 |
Pull { remote: Option<String> }, |
| 89 |
|
| 90 |
Push { |
| 91 |
remote: Option<String>, |
| 92 |
|
| 93 |
#[arg(long)] |
| 94 |
force: bool, |
| 95 |
}, |
| 96 |
|
| 97 |
Serve { |
| 98 |
|
| 99 |
#[arg(long, default_value = ".")] |
| 100 |
root: PathBuf, |
| 101 |
#[arg(long, default_value = "127.0.0.1:8890")] |
| 102 |
bind: String, |
| 103 |
|
| 104 |
#[arg(long = "token")] |
| 105 |
tokens: Vec<String>, |
| 106 |
|
| 107 |
#[arg(long, requires = "tls_key")] |
| 108 |
tls_cert: Option<PathBuf>, |
| 109 |
|
| 110 |
#[arg(long, requires = "tls_cert")] |
| 111 |
tls_key: Option<PathBuf>, |
| 112 |
}, |
| 113 |
|
| 114 |
Identity { |
| 115 |
#[command(subcommand)] |
| 116 |
cmd: IdentityCmd, |
| 117 |
}, |
| 118 |
|
| 119 |
Verify { change: Option<String> }, |
| 120 |
|
| 121 |
Git { |
| 122 |
#[command(subcommand)] |
| 123 |
cmd: GitCmd, |
| 124 |
}, |
| 125 |
|
| 126 |
Policy { |
| 127 |
#[command(subcommand)] |
| 128 |
cmd: PolicyCmd, |
| 129 |
}, |
| 130 |
|
| 131 |
Config { |
| 132 |
key: String, |
| 133 |
value: String, |
| 134 |
|
| 135 |
#[arg(long)] |
| 136 |
user: bool, |
| 137 |
}, |
| 138 |
} |
| 139 |
|
| 140 |
#[derive(Subcommand)] |
| 141 |
enum OpCmd { |
| 142 |
Log, |
| 143 |
} |
| 144 |
|
| 145 |
#[derive(Subcommand)] |
| 146 |
enum RemoteCmd { |
| 147 |
Add { name: String, url: String }, |
| 148 |
Remove { name: String }, |
| 149 |
List, |
| 150 |
} |
| 151 |
|
| 152 |
#[derive(Subcommand)] |
| 153 |
enum IdentityCmd { |
| 154 |
|
| 155 |
Create { |
| 156 |
#[arg(long)] |
| 157 |
name: Option<String>, |
| 158 |
#[arg(long)] |
| 159 |
email: Option<String>, |
| 160 |
|
| 161 |
#[arg(long)] |
| 162 |
local: bool, |
| 163 |
}, |
| 164 |
|
| 165 |
Show, |
| 166 |
|
| 167 |
Export { path: Option<PathBuf> }, |
| 168 |
} |
| 169 |
|
| 170 |
#[derive(Subcommand)] |
| 171 |
enum GitCmd { |
| 172 |
|
| 173 |
Export { git_dir: PathBuf }, |
| 174 |
|
| 175 |
Import { |
| 176 |
git_dir: PathBuf, |
| 177 |
dest: PathBuf, |
| 178 |
#[arg(long, default_value = "main")] |
| 179 |
branch: String, |
| 180 |
}, |
| 181 |
} |
| 182 |
|
| 183 |
#[derive(Subcommand)] |
| 184 |
enum PolicyCmd { |
| 185 |
|
| 186 |
Init, |
| 187 |
|
| 188 |
Show, |
| 189 |
|
| 190 |
Grant { |
| 191 |
identity_file: PathBuf, |
| 192 |
|
| 193 |
#[arg(long)] |
| 194 |
pattern: Option<String>, |
| 195 |
}, |
| 196 |
|
| 197 |
Revoke { key: String }, |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
fn open() -> Result<Repo> { |
| 202 |
Repo::open(&std::env::current_dir()?).context("opening repository") |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
pub fn run() -> Result<i32> { |
| 207 |
let cli = Cli::parse(); |
| 208 |
match cli.cmd { |
| 209 |
Cmd::Init { path } => workspace::init(path), |
| 210 |
Cmd::Status => workspace::status(), |
| 211 |
Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on), |
| 212 |
Cmd::Log => workspace::log(), |
| 213 |
Cmd::Diff => workspace::diff(), |
| 214 |
Cmd::Describe { message, change } => workspace::describe(&message, change.as_deref()), |
| 215 |
Cmd::Amend { message } => workspace::amend(message.as_deref()), |
| 216 |
Cmd::Abandon { change } => workspace::abandon(&change), |
| 217 |
Cmd::Show { change } => workspace::show(&change), |
| 218 |
Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard), |
| 219 |
Cmd::Resolve => workspace::resolve(), |
| 220 |
Cmd::Undo => workspace::undo(), |
| 221 |
Cmd::Gc { now } => workspace::gc(now), |
| 222 |
Cmd::Checkpoint => workspace::checkpoint(), |
| 223 |
Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(), |
| 224 |
Cmd::Clone { url, dest } => sync::clone(&url, dest), |
| 225 |
Cmd::Remote { cmd } => match cmd { |
| 226 |
RemoteCmd::Add { name, url } => sync::remote_add(&name, &url), |
| 227 |
RemoteCmd::Remove { name } => sync::remote_remove(&name), |
| 228 |
RemoteCmd::List => sync::remote_list(), |
| 229 |
}, |
| 230 |
Cmd::Fetch { remote } => sync::fetch(remote.as_deref()), |
| 231 |
Cmd::Pull { remote } => sync::pull(remote.as_deref()), |
| 232 |
Cmd::Push { remote, force } => sync::push(remote.as_deref(), force), |
| 233 |
Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)), |
| 234 |
Cmd::Identity { cmd } => match cmd { |
| 235 |
IdentityCmd::Create { name, email, local } => identity::create(name, email, local), |
| 236 |
IdentityCmd::Show => identity::show(), |
| 237 |
IdentityCmd::Export { path } => identity::export(path), |
| 238 |
}, |
| 239 |
Cmd::Git { cmd: GitCmd::Export { git_dir } } => sync::git_export(&git_dir), |
| 240 |
Cmd::Git { cmd: GitCmd::Import { git_dir, dest, branch } } => sync::git_import(&git_dir, &dest, &branch), |
| 241 |
Cmd::Policy { cmd } => match cmd { |
| 242 |
PolicyCmd::Init => identity::policy_init(), |
| 243 |
PolicyCmd::Show => identity::policy_show(), |
| 244 |
PolicyCmd::Grant { identity_file, pattern } => identity::policy_grant(&identity_file, pattern.as_deref()), |
| 245 |
PolicyCmd::Revoke { key } => identity::policy_revoke(&key), |
| 246 |
}, |
| 247 |
Cmd::Verify { change } => identity::verify(change.as_deref()), |
| 248 |
Cmd::Config { key, value, user } => identity::config(&key, &value, user), |
| 249 |
} |
| 250 |
} |