OXYGENHUB
# fce44ae9ad29 oxygen / src / cli / mod.rs
5.5 KiB · Rust · last changed User config file layered under repo config, user-scoped identities; split the CLI into modules; fix clone remote persistence 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
    },
75
    /// Serve repositories under ROOT over HTTP (spec §16); TLS is terminated in front
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>,
85
    },
86
    /// Manage the signing identity
87
    Identity {
88
        #[command(subcommand)]
89
        cmd: IdentityCmd,
90
    },
91
    /// Verify the signatures on a change (default: every change in the log)
92
    Verify { change: Option<String> },
93
    /// Set a configuration key (author.*, core.sign, workspace.bookmark, remote.<name>.url|token)
94
    Config {
95
        key: String,
96
        value: String,
97
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
98
        #[arg(long)]
99
        user: bool,
100
    },
101
}
102
103
#[derive(Subcommand)]
104
enum OpCmd {
105
    Log,
106
}
107
108
#[derive(Subcommand)]
109
enum RemoteCmd {
110
    Add { name: String, url: String },
111
    Remove { name: String },
112
    List,
113
}
114
115
#[derive(Subcommand)]
116
enum IdentityCmd {
117
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
118
    Create {
119
        #[arg(long)]
120
        name: Option<String>,
121
        #[arg(long)]
122
        email: Option<String>,
123
        /// Record the author in this repository only, not in the user config
124
        #[arg(long)]
125
        local: bool,
126
    },
127
    /// Show the configured identity
128
    Show,
129
}
130
131
/// Open the repository containing the current directory.
132
fn open() -> Result<Repo> {
133
    Repo::open(&std::env::current_dir()?).context("opening repository")
134
}
135
136
/// Parse arguments and run; returns the process exit code.
137
pub fn run() -> Result<i32> {
138
    let cli = Cli::parse();
139
    match cli.cmd {
140
        Cmd::Init { path } => workspace::init(path),
141
        Cmd::Status => workspace::status(),
142
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
143
        Cmd::Log => workspace::log(),
144
        Cmd::Diff => workspace::diff(),
145
        Cmd::Show { change } => workspace::show(&change),
146
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
147
        Cmd::Resolve => workspace::resolve(),
148
        Cmd::Undo => workspace::undo(),
149
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
150
        Cmd::Clone { url, dest } => sync::clone(&url, dest),
151
        Cmd::Remote { cmd } => match cmd {
152
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
153
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
154
            RemoteCmd::List => sync::remote_list(),
155
        },
156
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
157
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
158
        Cmd::Push { remote, force } => sync::push(remote.as_deref(), force),
159
        Cmd::Serve { root, bind, tokens } => sync::serve(root, &bind, tokens),
160
        Cmd::Identity { cmd } => match cmd {
161
            IdentityCmd::Create { name, email, local } => identity::create(name, email, local),
162
            IdentityCmd::Show => identity::show(),
163
        },
164
        Cmd::Verify { change } => identity::verify(change.as_deref()),
165
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
166
    }
167
}