Skip to content

Commit fc6e3b1

Browse files
authored
Add check update flag (#262)
1 parent 788f49d commit fc6e3b1

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

server/src/banner.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn print(config: &Config, meta: StorageMetadata) {
2727
let scheme = config.parseable.get_scheme();
2828
status_info(config, &scheme, meta.deployment_id);
2929
storage_info(config);
30-
about::print(meta);
30+
about::print(config, meta);
3131
println!();
3232
}
3333

@@ -95,6 +95,7 @@ pub mod about {
9595
use crossterm::style::Stylize;
9696
use std::fmt;
9797

98+
use crate::option::Config;
9899
use crate::storage::StorageMetadata;
99100
use crate::utils::update;
100101

@@ -114,8 +115,8 @@ pub mod about {
114115

115116
pub fn print_about(
116117
current_version: semver::Version,
118+
latest_release: Option<update::LatestRelease>,
117119
commit_hash: String,
118-
meta: StorageMetadata,
119120
) {
120121
eprint!(
121122
"
@@ -125,7 +126,7 @@ pub mod about {
125126
current_version,
126127
);
127128

128-
if let Ok(latest_release) = update::get_latest(meta) {
129+
if let Some(latest_release) = latest_release {
129130
if latest_release.version > current_version {
130131
print_latest_release(latest_release);
131132
}
@@ -149,13 +150,18 @@ pub mod about {
149150
eprint!("{}", fmt_latest_version.red());
150151
}
151152

152-
pub fn print(meta: StorageMetadata) {
153+
pub fn print(config: &Config, meta: StorageMetadata) {
153154
// print current version
154155
let current = current();
156+
let latest_release = if config.parseable.check_update {
157+
update::get_latest(&meta).ok()
158+
} else {
159+
None
160+
};
155161

156162
match current.0 {
157163
ParseableVersion::Version(current_version) => {
158-
print_about(current_version, current.1, meta);
164+
print_about(current_version, latest_release, current.1);
159165
}
160166
ParseableVersion::Prerelease(current_prerelease) => {
161167
eprintln!(

server/src/option.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ pub struct Server {
187187

188188
/// Password for the basic authentication on the server
189189
pub password: String,
190+
191+
/// Server should check for update or not
192+
pub check_update: bool,
190193
}
191194

192195
impl FromArgMatches for Server {
@@ -219,6 +222,10 @@ impl FromArgMatches for Server {
219222
.get_one::<String>(Self::PASSWORD)
220223
.cloned()
221224
.expect("default for password");
225+
self.check_update = m
226+
.get_one::<bool>(Self::CHECK_UPDATE)
227+
.cloned()
228+
.expect("default for check update");
222229

223230
Ok(())
224231
}
@@ -233,6 +240,7 @@ impl Server {
233240
pub const UPLOAD_INTERVAL: &str = "upload-interval";
234241
pub const USERNAME: &str = "username";
235242
pub const PASSWORD: &str = "password";
243+
pub const CHECK_UPDATE: &str = "check-update";
236244
pub const DEFAULT_USERNAME: &str = "admin";
237245
pub const DEFAULT_PASSWORD: &str = "admin";
238246

@@ -311,6 +319,16 @@ impl Server {
311319
.required(true)
312320
.help("Password for the basic authentication on the server"),
313321
)
322+
.arg(
323+
Arg::new(Self::CHECK_UPDATE)
324+
.long(Self::CHECK_UPDATE)
325+
.env("P_CHECK_UPDATE")
326+
.value_name("BOOL")
327+
.required(false)
328+
.default_value("true")
329+
.value_parser(value_parser!(bool))
330+
.help("Password for the basic authentication on the server"),
331+
)
314332
}
315333
}
316334

server/src/utils.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,36 +145,33 @@ pub mod update {
145145

146146
static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST";
147147

148+
#[derive(Debug)]
148149
pub struct LatestRelease {
149150
pub version: semver::Version,
150151
pub date: DateTime<Utc>,
151152
}
152153

153-
fn is_docker() -> String {
154-
if Path::new("/.dockerenv").exists() {
155-
return "Docker".to_string();
156-
}
157-
"Native".to_string()
154+
fn is_docker() -> bool {
155+
Path::new("/.dockerenv").exists()
158156
}
159157

160-
fn is_k8s() -> String {
161-
if env::var(K8S_ENV_TO_CHECK).is_ok() {
162-
return "Kubernetes".to_string();
163-
}
164-
"".to_string()
158+
fn is_k8s() -> bool {
159+
env::var(K8S_ENV_TO_CHECK).is_ok()
165160
}
166161

167-
fn platform() -> String {
168-
let mut platform = is_k8s();
169-
if platform.is_empty() {
170-
platform = is_docker();
162+
fn platform() -> &'static str {
163+
if is_k8s() {
164+
"Kubernetes"
165+
} else if is_docker() {
166+
"Docker"
167+
} else {
168+
"Native"
171169
}
172-
platform
173170
}
174171

175172
// User Agent for Download API call
176173
// Format: Parseable/<UID>/<version>/<commit_hash> (<OS>; <Platform>)
177-
fn user_agent(uid: Ulid) -> String {
174+
fn user_agent(uid: &Ulid) -> String {
178175
let info = os_info::get();
179176
format!(
180177
"Parseable/{}/{}/{} ({}; {})",
@@ -186,9 +183,9 @@ pub mod update {
186183
)
187184
}
188185

189-
pub fn get_latest(meta: StorageMetadata) -> Result<LatestRelease, anyhow::Error> {
186+
pub fn get_latest(meta: &StorageMetadata) -> Result<LatestRelease, anyhow::Error> {
190187
let agent = ureq::builder()
191-
.user_agent(user_agent(meta.deployment_id).as_str())
188+
.user_agent(user_agent(&meta.deployment_id).as_str())
192189
.timeout(Duration::from_secs(8))
193190
.build();
194191

0 commit comments

Comments
 (0)