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
// Copyright (C) 2026 Ordinary Labs, LLC.
//
// SPDX-License-Identifier: AGPL-3.0-only
use crate::cmds::accounts::get_current_account;
use clap::Subcommand;
use clio::ClioPath;
use ordinary_api::client::OrdinaryApiClient;
use serde_json::Value;
#[derive(Clone, Debug)]
pub enum LogFormat {
/// all logs that match query
All,
/// top logs that match query
Top,
/// count of logs that match query
Count,
}
impl LogFormat {
fn as_str(&self) -> &'static str {
match self {
Self::All => "all",
Self::Top => "top",
Self::Count => "count",
}
}
}
impl clap::ValueEnum for LogFormat {
fn value_variants<'a>() -> &'a [Self] {
&[Self::All, Self::Top, Self::Count]
}
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
match self {
Self::All => Some(clap::builder::PossibleValue::new("all")),
Self::Top => Some(clap::builder::PossibleValue::new("top")),
Self::Count => Some(clap::builder::PossibleValue::new("count")),
}
}
}
#[derive(Subcommand, Debug)]
pub enum App {
/// deploy a new application
Deploy,
/// push a configuration level change
Patch,
/// push a configuration change which will
/// modify the shape of your data stores
///
/// (i.e. a structural change to model or content definitions)
Migrate,
// /// request the current host "bridge" with an additional
// /// `ordinaryd` instance, in order to run your application
// /// across two hosts.
// Bridge {
// /// url for another `ordinaryd` instance
// remote: String,
// },
/// kill a running instance of the application
Kill,
/// restart a running instance of the application
Restart,
// /// fully erase all content of the application from the host
// Erase,
/// download an application as static files
Download {
/// url override. will use project domain by default
#[arg(short, long)]
url: Option<String>,
/// where to store downloaded files
#[arg(short, long, value_parser = clap::value_parser!(ClioPath).exists().is_dir(), default_value = "out")]
out: ClioPath,
},
/// query application logs
Logs {
/// format
format: LogFormat,
/// [reference](https://quickwit.io/docs/reference/query-language)
query: String,
#[arg(long)]
/// limit (when using 'top' format)
limit: Option<usize>,
/// for applications that need to consume stdio or pipe to `jq`
#[arg(long, default_value_t = false)]
json: bool,
},
/// manage application accounts
Accounts {
#[command(subcommand)]
accounts: Accounts,
},
}
#[derive(Subcommand, Debug)]
pub enum Accounts {
/// list application accounts
List {
/// for applications that need to consume stdio or pipe to `jq`
#[arg(long, default_value_t = false)]
json: bool,
},
}
impl App {
#[allow(clippy::missing_panics_doc)]
pub async fn handle(
&self,
api_domain: Option<&str>,
accept_invalid_certs: bool,
project: &str,
insecure: bool,
) -> anyhow::Result<()> {
let account = get_current_account(insecure)?;
let client = OrdinaryApiClient::new(
&account.host,
&account.name,
api_domain,
accept_invalid_certs,
crate::USER_AGENT,
)?;
match self {
Self::Deploy => {
let port = client.deploy(project).await?;
tracing::info!("running on port: {port}");
}
Self::Patch => {
let port = client.patch(project).await?;
tracing::info!("running on port: {port}");
}
Self::Migrate => {
println!("todo: migrate");
}
// Self::Bridge { remote } => {
// tracing::info!("todo: bridge {project} to {remote}");
// }
Self::Kill => {
client.kill(project).await?;
}
Self::Restart => {
let port = client.restart(project).await?;
tracing::info!("running on port: {port}");
}
// Self::Erase => {
// tracing::info!("todo: erase {project}");
// }
Self::Download { url, out } => {
let out = out.to_str().expect("failed to convert to string");
if let Some(url) = url {
ordinary_download::download(project, url, out).await?;
} else {
ordinary_download::download(
project,
&format!("https://{}", account.project),
out,
)
.await?;
}
}
Self::Logs {
format,
query,
limit,
json,
} => {
let res = client
.app_logs(project, query, format.as_str(), limit)
.await?;
if matches!(format.as_str(), "all" | "top") {
if json == &true {
print!("{res}");
} else {
match res {
Value::Array(lines) => {
for line in lines {
tracing::info!(%line);
}
}
_ => unreachable!(),
}
}
} else if json == &true {
print!("{res}");
} else {
match res {
Value::Number(count) => {
tracing::info!(count = %count);
}
_ => unreachable!(),
}
}
}
Self::Accounts { accounts } => match accounts {
Accounts::List { json } => {
let res = client.app_accounts_list(project).await?;
if json == &true {
print!("{res}");
} else {
let accounts: Vec<Value> = serde_json::from_str(&res)?;
for account in accounts {
tracing::info!(%account);
}
}
}
},
}
Ok(())
}
}