OXYGENHUB
# a269bee11bb2 oxygen / src / cli / mod.rs
6.9 KiB · Rust · last changed Trust policy: policy objects, init/grant/revoke, enforcement on push and fetch, propagation with downgrade protection (spec 0.6) 1 month ago source blame history raw
fce44ae9 Andy McCall ↶ 1
//! Command-line definitions and dispatch.
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
    /// Create a repository in PATH (default: current directory)
23
    Init { path: Option<PathBuf> },
24
    /// Clone a repository from a local path or an http(s) URL
25
    Clone { url: String, dest: Option<PathBuf> },
26
    /// Show the working copy: changed paths and conflicts
27
    Status,
28
    /// Record the working copy as a change on the current bookmark
29
    Commit {
30
        #[arg(short, long)]
31
        message: String,
32
        /// Extra explicit dependencies (ChangeId prefix, #hash prefix)
33
        #[arg(long = "depends-on")]
34
        depends_on: Vec<String>,
35
    },
36
    /// List changes reachable from the bookmark and the working copy
37
    Log,
38
    /// Show what the working copy changes against its base
39
    Diff,
40
    /// Show one change (ChangeId prefix, #hash prefix, or @)
41
    Show { change: String },
42
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
43
    Checkout {
44
        changes: Vec<String>,
45
        /// Throw away uncommitted working-copy changes
46
        #[arg(long)]
47
        discard: bool,
48
    },
49
    /// Report which conflicts of the working copy's base are resolved by the current files
50
    Resolve,
51
    /// Undo the last operation
52
    Undo,
53
    /// List operations
54
    #[command(name = "op")]
55
    Op {
56
        #[command(subcommand)]
57
        cmd: OpCmd,
58
    },
59
    /// Manage remotes
60
    Remote {
61
        #[command(subcommand)]
62
        cmd: RemoteCmd,
63
    },
64
    /// Fetch every bookmark of a remote into its tracking references
65
    Fetch { remote: Option<String> },
66
    /// Fetch, then union the remote bookmark into the local one (no merge change)
67
    Pull { remote: Option<String> },
68
    /// Push the current bookmark to a remote
69
    Push {
70
        remote: Option<String>,
71
        /// Allow dropping heads the remote has
72
        #[arg(long)]
73
        force: bool,
74
    },
2b5ee371 Andy McCall ↶ 75
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
fce44ae9 Andy McCall ↶ 76
    Serve {
77
        /// Directory with one repository per subdirectory
78
        #[arg(long, default_value = ".")]
79
        root: PathBuf,
80
        #[arg(long, default_value = "127.0.0.1:8890")]
81
        bind: String,
82
        /// Bearer token allowed to push (repeatable); none means open
83
        #[arg(long = "token")]
84
        tokens: Vec<String>,
2b5ee371 Andy McCall ↶ 85
        /// PEM certificate chain; enables HTTPS together with --tls-key
86
        #[arg(long, requires = "tls_key")]
87
        tls_cert: Option<PathBuf>,
88
        /// PEM private key
89
        #[arg(long, requires = "tls_cert")]
90
        tls_key: Option<PathBuf>,
fce44ae9 Andy McCall ↶ 91
    },
92
    /// Manage the signing identity
93
    Identity {
94
        #[command(subcommand)]
95
        cmd: IdentityCmd,
96
    },
97
    /// Verify the signatures on a change (default: every change in the log)
98
    Verify { change: Option<String> },
83e0677b Andy McCall ↶ 99
    /// Manage the trust policy (who may update which bookmarks)
100
    Policy {
101
        #[command(subcommand)]
102
        cmd: PolicyCmd,
103
    },
2b5ee371 Andy McCall ↶ 104
    /// Set a configuration key (author.*, core.sign, workspace.bookmark, remote.<name>.url|token|ca)
fce44ae9 Andy McCall ↶ 105
    Config {
106
        key: String,
107
        value: String,
108
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
109
        #[arg(long)]
110
        user: bool,
111
    },
112
}
113
114
#[derive(Subcommand)]
115
enum OpCmd {
116
    Log,
117
}
118
119
#[derive(Subcommand)]
120
enum RemoteCmd {
121
    Add { name: String, url: String },
122
    Remove { name: String },
123
    List,
124
}
125
126
#[derive(Subcommand)]
127
enum IdentityCmd {
128
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
129
    Create {
130
        #[arg(long)]
131
        name: Option<String>,
132
        #[arg(long)]
133
        email: Option<String>,
134
        /// Record the author in this repository only, not in the user config
135
        #[arg(long)]
136
        local: bool,
137
    },
138
    /// Show the configured identity
139
    Show,
83e0677b Andy McCall ↶ 140
    /// Write your identity object to a file, to hand to a policy holder
141
    Export { path: Option<PathBuf> },
fce44ae9 Andy McCall ↶ 142
}
143
83e0677b Andy McCall ↶ 144
#[derive(Subcommand)]
145
enum PolicyCmd {
146
    /// Create the initial policy with your identity as the only root
147
    Init,
148
    /// Show the current policy
149
    Show,
150
    /// Allow an identity (from `oxy identity export`) to update bookmarks
151
    Grant {
152
        identity_file: PathBuf,
153
        /// Reference pattern the grant applies to (default: every bookmark)
154
        #[arg(long)]
155
        pattern: Option<String>,
156
    },
157
    /// Revoke a key (KeyId prefix of an identity in the policy)
158
    Revoke { key: String },
159
}
160
fce44ae9 Andy McCall ↶ 161
/// Open the repository containing the current directory.
162
fn open() -> Result<Repo> {
163
    Repo::open(&std::env::current_dir()?).context("opening repository")
164
}
165
166
/// Parse arguments and run; returns the process exit code.
167
pub fn run() -> Result<i32> {
168
    let cli = Cli::parse();
169
    match cli.cmd {
170
        Cmd::Init { path } => workspace::init(path),
171
        Cmd::Status => workspace::status(),
172
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
173
        Cmd::Log => workspace::log(),
174
        Cmd::Diff => workspace::diff(),
175
        Cmd::Show { change } => workspace::show(&change),
176
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
177
        Cmd::Resolve => workspace::resolve(),
178
        Cmd::Undo => workspace::undo(),
179
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
180
        Cmd::Clone { url, dest } => sync::clone(&url, dest),
181
        Cmd::Remote { cmd } => match cmd {
182
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
183
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
184
            RemoteCmd::List => sync::remote_list(),
185
        },
186
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
187
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
188
        Cmd::Push { remote, force } => sync::push(remote.as_deref(), force),
2b5ee371 Andy McCall ↶ 189
        Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
fce44ae9 Andy McCall ↶ 190
        Cmd::Identity { cmd } => match cmd {
191
            IdentityCmd::Create { name, email, local } => identity::create(name, email, local),
192
            IdentityCmd::Show => identity::show(),
83e0677b Andy McCall ↶ 193
            IdentityCmd::Export { path } => identity::export(path),
194
        },
195
        Cmd::Policy { cmd } => match cmd {
196
            PolicyCmd::Init => identity::policy_init(),
197
            PolicyCmd::Show => identity::policy_show(),
198
            PolicyCmd::Grant { identity_file, pattern } => identity::policy_grant(&identity_file, pattern.as_deref()),
199
            PolicyCmd::Revoke { key } => identity::policy_revoke(&key),
fce44ae9 Andy McCall ↶ 200
        },
201
        Cmd::Verify { change } => identity::verify(change.as_deref()),
202
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
203
    }
204
}