Skip to content

Commit 974b03b

Browse files
authored
Banner fix (#236)
1 parent aec0c08 commit 974b03b

File tree

4 files changed

+93
-55
lines changed

4 files changed

+93
-55
lines changed

server/src/banner.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,52 @@
2020
use crossterm::style::Stylize;
2121
use sysinfo::{System, SystemExt};
2222

23+
use crate::{option::Config, utils::capitalize_ascii};
24+
25+
pub fn print(config: &Config) {
26+
let scheme = config.parseable.get_scheme();
27+
status_info(config, &scheme);
28+
version::print();
29+
storage_info(config);
30+
system_info();
31+
println!();
32+
}
33+
34+
fn status_info(config: &Config, scheme: &str) {
35+
let url = format!("{}://{}", scheme, config.parseable.address).underlined();
36+
eprintln!(
37+
"
38+
{}
39+
{}
40+
{}",
41+
format!("Parseable server started at: {}", url).bold(),
42+
format!("Username: {}", config.parseable.username).bold(),
43+
format!("Password: {}", config.parseable.password).bold(),
44+
);
45+
46+
if config.is_default_creds() {
47+
warning_line();
48+
eprintln!(
49+
"
50+
{}",
51+
"Using default credentials for Parseable server".red()
52+
)
53+
}
54+
}
55+
56+
fn storage_info(config: &Config) {
57+
eprintln!(
58+
"
59+
{}
60+
Local Staging Path: {}
61+
{} Storage: {}",
62+
"Storage:".to_string().blue().bold(),
63+
config.staging_dir().to_string_lossy(),
64+
capitalize_ascii(config.storage_name),
65+
config.storage().get_endpoint(),
66+
)
67+
}
68+
2369
pub fn system_info() {
2470
let system = System::new_all();
2571
eprintln!(
@@ -36,7 +82,6 @@ pub fn system_info() {
3682
)
3783
}
3884

39-
#[allow(dead_code)]
4085
pub fn warning_line() {
4186
eprint!(
4287
"
@@ -81,13 +126,10 @@ pub mod version {
81126
current_version
82127
);
83128

84-
// check for latest release, if it cannot be fetched then print error as warn and return
85-
let latest_release = match update::get_latest() {
86-
Ok(latest_release) => latest_release,
87-
Err(e) => {
88-
log::warn!("{}", e);
89-
return;
90-
}
129+
// check for latest release
130+
let Ok(latest_release) = update::get_latest() else {
131+
eprintln!();
132+
return
91133
};
92134

93135
if latest_release.version > current_version {
@@ -109,7 +151,7 @@ pub mod version {
109151
}
110152
}
111153
ParseableVersion::Prerelease(current_prerelease) => {
112-
eprint!(
154+
eprintln!(
113155
"
114156
{} {} ",
115157
"Current Version:".to_string().blue().bold(),

server/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const API_VERSION: &str = "v1";
6464
#[actix_web::main]
6565
async fn main() -> anyhow::Result<()> {
6666
env_logger::init();
67-
CONFIG.print();
67+
banner::print(&CONFIG);
6868
CONFIG.validate();
6969
let storage = CONFIG.storage().get_object_store();
7070
CONFIG.validate_storage(&*storage).await;

server/src/option.rs

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
*
1717
*/
1818

19+
use clap::error::ErrorKind;
1920
use clap::{command, value_parser, Arg, Args, Command, FromArgMatches};
20-
use crossterm::style::Stylize;
21+
2122
use std::path::{Path, PathBuf};
2223
use std::sync::Arc;
2324

24-
use crate::banner;
2525
use crate::storage::{
2626
FSConfig, ObjectStorage, ObjectStorageError, ObjectStorageProvider, S3Config,
2727
LOCAL_SYNC_INTERVAL,
2828
};
29-
use crate::utils::capitalize_ascii;
3029

3130
lazy_static::lazy_static! {
3231
#[derive(Debug)]
@@ -54,6 +53,15 @@ impl Config {
5453
Err(err) => err.exit(),
5554
};
5655

56+
if server.local_staging_path == storage.root {
57+
parseable_cli_command()
58+
.error(
59+
ErrorKind::ValueValidation,
60+
"Cannot use same path for storage and staging",
61+
)
62+
.exit()
63+
}
64+
5765
Config {
5866
parseable: server,
5967
storage: Arc::new(storage),
@@ -80,15 +88,6 @@ impl Config {
8088
}
8189
}
8290

83-
pub fn print(&self) {
84-
let scheme = CONFIG.parseable.get_scheme();
85-
self.status_info(&scheme);
86-
banner::version::print();
87-
self.storage_info();
88-
banner::system_info();
89-
println!();
90-
}
91-
9291
pub fn validate(&self) {
9392
if CONFIG.parseable.upload_interval < LOCAL_SYNC_INTERVAL {
9493
panic!("object storage upload_interval (P_STORAGE_UPLOAD_INTERVAL) must be 60 seconds or more");
@@ -113,39 +112,18 @@ impl Config {
113112
}
114113
}
115114

116-
fn status_info(&self, scheme: &str) {
117-
let url = format!("{}://{}", scheme, self.parseable.address).underlined();
118-
eprintln!(
119-
"
120-
{}
121-
{}
122-
{}",
123-
format!("Parseable server started at: {}", url).bold(),
124-
format!("Username: {}", self.parseable.username).bold(),
125-
format!("Password: {}", self.parseable.password).bold(),
126-
)
127-
}
128-
129-
fn storage_info(&self) {
130-
eprintln!(
131-
"
132-
{}
133-
Local Staging Path: {}
134-
{} Storage: {}",
135-
"Storage:".to_string().blue().bold(),
136-
self.staging_dir().to_string_lossy(),
137-
capitalize_ascii(self.storage_name),
138-
self.storage().get_endpoint(),
139-
)
140-
}
141-
142115
pub fn storage(&self) -> Arc<dyn ObjectStorageProvider + Send + Sync> {
143116
self.storage.clone()
144117
}
145118

146119
pub fn staging_dir(&self) -> &Path {
147120
&self.parseable.local_staging_path
148121
}
122+
123+
pub fn is_default_creds(&self) -> bool {
124+
self.parseable.username == Server::DEFAULT_USERNAME
125+
&& self.parseable.password == Server::DEFAULT_PASSWORD
126+
}
149127
}
150128

151129
impl Default for Config {
@@ -160,10 +138,10 @@ fn parseable_cli_command() -> Command {
160138

161139
let local = local
162140
.mut_arg(Server::USERNAME, |arg| {
163-
arg.required(false).default_value("admin")
141+
arg.required(false).default_value(Server::DEFAULT_USERNAME)
164142
})
165143
.mut_arg(Server::PASSWORD, |arg| {
166-
arg.required(false).default_value("admin")
144+
arg.required(false).default_value(Server::DEFAULT_PASSWORD)
167145
});
168146

169147
let s3 = Server::get_clap_command("--s3-store");
@@ -259,6 +237,8 @@ impl Server {
259237
pub const UPLOAD_INTERVAL: &str = "upload-interval";
260238
pub const USERNAME: &str = "username";
261239
pub const PASSWORD: &str = "password";
240+
pub const DEFAULT_USERNAME: &str = "admin";
241+
pub const DEFAULT_PASSWORD: &str = "admin";
262242

263243
pub fn local_stream_data_path(&self, stream_name: &str) -> PathBuf {
264244
self.local_staging_path.join(stream_name)
@@ -305,7 +285,7 @@ impl Server {
305285
.env("P_STAGING_DIR")
306286
.value_name("DIR")
307287
.default_value("./staging")
308-
.value_parser(value_parser!(PathBuf))
288+
.value_parser(validation::canonicalize_path)
309289
.help("The local staging path is used as a temporary landing point for incoming events and local cache")
310290
.next_line_help(true),
311291
)
@@ -339,7 +319,11 @@ impl Server {
339319
}
340320

341321
pub mod validation {
342-
use std::{net::ToSocketAddrs, path::PathBuf};
322+
use std::{
323+
fs::{canonicalize, create_dir_all},
324+
net::ToSocketAddrs,
325+
path::PathBuf,
326+
};
343327

344328
pub fn file_path(s: &str) -> Result<PathBuf, String> {
345329
if s.is_empty() {
@@ -355,6 +339,17 @@ pub mod validation {
355339
Ok(path)
356340
}
357341

342+
pub fn canonicalize_path(s: &str) -> Result<PathBuf, String> {
343+
let path = PathBuf::from(s);
344+
345+
create_dir_all(&path)
346+
.map_err(|err| err.to_string())
347+
.and_then(|_| {
348+
canonicalize(&path)
349+
.map_err(|_| "Cannot use the path provided as an absolute path".to_string())
350+
})
351+
}
352+
358353
pub fn socket_addr(s: &str) -> Result<String, String> {
359354
s.to_socket_addrs()
360355
.is_ok()

server/src/storage/localfs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use relative_path::RelativePath;
3636
use tokio::fs;
3737
use tokio_stream::wrappers::ReadDirStream;
3838

39-
use crate::query::Query;
39+
use crate::{option::validation, query::Query};
4040

4141
use super::{LogStream, ObjectStorage, ObjectStorageError, ObjectStorageProvider};
4242

@@ -53,9 +53,10 @@ pub struct FSConfig {
5353
#[arg(
5454
env = "P_FS_PATH",
5555
value_name = "filesystem path",
56-
default_value = "./data"
56+
default_value = "./data",
57+
value_parser = validation::canonicalize_path
5758
)]
58-
root: PathBuf,
59+
pub root: PathBuf,
5960
}
6061

6162
impl ObjectStorageProvider for FSConfig {

0 commit comments

Comments
 (0)