Skip to content

Commit

Permalink
refactor(risedev): prompt user with argv[0] instead of hard-coded `…
Browse files Browse the repository at this point in the history
…./risedev`

Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao committed Sep 2, 2024
1 parent b2eea6b commit 63fb504
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ cat <<EOF > "${INSTALL_PATH}"
#!/usr/bin/env bash
set -e
cd "$DIR"
./risedev "\\$@"
RISEDEV_CMD="\\$(basename \\"$0")" ./risedev "\\$@"
EOF
chmod +x "${INSTALL_PATH}"
Expand Down
6 changes: 6 additions & 0 deletions risedev
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ fi

touch risedev-components.user.env

# RISEDEV_CMD might be set if it is installed to the PATH.
# Otherwise, we set it to the current script name.
if [ -z "$RISEDEV_CMD" ]; then
export RISEDEV_CMD="$0"
fi

if [ $# -eq 0 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
cargo make --list-all-steps --hide-uninteresting
exit 0
Expand Down
2 changes: 1 addition & 1 deletion src/risedevtool/common.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ condition = { env_not_set = [ "RISEDEV_CONFIGURED" ] }
script = '''
#!/usr/bin/env bash
set -e
echo "RiseDev is not configured, please run ./risedev configure"
echo "RiseDev is not configured, please run ${RISEDEV_CMD} configure"
exit 1
'''

Expand Down
28 changes: 9 additions & 19 deletions src/risedevtool/src/bin/risedev-dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ fn main() -> Result<()> {
}
manager.finish_all();

use risedev::util::stylized_risedev_subcmd as r;

match task_result {
Ok((stat, log_buffer)) => {
println!("---- summary of startup time ----");
Expand All @@ -458,20 +460,11 @@ fn main() -> Result<()> {

print!("{}", log_buffer);

println!(
"* You may find logs using {} command",
style("./risedev l").blue().bold()
);
println!("* You may find logs using {} command", r("l"));

println!(
"* Run {} to kill cluster.",
style("./risedev k").blue().bold()
);
println!("* Run {} to kill cluster.", r("k"));

println!(
"* Run {} to run `risedev` anywhere!",
style("./risedev install").blue().bold()
);
println!("* Run {} to run `risedev` anywhere!", r("install"));

Ok(())
}
Expand All @@ -484,20 +477,17 @@ fn main() -> Result<()> {
println!();
println!(
"* Use `{}` to enable new components, if they are missing.",
style("./risedev configure").blue().bold(),
r("configure")
);
println!(
"* Use `{}` to view logs, or visit `{}`",
style("./risedev l").blue().bold(),
r("l"),
env::var("PREFIX_LOG")?
);
println!(
"* Run `{}` to clean up cluster.",
style("./risedev k").blue().bold()
);
println!("* Run `{}` to clean up cluster.", r("k"));
println!(
"* Run `{}` to clean data, which might potentially fix the issue.",
style("./risedev clean-data").blue().bold()
r("clean-data")
);
println!("---");
println!();
Expand Down
3 changes: 2 additions & 1 deletion src/risedevtool/src/task/configure_tmux_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::process::Command;
use anyhow::{bail, Context, Result};
use console::style;

use crate::util::stylized_risedev_subcmd;
use crate::{ExecuteContext, Task};

pub struct ConfigureTmuxTask;
Expand Down Expand Up @@ -60,7 +61,7 @@ impl Task for ConfigureTmuxTask {
if ctx.run_command(cmd).is_ok() {
bail!(
"A previous cluster is already running. Please kill it first with {}.",
style("./risedev k").blue().bold()
stylized_risedev_subcmd("k"),
);
}

Expand Down
7 changes: 6 additions & 1 deletion src/risedevtool/src/task/etcd_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::process::Command;
use anyhow::{anyhow, Result};
use itertools::Itertools;

use crate::util::stylized_risedev_subcmd;
use crate::{EtcdConfig, Task};

pub struct EtcdService {
Expand Down Expand Up @@ -102,7 +103,11 @@ impl Task for EtcdService {

let path = Self::path()?;
if !path.exists() {
return Err(anyhow!("etcd binary not found in {:?}\nDid you enable etcd feature in `./risedev configure`?", path));
return Err(anyhow!(
"etcd binary not found in {:?}\nDid you enable etcd feature in `{}`?",
path,
stylized_risedev_subcmd("configure")
));
}

let mut cmd = Self::etcd()?;
Expand Down
3 changes: 2 additions & 1 deletion src/risedevtool/src/task/grafana_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::process::Command;
use anyhow::{anyhow, Result};

use super::{ExecuteContext, Task};
use crate::util::stylized_risedev_subcmd;
use crate::{GrafanaConfig, GrafanaGen};

pub struct GrafanaService {
Expand Down Expand Up @@ -102,7 +103,7 @@ impl Task for GrafanaService {

let path = self.grafana_server_path()?;
if !path.exists() {
return Err(anyhow!("grafana-server binary not found in {:?}\nDid you enable monitoring feature in `./risedev configure`?", path));
return Err(anyhow!("grafana-server binary not found in {:?}\nDid you enable monitoring feature in `{}`?", path, stylized_risedev_subcmd("configure")));
}

Self::write_config_files(
Expand Down
7 changes: 6 additions & 1 deletion src/risedevtool/src/task/minio_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::process::Command;
use anyhow::{anyhow, Result};

use super::{ExecuteContext, Task};
use crate::util::stylized_risedev_subcmd;
use crate::MinioConfig;

pub struct MinioService {
Expand Down Expand Up @@ -91,7 +92,11 @@ impl Task for MinioService {

let path = self.minio_path()?;
if !path.exists() {
return Err(anyhow!("minio binary not found in {:?}\nDid you enable minio feature in `./risedev configure`?", path));
return Err(anyhow!(
"minio binary not found in {:?}\nDid you enable minio feature in `{}`?",
path,
stylized_risedev_subcmd("configure")
));
}

let mut cmd = self.minio()?;
Expand Down
7 changes: 6 additions & 1 deletion src/risedevtool/src/task/prometheus_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::process::Command;
use anyhow::{anyhow, Result};

use super::{ExecuteContext, Task};
use crate::util::stylized_risedev_subcmd;
use crate::{PrometheusConfig, PrometheusGen};

pub struct PrometheusService {
Expand Down Expand Up @@ -57,7 +58,11 @@ impl Task for PrometheusService {

let path = self.prometheus_path()?;
if !path.exists() {
return Err(anyhow!("prometheus binary not found in {:?}\nDid you enable monitoring feature in `./risedev configure`?", path));
return Err(anyhow!(
"prometheus binary not found in {:?}\nDid you enable monitoring feature in `{}`?",
path,
stylized_risedev_subcmd("configure")
));
}

let prefix_config = env::var("PREFIX_CONFIG")?;
Expand Down
7 changes: 6 additions & 1 deletion src/risedevtool/src/task/pubsub_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::process::Command;
use anyhow::{anyhow, Result};

use super::{ExecuteContext, Task};
use crate::util::stylized_risedev_subcmd;
use crate::PubsubConfig;

pub struct PubsubService {
Expand Down Expand Up @@ -49,7 +50,11 @@ impl Task for PubsubService {

let path = self.gcloud_path()?;
if !path.exists() {
return Err(anyhow!("gcloud binary not found in {:?}\nDid you enable pubsub-emulator feature in `./risedev configure`?", path));
return Err(anyhow!(
"gcloud binary not found in {:?}\nDid you enable pubsub-emulator feature in `{}`?",
path,
stylized_risedev_subcmd("configure")
));
}

let mut cmd = self.gcloud()?;
Expand Down
7 changes: 6 additions & 1 deletion src/risedevtool/src/task/redis_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::process::Command;

use anyhow::{anyhow, Result};

use crate::util::stylized_risedev_subcmd;
use crate::{ExecuteContext, RedisConfig, Task};

pub struct RedisService {
Expand Down Expand Up @@ -49,7 +50,11 @@ impl Task for RedisService {
ctx.pb.set_message("starting");
let path = self.redis_path()?;
if !path.exists() {
return Err(anyhow!("Redis binary not found in {:?}\nDid you enable redis feature in `./risedev configure`?", path));
return Err(anyhow!(
"Redis binary not found in {:?}\nDid you enable redis feature in `{}`?",
path,
stylized_risedev_subcmd("configure")
));
}

let mut cmd = self.redis()?;
Expand Down
7 changes: 6 additions & 1 deletion src/risedevtool/src/task/tempo_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::process::Command;
use anyhow::{anyhow, Result};

use super::{ExecuteContext, Task};
use crate::util::stylized_risedev_subcmd;
use crate::{TempoConfig, TempoGen};

pub struct TempoService {
Expand Down Expand Up @@ -63,7 +64,11 @@ impl Task for TempoService {

let path = self.tempo_path()?;
if !path.exists() {
return Err(anyhow!("tempo binary not found in {:?}\nDid you enable tracing feature in `./risedev configure`?", path));
return Err(anyhow!(
"tempo binary not found in {:?}\nDid you enable tracing feature in `{}`?",
path,
stylized_risedev_subcmd("configure")
));
}

let prefix_config = env::var("PREFIX_CONFIG")?;
Expand Down
20 changes: 20 additions & 0 deletions src/risedevtool/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::Display;
use std::process::Command;
use std::sync::LazyLock;

use indicatif::{ProgressBar, ProgressStyle};
use itertools::Itertools;
Expand Down Expand Up @@ -83,3 +85,21 @@ pub fn is_env_set(var: &str) -> bool {
pub fn is_enable_backtrace() -> bool {
!is_env_set("DISABLE_BACKTRACE")
}

pub fn risedev_cmd() -> &'static str {
static RISEDEV_CMD: LazyLock<String> = LazyLock::new(|| {
if let Ok(val) = std::env::var("RISEDEV_CMD") {
val
} else {
"./risedev".to_owned()
}
});

RISEDEV_CMD.as_str()
}

pub fn stylized_risedev_subcmd(subcmd: &str) -> impl Display {
console::style(format!("{} {}", risedev_cmd(), subcmd))
.blue()
.bold()
}

0 comments on commit 63fb504

Please sign in to comment.