1use clap::{builder::Str, command, Arg, Command, Id};
2
3use crate::commands::{
4 build::build, init::init, login::login, password, publish::publish, register::register,
5 subscribe::subscribe, sync_servers, unsubscribe::unsubscribe,
6};
7
8pub async fn run() {
9 let matches = command!()
10 .subcommand(
11 Command::new(Subcommand::Register)
12 .about("Create a new account in Rocal platform")
13 )
14 .subcommand(
15 Command::new(Subcommand::Login)
16 .about("Login to Rocal platform")
17 )
18 .subcommand(
19 Command::new(Subcommand::Subscribe)
20 .about("Subscribe Rocal platform to publish a Rocal app")
21 )
22 .subcommand(
23 Command::new(Subcommand::Unsubscribe)
24 .about("Unsubscribe Rocal platform which leads to revoke tokens and shut your hosting app down")
25 )
26 .subcommand(
27 Command::new(Subcommand::New)
28 .about("Create a new Rocal app")
29 .arg(
30 Arg::new(InitCommandArg::Name)
31 .short('n')
32 .long("name")
33 .required(true)
34 .help("Set the resulting package name"),
35 ),
36 )
37 .subcommand(Command::new(Subcommand::Build).about("Build a Rocal app"))
38 .subcommand(
39 Command::new(Subcommand::Run)
40 .about("Run a Rocal app on your local")
41 .arg(
42 Arg::new(RunCommandArg::Port)
43 .short('p')
44 .long("port")
45 .required(false)
46 .help("Set port where you want to serve an app")
47 )
48 )
49 .subcommand(Command::new(Subcommand::Publish).about("Publish a Rocal app"))
50 .subcommand(
51 Command::new(Subcommand::Password)
52 .about("Password settings")
53 .arg_required_else_help(true)
54 .subcommand(Command::new(PasswordSubcommand::Reset).about("Reset your password"))
55 )
56 .subcommand(
57 Command::new(Subcommand::SyncServers)
58 .about("Manage sync servers")
59 .arg_required_else_help(true)
60 .subcommand(Command::new(SyncServersSubcommand::List).about("List available sync servers and show app_id"))
61 )
62 .about("A tool to create and build a Rocal app.")
63 .arg_required_else_help(true)
64 .get_matches();
65
66 match matches.subcommand() {
67 Some((name, arg_matches)) => {
68 if name == Subcommand::New.as_str() {
69 if let Some(name) = arg_matches.get_one::<String>(InitCommandArg::Name.as_str()) {
70 init(name);
71 }
72 } else if name == Subcommand::Build.as_str() {
73 build();
74 } else if name == Subcommand::Publish.as_str() {
75 publish().await;
76 } else if name == Subcommand::Register.as_str() {
77 register().await;
78 } else if name == Subcommand::Login.as_str() {
79 login().await;
80 } else if name == Subcommand::Password.as_str() {
81 match arg_matches.subcommand() {
82 Some((name, _arg_matches)) => {
83 if name == PasswordSubcommand::Reset.as_str() {
84 password::reset().await;
85 }
86 }
87 None => (),
88 }
89 } else if name == Subcommand::Subscribe.as_str() {
90 if let Err(err) = subscribe().await {
91 println!("Error: {}", err.to_string());
92 }
93 } else if name == Subcommand::Unsubscribe.as_str() {
94 unsubscribe().await;
95 } else if name == Subcommand::SyncServers.as_str() {
96 match arg_matches.subcommand() {
97 Some((name, _arg_matches)) => {
98 if name == SyncServersSubcommand::List.as_str() {
99 sync_servers::list().await;
100 }
101 }
102 None => (),
103 }
104 } else if name == Subcommand::Run.as_str() {
105 build();
106 if let Some(port) = arg_matches.get_one::<String>(RunCommandArg::Port.as_str()) {
107 rocal_dev_server::run(Some(&port));
108 } else {
109 rocal_dev_server::run(None);
110 }
111 }
112 }
113 None => (),
114 }
115}
116
117enum Subcommand {
118 Register,
119 Login,
120 Subscribe,
121 Unsubscribe,
122 New,
123 Build,
124 Publish,
125 Password,
126 SyncServers,
127 Run,
128}
129
130enum PasswordSubcommand {
131 Reset,
132}
133
134enum InitCommandArg {
135 Name,
136}
137
138enum SyncServersSubcommand {
139 List,
140}
141
142enum RunCommandArg {
143 Port,
144}
145
146impl Into<Str> for Subcommand {
147 fn into(self) -> Str {
148 self.as_str().into()
149 }
150}
151
152impl Subcommand {
153 pub fn as_str(self) -> &'static str {
154 match self {
155 Subcommand::Register => "register",
156 Subcommand::Login => "login",
157 Subcommand::Subscribe => "subscribe",
158 Subcommand::Unsubscribe => "unsubscribe",
159 Subcommand::New => "new",
160 Subcommand::Build => "build",
161 Subcommand::Publish => "publish",
162 Subcommand::Password => "password",
163 Subcommand::SyncServers => "sync-servers",
164 Subcommand::Run => "run",
165 }
166 }
167}
168
169impl Into<Id> for InitCommandArg {
170 fn into(self) -> Id {
171 self.as_str().into()
172 }
173}
174
175impl InitCommandArg {
176 pub fn as_str(self) -> &'static str {
177 match self {
178 InitCommandArg::Name => "name",
179 }
180 }
181}
182
183impl Into<Str> for PasswordSubcommand {
184 fn into(self) -> Str {
185 self.as_str().into()
186 }
187}
188
189impl PasswordSubcommand {
190 pub fn as_str(self) -> &'static str {
191 match self {
192 PasswordSubcommand::Reset => "reset",
193 }
194 }
195}
196
197impl Into<Str> for SyncServersSubcommand {
198 fn into(self) -> Str {
199 self.as_str().into()
200 }
201}
202
203impl SyncServersSubcommand {
204 pub fn as_str(self) -> &'static str {
205 match self {
206 SyncServersSubcommand::List => "ls",
207 }
208 }
209}
210
211impl Into<Id> for RunCommandArg {
212 fn into(self) -> Id {
213 self.as_str().into()
214 }
215}
216
217impl RunCommandArg {
218 pub fn as_str(self) -> &'static str {
219 match self {
220 RunCommandArg::Port => "port",
221 }
222 }
223}