openclaw-cli 0.1.0

Command-line interface for OpenClaw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! `OpenClaw` CLI - Command-line interface for `OpenClaw`.

mod commands;
mod ui;

use clap::{Parser, Subcommand};
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

#[derive(Parser)]
#[command(name = "openclaw")]
#[command(about = "OpenClaw - AI agent platform")]
#[command(version)]
#[command(propagate_version = true)]
struct Cli {
    /// Verbose output
    #[arg(short, long, global = true)]
    verbose: bool,

    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// Run the interactive onboarding wizard
    Onboard {
        /// Non-interactive mode
        #[arg(long)]
        non_interactive: bool,

        /// Accept risk acknowledgement (required for non-interactive)
        #[arg(long)]
        accept_risk: bool,

        /// Setup flow: quickstart or advanced
        #[arg(long)]
        flow: Option<String>,

        /// Auth provider choice
        #[arg(long)]
        auth_choice: Option<String>,

        /// API key for selected provider
        #[arg(long)]
        api_key: Option<String>,

        /// Install daemon after setup
        #[arg(long)]
        install_daemon: bool,
    },

    /// Update configuration interactively
    Configure {
        /// Section to configure: auth, gateway, agents, channels, workspace
        #[arg(long)]
        section: Option<String>,
    },

    /// Run health checks and auto-repair
    Doctor {
        /// Apply recommended fixes
        #[arg(long, alias = "fix")]
        repair: bool,

        /// Aggressive repairs (may overwrite custom config)
        #[arg(long)]
        force: bool,

        /// Deep scan for extra service installs
        #[arg(long)]
        deep: bool,
    },

    /// Show gateway, agents, and channels status
    Status {
        /// Show all details
        #[arg(long)]
        all: bool,

        /// Probe services for connectivity
        #[arg(long)]
        deep: bool,
    },

    /// Gateway operations
    Gateway {
        #[command(subcommand)]
        action: GatewayCommands,
    },

    /// Channel management
    Channels {
        /// List configured channels
        #[arg(long)]
        list: bool,

        /// Probe channels for connectivity
        #[arg(long)]
        probe: bool,
    },

    /// Configuration get/set
    Config {
        #[command(subcommand)]
        action: Option<ConfigCommands>,
    },

    /// Shell completion setup
    Completion {
        /// Shell type: zsh, bash, fish, powershell
        #[arg(long)]
        shell: Option<String>,

        /// Install completions to shell profile
        #[arg(long)]
        install: bool,

        /// Write completion state cache
        #[arg(long)]
        write_state: bool,
    },

    /// Daemon management (system service)
    Daemon {
        #[command(subcommand)]
        action: DaemonCommands,
    },

    /// Reset configuration or state
    Reset {
        /// Reset only configuration
        #[arg(long)]
        config_only: bool,

        /// Reset everything including sessions
        #[arg(long)]
        all: bool,
    },

    /// User management (admin commands)
    Admin {
        #[command(subcommand)]
        action: AdminCommands,

        /// Data directory override
        #[arg(long, global = true)]
        data_dir: Option<std::path::PathBuf>,
    },
}

#[derive(Subcommand)]
enum GatewayCommands {
    /// Start the gateway server
    Run {
        /// Port to listen on
        #[arg(short, long)]
        port: Option<u16>,

        /// Bind address (loopback, lan, or IP)
        #[arg(long)]
        bind: Option<String>,

        /// Force start even if port is in use
        #[arg(long)]
        force: bool,
    },

    /// Check gateway status
    Status,
}

#[derive(Subcommand)]
enum ConfigCommands {
    /// Get a configuration value
    Get {
        /// Configuration key (e.g., gateway.port)
        key: String,
    },

    /// Set a configuration value
    Set {
        /// Configuration key (e.g., gateway.port)
        key: String,

        /// Value to set
        value: String,
    },

    /// Show full configuration
    Show,

    /// Validate configuration
    Validate,
}

#[derive(Subcommand)]
enum DaemonCommands {
    /// Install as system service
    Install,

    /// Remove system service
    Uninstall,

    /// Start the daemon
    Start,

    /// Stop the daemon
    Stop,

    /// Check daemon status
    Status,
}

#[derive(Subcommand)]
enum AdminCommands {
    /// Create a new user
    Create {
        /// Username for the new user
        #[arg(long)]
        username: String,

        /// Password (or use --generate-password)
        #[arg(long)]
        password: Option<String>,

        /// User role: admin, operator, or viewer
        #[arg(long, default_value = "admin")]
        role: String,

        /// Generate a random password
        #[arg(long)]
        generate_password: bool,
    },

    /// List all users
    List,

    /// Reset a user's password
    ResetPassword {
        /// Username of the user
        #[arg(long)]
        username: String,
    },

    /// Enable a user account
    Enable {
        /// Username of the user
        #[arg(long)]
        username: String,
    },

    /// Disable a user account
    Disable {
        /// Username of the user
        #[arg(long)]
        username: String,
    },

    /// Delete a user
    Delete {
        /// Username of the user to delete
        #[arg(long)]
        username: String,
    },
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let cli = Cli::parse();

    // Setup logging
    let filter = if cli.verbose {
        EnvFilter::new("debug")
    } else {
        EnvFilter::new("info")
    };

    tracing_subscriber::registry()
        .with(fmt::layer().with_target(false))
        .with(filter)
        .init();

    // If no command, show help or run onboard for first-time users
    let command = if let Some(cmd) = cli.command {
        cmd
    } else {
        // Check if this is first run
        let config_exists = openclaw_core::Config::load_default().is_ok();

        if config_exists {
            // Show status by default
            commands::run_status(commands::status::StatusArgs::default()).await?;
            return Ok(());
        }
        // First run - suggest onboarding
        ui::banner();
        ui::info("Welcome to OpenClaw!");
        ui::info("Run 'openclaw onboard' to get started, or 'openclaw --help' for all commands.");
        return Ok(());
    };

    match command {
        Commands::Onboard {
            non_interactive,
            accept_risk,
            flow,
            auth_choice,
            api_key,
            install_daemon,
        } => {
            let args = commands::onboard::OnboardArgs {
                non_interactive,
                accept_risk,
                flow,
                auth_choice,
                api_key,
                install_daemon,
            };
            commands::run_onboard(args).await?;
        }

        Commands::Configure { section } => {
            let args = commands::configure::ConfigureArgs { section };
            commands::run_configure(args).await?;
        }

        Commands::Doctor {
            repair,
            force,
            deep,
        } => {
            let args = commands::doctor::DoctorArgs {
                repair,
                force,
                deep,
            };
            commands::run_doctor(args).await?;
        }

        Commands::Status { all, deep } => {
            let args = commands::status::StatusArgs { all, deep };
            commands::run_status(args).await?;
        }

        Commands::Gateway { action } => {
            let args = match action {
                GatewayCommands::Run { port, bind, force } => commands::gateway::GatewayArgs {
                    action: commands::gateway::GatewayAction::Run { port, bind, force },
                },
                GatewayCommands::Status => commands::gateway::GatewayArgs {
                    action: commands::gateway::GatewayAction::Status,
                },
            };
            commands::run_gateway(args).await?;
        }

        Commands::Channels { list: _, probe } => {
            if probe {
                ui::info("Probing channels...");
                // TODO: Implement channel probing
                ui::warning("Channel probing not yet implemented");
            } else {
                ui::info("Listing channels...");
                // TODO: Implement channel listing
                ui::warning("Channel listing not yet implemented");
            }
        }

        Commands::Config { action } => {
            let args = match action {
                Some(ConfigCommands::Get { key }) => commands::config::ConfigArgs {
                    get: Some(key),
                    set: None,
                    show: false,
                    validate: false,
                },
                Some(ConfigCommands::Set { key, value }) => commands::config::ConfigArgs {
                    get: None,
                    set: Some(format!("{key}={value}")),
                    show: false,
                    validate: false,
                },
                Some(ConfigCommands::Show) => commands::config::ConfigArgs {
                    get: None,
                    set: None,
                    show: true,
                    validate: false,
                },
                Some(ConfigCommands::Validate) => commands::config::ConfigArgs {
                    get: None,
                    set: None,
                    show: false,
                    validate: true,
                },
                None => commands::config::ConfigArgs {
                    get: None,
                    set: None,
                    show: true,
                    validate: false,
                },
            };
            commands::run_config(args).await?;
        }

        Commands::Completion {
            shell,
            install,
            write_state,
        } => {
            let args = commands::completion::CompletionArgs {
                shell,
                install,
                write_state,
            };
            commands::run_completion(args).await?;
        }

        Commands::Daemon { action } => {
            let args = commands::daemon::DaemonArgs {
                action: match action {
                    DaemonCommands::Install => commands::daemon::DaemonAction::Install,
                    DaemonCommands::Uninstall => commands::daemon::DaemonAction::Uninstall,
                    DaemonCommands::Start => commands::daemon::DaemonAction::Start,
                    DaemonCommands::Stop => commands::daemon::DaemonAction::Stop,
                    DaemonCommands::Status => commands::daemon::DaemonAction::Status,
                },
            };
            commands::run_daemon(args).await?;
        }

        Commands::Reset { config_only, all } => {
            ui::header("Reset OpenClaw");

            if all {
                ui::warning("This will delete all OpenClaw data including sessions!");
            } else if config_only {
                ui::warning("This will delete your configuration file.");
            } else {
                ui::info("This will reset configuration and credentials.");
            }

            let confirm = ui::prompts::confirm("Are you sure you want to continue?")?;

            if confirm {
                let state_dir = dirs::home_dir()
                    .map(|h| h.join(".openclaw"))
                    .unwrap_or_default();

                if all {
                    if state_dir.exists() {
                        std::fs::remove_dir_all(&state_dir)?;
                        ui::success("All OpenClaw data deleted");
                    }
                } else if config_only {
                    let config_path = state_dir.join("openclaw.json");
                    if config_path.exists() {
                        std::fs::remove_file(&config_path)?;
                        ui::success("Configuration deleted");
                    }
                } else {
                    let config_path = state_dir.join("openclaw.json");
                    let cred_path = state_dir.join("credentials");

                    if config_path.exists() {
                        std::fs::remove_file(&config_path)?;
                    }
                    if cred_path.exists() {
                        std::fs::remove_dir_all(&cred_path)?;
                    }

                    ui::success("Configuration and credentials deleted");
                }

                ui::info("Run 'openclaw onboard' to set up again");
            } else {
                ui::info("Reset cancelled");
            }
        }

        Commands::Admin { action, data_dir } => {
            let args = commands::admin::AdminArgs {
                action: match action {
                    AdminCommands::Create {
                        username,
                        password,
                        role,
                        generate_password,
                    } => commands::admin::AdminAction::Create {
                        username,
                        password,
                        role,
                        generate_password,
                    },
                    AdminCommands::List => commands::admin::AdminAction::List,
                    AdminCommands::ResetPassword { username } => {
                        commands::admin::AdminAction::ResetPassword { username }
                    }
                    AdminCommands::Enable { username } => {
                        commands::admin::AdminAction::Enable { username }
                    }
                    AdminCommands::Disable { username } => {
                        commands::admin::AdminAction::Disable { username }
                    }
                    AdminCommands::Delete { username } => {
                        commands::admin::AdminAction::Delete { username }
                    }
                },
                data_dir,
            };
            commands::run_admin(args).await?;
        }
    }

    Ok(())
}