Skip to content

Commit 4702085

Browse files
authored
Redefine CLI subcommands (#220)
Subcommands are moved to top level of cli and can be specified with `--s3` and `--drive`.
1 parent 780e141 commit 4702085

File tree

11 files changed

+82
-146
lines changed

11 files changed

+82
-146
lines changed

server/src/banner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub fn system_info() {
3636
)
3737
}
3838

39+
#[allow(dead_code)]
3940
pub fn warning_line() {
4041
eprint!(
4142
"

server/src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use std::sync::RwLock;
3636
use crate::metadata;
3737
use crate::metadata::LOCK_EXPECT;
3838
use crate::option::CONFIG;
39-
use crate::storage::{ObjectStorageProvider, StorageDir};
39+
use crate::storage::StorageDir;
4040

4141
use self::error::{EventError, StreamWriterError};
4242

server/src/handlers/event.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use crate::event;
2525
use crate::option::CONFIG;
2626
use crate::query::Query;
2727
use crate::response::QueryResponse;
28-
use crate::storage::ObjectStorageProvider;
2928
use crate::utils::header_parsing::{collect_labelled_headers, ParseHeaderError};
3029
use crate::utils::{self, flatten_json_body, merge};
3130

server/src/handlers/logstream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use serde_json::Value;
2525

2626
use crate::alerts::Alerts;
2727
use crate::option::CONFIG;
28-
use crate::storage::{ObjectStorageProvider, StorageDir};
28+
use crate::storage::StorageDir;
2929
use crate::{event, response};
3030
use crate::{metadata, validator};
3131

server/src/handlers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub mod logstream;
2222
use actix_web::http::StatusCode;
2323
use actix_web::HttpResponse;
2424

25-
use crate::{option::CONFIG, storage::ObjectStorageProvider};
25+
use crate::option::CONFIG;
2626

2727
pub async fn liveness() -> HttpResponse {
2828
HttpResponse::new(StatusCode::OK)

server/src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ mod validator;
5454

5555
use option::CONFIG;
5656

57-
use crate::storage::ObjectStorageProvider;
58-
5957
// Global configurations
6058
const MAX_EVENT_PAYLOAD_SIZE: usize = 1024000;
6159
const API_BASE_PATH: &str = "/api";

server/src/option.rs

Lines changed: 53 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
*
1717
*/
1818

19-
use clap::builder::ArgPredicate;
2019
use clap::{Parser, Subcommand};
2120
use crossterm::style::Stylize;
22-
use std::path::PathBuf;
21+
use std::path::{Path, PathBuf};
2322
use std::sync::Arc;
2423

2524
use crate::banner;
@@ -35,29 +34,31 @@ lazy_static::lazy_static! {
3534

3635
pub const USERNAME_ENV: &str = "P_USERNAME";
3736
pub const PASSWORD_ENV: &str = "P_PASSWORD";
38-
pub const DEFAULT_USERNAME: &str = "parseable";
39-
pub const DEFAULT_PASSWORD: &str = "parseable";
4037

4138
pub struct Config {
4239
pub parseable: Server,
40+
storage: Arc<dyn ObjectStorageProvider + Send + Sync>,
4341
}
4442

4543
impl Config {
4644
fn new() -> Self {
47-
let Cli::Server(args) = match Cli::try_parse() {
48-
Ok(s) => s,
49-
Err(e) => {
50-
e.exit();
51-
}
52-
};
53-
Config { parseable: args }
45+
let cli = Cli::parse();
46+
match cli.command {
47+
SubCmd::ServerS3 { server, storage } => Config {
48+
parseable: server,
49+
storage: Arc::new(storage),
50+
},
51+
SubCmd::ServerDrive { server, storage } => Config {
52+
parseable: server,
53+
storage: Arc::new(storage),
54+
},
55+
}
5456
}
5557

5658
pub fn print(&self) {
5759
let scheme = CONFIG.parseable.get_scheme();
5860
self.status_info(&scheme);
5961
banner::version::print();
60-
self.demo();
6162
self.storage_info();
6263
banner::system_info();
6364
println!();
@@ -107,30 +108,23 @@ impl Config {
107108
Local Data Path: {}
108109
Object Storage: {}",
109110
"Storage:".to_string().blue().bold(),
110-
self.parseable.local_disk_path.to_string_lossy(),
111-
self.parseable.object_store.get_endpoint(),
111+
self.staging_dir().to_string_lossy(),
112+
self.storage().get_endpoint(),
112113
)
113114
}
114115

115-
fn demo(&self) {
116-
if self.is_demo() {
117-
banner::warning_line();
118-
eprintln!(
119-
"
120-
{}",
121-
"Parseable is in demo mode with default credentials and open object store. Please use this for demo purposes only."
122-
.to_string()
123-
.red(),
124-
)
125-
}
116+
pub fn storage(&self) -> Arc<dyn ObjectStorageProvider + Send + Sync> {
117+
self.storage.clone()
126118
}
127119

128-
fn is_demo(&self) -> bool {
129-
self.parseable.demo
120+
pub fn staging_dir(&self) -> &Path {
121+
&self.parseable.local_staging_path
130122
}
123+
}
131124

132-
pub fn storage(&self) -> &impl ObjectStorageProvider {
133-
&self.parseable.object_store
125+
impl Default for Config {
126+
fn default() -> Self {
127+
Self::new()
134128
}
135129
}
136130

@@ -141,8 +135,27 @@ impl Config {
141135
about = "Parseable is a log storage and observability platform.",
142136
version
143137
)]
144-
enum Cli {
145-
Server(Server),
138+
struct Cli {
139+
#[command(subcommand)]
140+
command: SubCmd,
141+
}
142+
143+
#[derive(Subcommand, Clone)]
144+
enum SubCmd {
145+
#[command(name = "--s3")]
146+
ServerS3 {
147+
#[command(flatten)]
148+
server: Server,
149+
#[command(flatten)]
150+
storage: S3Config,
151+
},
152+
#[command(name = "--drive")]
153+
ServerDrive {
154+
#[command(flatten)]
155+
server: Server,
156+
#[command(flatten)]
157+
storage: FSConfig,
158+
},
146159
}
147160

148161
#[derive(clap::Args, Debug, Clone)]
@@ -175,19 +188,18 @@ pub struct Server {
175188
)]
176189
pub address: String,
177190

178-
/// The local storage path is used as temporary landing point
179-
/// for incoming events and local cache while querying data pulled
180-
/// from object storage backend
191+
/// The local staging path is used as a temporary landing point
192+
/// for incoming events and local cache
181193
#[arg(
182194
long,
183-
env = "P_LOCAL_STORAGE",
195+
env = "P_STAGING_DIR",
184196
default_value = "./data",
185197
value_name = "path"
186198
)]
187-
pub local_disk_path: PathBuf,
199+
pub local_staging_path: PathBuf,
188200

189-
/// Optional interval after which server would upload uncommited data to
190-
/// remote object storage platform. Defaults to 1min.
201+
/// Interval in seconds after which uncommited data would be
202+
/// uploaded to the storage platform.
191203
#[arg(
192204
long,
193205
env = "P_STORAGE_UPLOAD_INTERVAL",
@@ -196,64 +208,26 @@ pub struct Server {
196208
)]
197209
pub upload_interval: u64,
198210

199-
/// Optional username to enable basic auth on the server
211+
/// Username for the basic authentication on the server
200212
#[arg(
201213
long,
202214
env = USERNAME_ENV,
203215
value_name = "username",
204-
default_value_if("demo", ArgPredicate::IsPresent, DEFAULT_USERNAME)
205216
)]
206217
pub username: String,
207218

208-
/// Optional password to enable basic auth on the server
219+
/// Password for the basic authentication on the server
209220
#[arg(
210221
long,
211222
env = PASSWORD_ENV,
212223
value_name = "password",
213-
default_value_if("demo", ArgPredicate::IsPresent, DEFAULT_PASSWORD)
214224
)]
215225
pub password: String,
216-
217-
#[command(subcommand)]
218-
pub object_store: ObjectStore,
219-
220-
/// Run Parseable in demo mode with default credentials and open object store
221-
#[arg(short, long, exclusive = true)]
222-
pub demo: bool,
223-
}
224-
225-
#[derive(Debug, Clone, Subcommand)]
226-
pub enum ObjectStore {
227-
Drive(FSConfig),
228-
S3(S3Config),
229-
}
230-
231-
impl ObjectStorageProvider for ObjectStore {
232-
fn get_datafusion_runtime(&self) -> Arc<datafusion::execution::runtime_env::RuntimeEnv> {
233-
match self {
234-
ObjectStore::Drive(x) => x.get_datafusion_runtime(),
235-
ObjectStore::S3(x) => x.get_datafusion_runtime(),
236-
}
237-
}
238-
239-
fn get_object_store(&self) -> Arc<dyn ObjectStorage + Send> {
240-
match self {
241-
ObjectStore::Drive(x) => x.get_object_store(),
242-
ObjectStore::S3(x) => x.get_object_store(),
243-
}
244-
}
245-
246-
fn get_endpoint(&self) -> String {
247-
match self {
248-
ObjectStore::Drive(x) => x.get_endpoint(),
249-
ObjectStore::S3(x) => x.get_endpoint(),
250-
}
251-
}
252226
}
253227

254228
impl Server {
255229
pub fn local_stream_data_path(&self, stream_name: &str) -> PathBuf {
256-
self.local_disk_path.join(stream_name)
230+
self.local_staging_path.join(stream_name)
257231
}
258232

259233
pub fn get_scheme(&self) -> String {

server/src/query.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ use std::path::PathBuf;
3030
use std::sync::Arc;
3131

3232
use crate::option::CONFIG;
33-
use crate::storage::ObjectStorage;
3433
use crate::storage::ObjectStorageError;
3534
use crate::storage::StorageDir;
36-
use crate::storage::{self, ObjectStorageProvider};
35+
use crate::storage::{ObjectStorage, OBJECT_STORE_DATA_GRANULARITY};
3736
use crate::utils::TimePeriod;
3837
use crate::validator;
3938

@@ -68,7 +67,7 @@ impl Query {
6867

6968
/// Return prefixes, each per day/hour/minutes as necessary
7069
pub fn get_prefixes(&self) -> Vec<String> {
71-
TimePeriod::new(self.start, self.end, storage::OBJECT_STORE_DATA_GRANULARITY)
70+
TimePeriod::new(self.start, self.end, OBJECT_STORE_DATA_GRANULARITY)
7271
.generate_prefixes(&self.stream_name)
7372
}
7473

server/src/storage/localfs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use super::{LogStream, ObjectStorage, ObjectStorageError, ObjectStorageProvider}
4646
about = "configuration for using local filesystem for storage"
4747
)]
4848
pub struct FSConfig {
49-
#[arg(long, env = "P_FS_PATH", value_name = "path")]
49+
#[arg(env = "P_FS_PATH", value_name = "filesystem path")]
5050
root: PathBuf,
5151
}
5252

server/src/storage/object_storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub trait ObjectStorage: Sync + 'static {
145145
}
146146

147147
async fn sync(&self) -> Result<(), MoveDataError> {
148-
if !Path::new(&CONFIG.parseable.local_disk_path).exists() {
148+
if !Path::new(&CONFIG.staging_dir()).exists() {
149149
return Ok(());
150150
}
151151

0 commit comments

Comments
 (0)