Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non interactive setup to golemsp #2752

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions golem_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,20 @@ async fn my_main() -> Result</*exit code*/ i32> {
}
}

pub fn banner() {
terminal::fade_in(&format!(
pub fn banner(interactive: bool) {
let banner = format!(
include_str!("banner.txt"),
version = ya_compile_time_utils::semver_str!(),
git_commit = ya_compile_time_utils::git_rev(),
date = ya_compile_time_utils::build_date(),
build = ya_compile_time_utils::build_number_str().unwrap_or("-"),
))
.unwrap();
);

if interactive {
terminal::fade_in(&banner).unwrap();
} else {
println!("{banner}")
}
}

#[actix_rt::main]
Expand Down
50 changes: 35 additions & 15 deletions golem_cli/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ pub struct RunConfig {
set = clap::ArgSettings::Global
)]
pub log_dir: Option<PathBuf>,
/// Don't prompt user for configuration, but use defaults instead.
#[structopt(long)]
pub no_interactive: bool,
}

pub async fn setup(run_config: &RunConfig, force: bool) -> Result<i32> {
let interactive = !run_config.no_interactive;
if force {
super::banner();
super::banner(interactive);
eprintln!("Initial node setup");
let _ = clear_stdin().await;
}
Expand All @@ -71,13 +75,20 @@ pub async fn setup(run_config: &RunConfig, force: bool) -> Result<i32> {
}

if config.node_name.is_none() || force {
let node_name = promptly::prompt_default(
"Node name ",
config
.node_name
.clone()
.unwrap_or_else(|| names::Generator::default().next().unwrap_or_default()),
)?;
let node_name = if interactive {
promptly::prompt_default(
"Node name ",
config
.node_name
.clone()
.unwrap_or_else(|| names::Generator::default().next().unwrap_or_default()),
)?
} else {
let name = names::Generator::default().next().unwrap_or_default();
println!("Node name (default={name})");
name
};

let account_msg = &config
.account
.map(|n| n.to_string())
Expand All @@ -87,14 +98,18 @@ pub async fn setup(run_config: &RunConfig, force: bool) -> Result<i32> {
run_config.account.network, account_msg
);

while let Some(account) = promptly::prompt_opt::<String, _>(&message)? {
match account.parse::<NodeId>() {
Err(e) => eprintln!("Invalid ethereum address, is should be 20-byte hex (example 0xB1974E1F44EAD2d22bB995167A709b89Fc466B6c): {}", e),
Ok(account) => {
config.account = Some(account);
break;
if interactive {
while let Some(account) = promptly::prompt_opt::<String, _>(&message)? {
match account.parse::<NodeId>() {
Err(e) => eprintln!("Invalid ethereum address, is should be 20-byte hex (example 0xB1974E1F44EAD2d22bB995167A709b89Fc466B6c): {}", e),
Ok(account) => {
config.account = Some(account);
break;
}
}
}
} else {
println!("{message}");
}

config.node_name = Some(node_name);
Expand Down Expand Up @@ -132,7 +147,12 @@ pub async fn setup(run_config: &RunConfig, force: bool) -> Result<i32> {
.collect();

let default_glm_per_h = 0.025;
let glm_per_h = promptly::prompt_default("Price GLM per hour", default_glm_per_h)?;
let glm_per_h = if interactive {
promptly::prompt_default("Price GLM per hour", default_glm_per_h)?
} else {
println!("Price GLM per hour (default={default_glm_per_h})");
default_glm_per_h
};

let mut usage = UsageDef::new();
usage.insert("CPU".into(), glm_per_h / 3600.0);
Expand Down