Skip to main content

twc_rs/
cli.rs

1// SPDX-FileCopyrightText: 2026 RAprogramm <andrey.rozanov.vl@gmail.com>
2// SPDX-License-Identifier: MIT
3
4use clap::{
5    Parser, Subcommand, ValueEnum,
6    builder::styling::{AnsiColor, Effects, Styles}
7};
8
9/// ANSI color scheme applied to every `--help` screen and error message.
10const HELP_STYLES: Styles = Styles::styled()
11    .header(AnsiColor::Yellow.on_default().effects(Effects::BOLD))
12    .usage(AnsiColor::Yellow.on_default().effects(Effects::BOLD))
13    .literal(AnsiColor::Cyan.on_default().effects(Effects::BOLD))
14    .placeholder(AnsiColor::Green.on_default())
15    .error(AnsiColor::Red.on_default().effects(Effects::BOLD))
16    .valid(AnsiColor::Green.on_default())
17    .invalid(AnsiColor::Red.on_default());
18
19mod apps;
20mod balancers;
21pub mod completers;
22mod databases;
23mod domains;
24mod firewall;
25mod images;
26mod kubernetes;
27mod network;
28mod projects;
29mod registry;
30mod s3;
31mod servers;
32mod settings;
33mod ssh;
34
35pub use apps::AppsCommands;
36pub use balancers::BalancerCommands;
37pub use databases::DatabaseCommands;
38pub use domains::DomainCommands;
39pub use firewall::FirewallCommands;
40pub use images::ImageCommands;
41pub use kubernetes::KubernetesCommands;
42pub use network::{IpCommands, VpcCommands};
43pub use projects::ProjectCommands;
44pub use registry::RegistryCommands;
45pub use s3::S3Commands;
46pub use servers::ServerCommands;
47pub use settings::{AccountCommands, AuthCommands, ConfigCommands};
48pub use ssh::SshCommands;
49
50/// UI language selectable on the command line.
51#[derive(ValueEnum, Clone, Copy, Debug)]
52pub enum LangArg {
53    /// English.
54    En,
55    /// Russian.
56    Ru
57}
58
59/// Shell to generate a completion script for.
60#[derive(ValueEnum, Clone, Copy, Debug)]
61pub enum ShellArg {
62    /// Bash.
63    Bash,
64    /// Zsh.
65    Zsh,
66    /// Fish.
67    Fish,
68    /// PowerShell.
69    Powershell,
70    /// Elvish.
71    Elvish,
72    /// Nushell.
73    Nushell
74}
75
76/// Professional CLI tool for managing Timeweb Cloud infrastructure.
77#[derive(Parser, Debug)]
78#[command(
79    name = "twc-rs",
80    version,
81    about = "Timeweb Cloud CLI — servers, databases, S3, Kubernetes, apps and more",
82    styles = HELP_STYLES
83)]
84pub struct Cli {
85    /// Output format: table (default), json, yaml, or quiet.
86    #[arg(
87        short,
88        long,
89        global = true,
90        default_value = "table",
91        env = "TWC_OUTPUT",
92        display_order = 900
93    )]
94    pub format: String,
95
96    /// API token override (overrides config file and `TWC_TOKEN` env).
97    #[arg(short, long, global = true, env = "TWC_TOKEN", display_order = 901)]
98    pub token: Option<String>,
99
100    /// Use a named profile's token from the config file.
101    #[arg(long, global = true, env = "TWC_PROFILE", display_order = 902)]
102    pub profile: Option<String>,
103
104    #[command(subcommand)]
105    pub command: Commands
106}
107
108/// Available top-level commands.
109#[derive(Subcommand, Debug)]
110pub enum Commands {
111    /// Manage cloud servers.
112    #[command(subcommand)]
113    Server(ServerCommands),
114
115    /// Manage SSH keys.
116    #[command(subcommand)]
117    Ssh(SshCommands),
118
119    /// Manage projects.
120    #[command(subcommand)]
121    Project(ProjectCommands),
122
123    /// Manage databases.
124    #[command(subcommand)]
125    Database(DatabaseCommands),
126
127    /// Manage S3 storages.
128    #[command(subcommand)]
129    S3(S3Commands),
130
131    /// Manage Kubernetes clusters.
132    #[command(subcommand)]
133    Kubernetes(KubernetesCommands),
134
135    /// Manage container registries.
136    #[command(subcommand)]
137    Registry(RegistryCommands),
138
139    /// Manage load balancers.
140    #[command(subcommand)]
141    Balancer(BalancerCommands),
142
143    /// Manage domains.
144    #[command(subcommand)]
145    Domain(DomainCommands),
146
147    /// Manage firewall groups.
148    #[command(subcommand)]
149    Firewall(FirewallCommands),
150
151    /// Manage cloud apps.
152    #[command(subcommand)]
153    Apps(AppsCommands),
154
155    /// Manage disk images.
156    #[command(subcommand)]
157    Image(ImageCommands),
158
159    /// Manage floating IPs.
160    #[command(subcommand)]
161    Ip(IpCommands),
162
163    /// Manage virtual networks (VPC).
164    #[command(subcommand)]
165    Vpc(VpcCommands),
166
167    /// Show account information.
168    #[command(subcommand)]
169    Account(AccountCommands),
170
171    /// Configure twc-rs settings.
172    #[command(subcommand)]
173    Config(ConfigCommands),
174
175    /// Authenticate with Timeweb Cloud (guided browser flow).
176    #[command(subcommand)]
177    Auth(AuthCommands),
178
179    /// Open the interactive dashboard.
180    Dashboard {
181        /// Refresh interval in seconds.
182        #[arg(short, long, default_value_t = 5)]
183        interval: u64
184    },
185
186    /// Generate a shell completion script (print to stdout).
187    Completions {
188        /// Target shell.
189        #[arg(value_enum)]
190        shell: ShellArg
191    },
192
193    /// Check the local installation for conflicting copies in PATH.
194    Doctor,
195
196    /// Check for a newer release and update via the detected install channel.
197    Update {
198        /// Only report the versions and the update command; install nothing.
199        #[arg(long)]
200        check: bool
201    }
202}
203
204#[cfg(test)]
205mod tests;