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

refactor(risedev): replace std::fs with fs_err #8955

Merged
merged 1 commit into from
Apr 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/risedevtool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ clap = { version = "4", features = ["derive"] }
console = "0.15"
dialoguer = "0.10"
enum-iterator = "1"
fs-err = "2.9.0"
google-cloud-pubsub = "0.7.0"
indicatif = "0.17"
isahc = { version = "1", default-features = false, features = ["text-decoding"] }
Expand Down
14 changes: 7 additions & 7 deletions src/risedevtool/src/bin/risedev-compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
// limitations under the License.

use std::collections::BTreeMap;
use std::fs::{self, File};
use std::io::Read;
use std::path::Path;

use anyhow::{anyhow, Result};
use clap::Parser;
use console::style;
use fs_err::{self, File};
use itertools::Itertools;
use risedev::{
compose_deploy, compute_risectl_env, Compose, ComposeConfig, ComposeDeployConfig, ComposeFile,
Expand Down Expand Up @@ -92,7 +92,7 @@ fn main() -> Result<()> {

let compose_config = ComposeConfig {
image: load_docker_image_config(
&std::fs::read_to_string(RISEDEV_CONFIG_FILE)?,
&fs_err::read_to_string(RISEDEV_CONFIG_FILE)?,
compose_deploy_config
.as_ref()
.and_then(|x| x.risingwave_image_override.as_ref()),
Expand Down Expand Up @@ -164,7 +164,7 @@ fn main() -> Result<()> {
.green(),
style(&arg).green()
)?;
fs::write(
fs_err::write(
Path::new(&opts.directory).join("tpch-bench-args-frontend"),
arg,
)?;
Expand Down Expand Up @@ -213,7 +213,7 @@ fn main() -> Result<()> {
"-- Redpanda --\ntpch-bench: {}\n",
style(&arg).green()
)?;
fs::write(
fs_err::write(
Path::new(&opts.directory).join("tpch-bench-args-kafka"),
arg,
)?;
Expand Down Expand Up @@ -271,7 +271,7 @@ fn main() -> Result<()> {
)?;
}

fs::write(
fs_err::write(
Path::new(&opts.directory).join(format!("{}.yml", node)),
yaml,
)?;
Expand All @@ -290,7 +290,7 @@ fn main() -> Result<()> {

println!("\n{}", log_buffer);

std::fs::write(
fs_err::write(
Path::new(&opts.directory).join("_message.partial.sh"),
log_buffer,
)?;
Expand All @@ -310,7 +310,7 @@ fn main() -> Result<()> {

let yaml = serde_yaml::to_string(&compose_file)?;

fs::write(Path::new(&opts.directory).join("docker-compose.yml"), yaml)?;
fs_err::write(Path::new(&opts.directory).join("docker-compose.yml"), yaml)?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/risedevtool/src/bin/risedev-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

#![allow(clippy::needless_question_mark)]

use std::fs::OpenOptions;
use std::io::{BufRead, BufReader, BufWriter, Write};

use anyhow::{Context, Result};
use clap::{Parser, Subcommand, ValueEnum};
use console::style;
use dialoguer::MultiSelect;
use enum_iterator::{all, Sequence};
use fs_err::OpenOptions;
use itertools::Itertools;
use risedev::RISEDEV_CONFIG_FILE;

Expand Down
8 changes: 4 additions & 4 deletions src/risedevtool/src/bin/risedev-dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

use std::env;
use std::fmt::Write;
use std::fs::OpenOptions;
use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::{anyhow, Context, Result};
use console::style;
use fs_err::OpenOptions;
use indicatif::ProgressBar;
use risedev::util::{complete_spin, fail_spin};
use risedev::{
Expand Down Expand Up @@ -376,14 +376,14 @@ fn main() -> Result<()> {

if let Some(config_path) = &config_path {
let target = Path::new(&env::var("PREFIX_CONFIG")?).join("risingwave.toml");
std::fs::copy(config_path, target).context("config file not found")?;
fs_err::copy(config_path, target).context("config file not found")?;
}

{
let mut out_str = String::new();
let mut emitter = YamlEmitter::new(&mut out_str);
emitter.dump(&risedev_config)?;
std::fs::write(
fs_err::write(
Path::new(&env::var("PREFIX_CONFIG")?).join("risedev-expanded.yml"),
&out_str,
)?;
Expand Down Expand Up @@ -434,7 +434,7 @@ fn main() -> Result<()> {
Err(_) => "".into(),
};

std::fs::write(
fs_err::write(
Path::new(&env::var("PREFIX_CONFIG")?).join("risectl-env"),
risectl_env,
)?;
Expand Down
12 changes: 6 additions & 6 deletions src/risedevtool/src/bin/risedev-docslt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ fn generate_slt_files(package_name: &str) -> Result<()> {
}
}

let rustdoc: JsonValue = serde_json::from_reader(std::io::BufReader::new(
std::fs::File::open(format!("target/doc/{}.json", package_name))?,
))?;
let rustdoc: JsonValue = serde_json::from_reader(std::io::BufReader::new(fs_err::File::open(
format!("target/doc/{}.json", package_name),
)?))?;
let index = rustdoc["index"]
.as_object()
.ok_or_else(|| anyhow!("failed to access `index` field as object"))?;
Expand Down Expand Up @@ -174,9 +174,9 @@ fn generate_slt_files(package_name: &str) -> Result<()> {
}

let slt_dir = PathBuf::from(format!("e2e_test/generated/docslt/{}", package_name));
std::fs::remove_dir_all(&slt_dir).ok();
fs_err::remove_dir_all(&slt_dir).ok();
if !slt_blocks_per_file.is_empty() {
std::fs::create_dir_all(&slt_dir)?;
fs_err::create_dir_all(&slt_dir)?;
}

for filename in slt_blocks_per_file.keys() {
Expand All @@ -186,7 +186,7 @@ fn generate_slt_files(package_name: &str) -> Result<()> {
.components()
.filter_map(|comp| comp.as_os_str().to_str().filter(|s| *s != "src"))
.join("__");
let mut slt_file = std::fs::File::create(slt_dir.join(slt_filename))?;
let mut slt_file = fs_err::File::create(slt_dir.join(slt_filename))?;
write!(
slt_file,
"\
Expand Down
16 changes: 8 additions & 8 deletions src/risedevtool/src/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Compose for ComputeNodeConfig {

if let Some(c) = &config.rw_config_path {
let target = Path::new(&config.config_directory).join("risingwave.toml");
std::fs::copy(c, target)?;
fs_err::copy(c, target)?;
command.arg("--config-path").arg("/risingwave.toml");
}

Expand Down Expand Up @@ -205,7 +205,7 @@ impl Compose for MetaNodeConfig {

if let Some(c) = &config.rw_config_path {
let target = Path::new(&config.config_directory).join("risingwave.toml");
std::fs::copy(c, target)?;
fs_err::copy(c, target)?;
command.arg("--config-path").arg("/risingwave.toml");
}

Expand Down Expand Up @@ -238,7 +238,7 @@ impl Compose for FrontendConfig {

if let Some(c) = &config.rw_config_path {
let target = Path::new(&config.config_directory).join("risingwave.toml");
std::fs::copy(c, target)?;
fs_err::copy(c, target)?;
command.arg("--config-path").arg("/risingwave.toml");
}

Expand Down Expand Up @@ -268,7 +268,7 @@ impl Compose for CompactorConfig {

if let Some(c) = &config.rw_config_path {
let target = Path::new(&config.config_directory).join("risingwave.toml");
std::fs::copy(c, target)?;
fs_err::copy(c, target)?;
command.arg("--config-path").arg("/risingwave.toml");
}

Expand Down Expand Up @@ -404,7 +404,7 @@ impl Compose for PrometheusConfig {
..Default::default()
};

std::fs::write(
fs_err::write(
Path::new(&config.config_directory).join("prometheus.yaml"),
prometheus_config,
)?;
Expand All @@ -419,17 +419,17 @@ impl Compose for PrometheusConfig {
impl Compose for GrafanaConfig {
fn compose(&self, config: &ComposeConfig) -> Result<ComposeService> {
let config_root = Path::new(&config.config_directory);
std::fs::write(
fs_err::write(
config_root.join("grafana.ini"),
GrafanaGen.gen_custom_ini(self),
)?;

std::fs::write(
fs_err::write(
config_root.join("grafana-risedev-datasource.yml"),
GrafanaGen.gen_datasource_yml(self)?,
)?;

std::fs::write(
fs_err::write(
config_root.join("grafana-risedev-dashboard.yml"),
GrafanaGen.gen_dashboard_yml(self, config_root, "/")?,
)?;
Expand Down
13 changes: 6 additions & 7 deletions src/risedevtool/src/compose_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use std::collections::{BTreeMap, HashMap};
use std::fs;
use std::os::unix::prelude::PermissionsExt;
use std::path::Path;

Expand Down Expand Up @@ -136,7 +135,7 @@ fi
r#"echo "{id}: $(tput setaf 2)done sync config$(tput sgr0)""#,
)?;
let sh = format!("_deploy.{id}.partial.sh");
std::fs::write(Path::new(output_directory).join(&sh), y)?;
fs_err::write(Path::new(output_directory).join(&sh), y)?;
writeln!(x, "{sh}")?;
}
writeln!(x, "EOF")?;
Expand Down Expand Up @@ -182,7 +181,7 @@ fi
}

let sh = format!("_stop.{id}.partial.sh");
std::fs::write(Path::new(output_directory).join(&sh), y)?;
fs_err::write(Path::new(output_directory).join(&sh), y)?;
writeln!(x, "{sh}")?;
}
writeln!(x, "EOF")?;
Expand Down Expand Up @@ -233,17 +232,17 @@ fi
writeln!(y, "ssh {ssh_extra_args} ubuntu@{public_ip} \"bash -c 'cd {base_folder} && docker compose ps'\"")?;

let sh = format!("_check.{id}.partial.sh");
std::fs::write(Path::new(output_directory).join(&sh), y)?;
fs_err::write(Path::new(output_directory).join(&sh), y)?;
writeln!(x, "{sh}")?;
}
writeln!(x, "EOF")?;
writeln!(x, r#"fi"#)?;
x
};
let deploy_sh = Path::new(output_directory).join("deploy.sh");
fs::write(&deploy_sh, shell_script)?;
let mut perms = fs::metadata(&deploy_sh)?.permissions();
fs_err::write(&deploy_sh, shell_script)?;
let mut perms = fs_err::metadata(&deploy_sh)?.permissions();
perms.set_mode(perms.mode() | 0o755);
fs::set_permissions(&deploy_sh, perms)?;
fs_err::set_permissions(&deploy_sh, perms)?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/risedevtool/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ConfigExpander {
/// Load a single document YAML file.
fn load_yaml(path: impl AsRef<Path>) -> Result<Yaml> {
let path = path.as_ref();
let content = std::fs::read_to_string(path)?;
let content = fs_err::read_to_string(path)?;
let [config]: [_; 1] = YamlLoader::load_from_str(&content)?
.try_into()
.map_err(|_| anyhow!("expect `{}` to have only one section", path.display()))?;
Expand Down
2 changes: 1 addition & 1 deletion src/risedevtool/src/config_gen/grafana_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ datasources:
let filename = "risingwave-dashboard.json";
let generate_path = generate_path.as_ref();
let dashboard_path = Path::new(generate_path).join(filename);
std::fs::copy("grafana/risingwave-dashboard.json", dashboard_path)?;
fs_err::copy("grafana/risingwave-dashboard.json", dashboard_path)?;

let grafana_read_path = grafana_read_path.as_ref();
let dashboard_path = Path::new(grafana_read_path).join(filename);
Expand Down
2 changes: 1 addition & 1 deletion src/risedevtool/src/task/etcd_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Task for EtcdService {
Self::apply_command_args(&mut cmd, &self.config)?;

let path = Path::new(&env::var("PREFIX_DATA")?).join(self.id());
std::fs::create_dir_all(&path)?;
fs_err::create_dir_all(&path)?;
cmd.arg("--data-dir").arg(&path);

ctx.run_command(ctx.tmux_run(cmd)?)?;
Expand Down
16 changes: 8 additions & 8 deletions src/risedevtool/src/task/grafana_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@ impl GrafanaService {
) -> Result<()> {
let config_root = config_root.as_ref();

std::fs::write(
fs_err::write(
config_root.join("custom.ini"),
GrafanaGen.gen_custom_ini(config),
)?;

let config_datasources_dir = config_root.join("provisioning").join("datasources");
std::fs::remove_dir_all(&config_datasources_dir)?;
std::fs::create_dir_all(&config_datasources_dir)?;
std::fs::write(
fs_err::remove_dir_all(&config_datasources_dir)?;
fs_err::create_dir_all(&config_datasources_dir)?;
fs_err::write(
config_datasources_dir.join("risedev-prometheus.yml"),
GrafanaGen.gen_datasource_yml(config)?,
)?;

let prefix_config = prefix_config.as_ref();
let config_dashboards_dir = config_root.join("provisioning").join("dashboards");
std::fs::remove_dir_all(&config_dashboards_dir)?;
std::fs::create_dir_all(&config_dashboards_dir)?;
std::fs::write(
fs_err::remove_dir_all(&config_dashboards_dir)?;
fs_err::create_dir_all(&config_dashboards_dir)?;
fs_err::write(
config_dashboards_dir.join("risingwave-dashboard.yaml"),
GrafanaGen.gen_dashboard_yml(config, prefix_config, prefix_config)?,
)?;
// std::fs::write(
// fs_err::write(
// config_dashboards_dir.join("aws-s3-dashboards.yaml"),
// &GrafanaGen.gen_s3_dashboard_yml(config, prefix_config)?,
// )?;
Expand Down
6 changes: 3 additions & 3 deletions src/risedevtool/src/task/kafka_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ impl Task for KafkaService {
Path::new(&env::var("PREFIX_DATA")?).join(self.id())
} else {
let path = Path::new("/tmp/risedev").join(self.id());
std::fs::remove_dir_all(&path).ok();
fs_err::remove_dir_all(&path).ok();
path
};
std::fs::create_dir_all(&path)?;
fs_err::create_dir_all(&path)?;

let config_path = Path::new(&prefix_config).join(format!("{}.properties", self.id()));
std::fs::write(
fs_err::write(
&config_path,
KafkaGen.gen_server_properties(&self.config, &path.to_string_lossy()),
)?;
Expand Down
2 changes: 1 addition & 1 deletion src/risedevtool/src/task/minio_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Task for MinioService {
let prefix_config = env::var("PREFIX_CONFIG")?;

let data_path = Path::new(&env::var("PREFIX_DATA")?).join(self.id());
std::fs::create_dir_all(&data_path)?;
fs_err::create_dir_all(&data_path)?;

cmd.arg("--config-dir")
.arg(Path::new(&prefix_config).join("minio"))
Expand Down
2 changes: 1 addition & 1 deletion src/risedevtool/src/task/prometheus_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Task for PrometheusService {
let prefix_config = env::var("PREFIX_CONFIG")?;
let prefix_data = env::var("PREFIX_DATA")?;

std::fs::write(
fs_err::write(
Path::new(&prefix_config).join("prometheus.yml"),
PrometheusGen.gen_prometheus_yml(&self.config),
)?;
Expand Down