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(Command::new(Subcommand::Publish).about("Publish a Rocal app"))
39 .subcommand(
40 Command::new(Subcommand::Password)
41 .about("Password settings")
42 .arg_required_else_help(true)
43 .subcommand(Command::new(PasswordSubcommand::Reset).about("Reset your password"))
44 )
45 .subcommand(
46 Command::new(Subcommand::SyncServers)
47 .about("Manage sync servers")
48 .arg_required_else_help(true)
49 .subcommand(Command::new(SyncServersSubcommand::List).about("List available sync servers and show app_id"))
50 )
51 .about("A tool to create and build a Rocal app.")
52 .arg_required_else_help(true)
53 .get_matches();
54
55 match matches.subcommand() {
56 Some((name, arg_matches)) => {
57 if name == Subcommand::New.as_str() {
58 if let Some(name) = arg_matches.get_one::<String>(InitCommandArg::Name.as_str()) {
59 init(name);
60 }
61 } else if name == Subcommand::Build.as_str() {
62 build();
63 } else if name == Subcommand::Publish.as_str() {
64 publish().await;
65 } else if name == Subcommand::Register.as_str() {
66 register().await;
67 } else if name == Subcommand::Login.as_str() {
68 login().await;
69 } else if name == Subcommand::Password.as_str() {
70 match arg_matches.subcommand() {
71 Some((name, _arg_matches)) => {
72 if name == PasswordSubcommand::Reset.as_str() {
73 password::reset().await;
74 }
75 }
76 None => (),
77 }
78 } else if name == Subcommand::Subscribe.as_str() {
79 if let Err(err) = subscribe().await {
80 println!("Error: {}", err.to_string());
81 }
82 } else if name == Subcommand::Unsubscribe.as_str() {
83 unsubscribe().await;
84 } else if name == Subcommand::SyncServers.as_str() {
85 match arg_matches.subcommand() {
86 Some((name, _arg_matches)) => {
87 if name == SyncServersSubcommand::List.as_str() {
88 sync_servers::list().await;
89 }
90 }
91 None => (),
92 }
93 }
94 }
95 None => (),
96 }
97}
98
99enum Subcommand {
100 Register,
101 Login,
102 Subscribe,
103 Unsubscribe,
104 New,
105 Build,
106 Publish,
107 Password,
108 SyncServers,
109}
110
111enum PasswordSubcommand {
112 Reset,
113}
114
115enum InitCommandArg {
116 Name,
117}
118
119enum SyncServersSubcommand {
120 List,
121}
122
123impl Into<Str> for Subcommand {
124 fn into(self) -> Str {
125 self.as_str().into()
126 }
127}
128
129impl Subcommand {
130 pub fn as_str(self) -> &'static str {
131 match self {
132 Subcommand::Register => "register",
133 Subcommand::Login => "login",
134 Subcommand::Subscribe => "subscribe",
135 Subcommand::Unsubscribe => "unsubscribe",
136 Subcommand::New => "new",
137 Subcommand::Build => "build",
138 Subcommand::Publish => "publish",
139 Subcommand::Password => "password",
140 Subcommand::SyncServers => "sync-servers",
141 }
142 }
143}
144
145impl Into<Id> for InitCommandArg {
146 fn into(self) -> Id {
147 self.as_str().into()
148 }
149}
150
151impl InitCommandArg {
152 pub fn as_str(self) -> &'static str {
153 match self {
154 InitCommandArg::Name => "name",
155 }
156 }
157}
158
159impl Into<Str> for PasswordSubcommand {
160 fn into(self) -> Str {
161 self.as_str().into()
162 }
163}
164
165impl PasswordSubcommand {
166 pub fn as_str(self) -> &'static str {
167 match self {
168 PasswordSubcommand::Reset => "reset",
169 }
170 }
171}
172
173impl Into<Str> for SyncServersSubcommand {
174 fn into(self) -> Str {
175 self.as_str().into()
176 }
177}
178
179impl SyncServersSubcommand {
180 pub fn as_str(self) -> &'static str {
181 match self {
182 SyncServersSubcommand::List => "ls",
183 }
184 }
185}