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

feat: Adds hc update command #176

Merged
merged 1 commit into from
Jul 3, 2024
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
38 changes: 19 additions & 19 deletions Cargo.lock

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

17 changes: 17 additions & 0 deletions hipcheck/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ pub enum FullCommands {
Schema(SchemaArgs),
Setup(SetupArgs),
Ready,
Update(UpdateArgs),
PrintConfig,
PrintData,
PrintCache,
Expand All @@ -380,6 +381,7 @@ impl From<&Commands> for FullCommands {
Commands::Setup(args) => FullCommands::Setup(args.clone()),
Commands::Ready => FullCommands::Ready,
Commands::Scoring => FullCommands::Scoring,
Commands::Update(args) => FullCommands::Update(args.clone()),
}
}
}
Expand All @@ -404,6 +406,8 @@ pub enum Commands {
Ready,
/// Print the tree used to weight analyses during scoring.
Scoring,
/// Run Hipcheck self-updater, if installed
Update(UpdateArgs),
}

// If no subcommand matched, default to use of '-t <TYPE> <TARGET' syntax. In
Expand Down Expand Up @@ -570,6 +574,19 @@ pub struct SetupArgs {
pub source: Option<PathBuf>,
}

#[derive(Debug, Clone, clap::Args)]
pub struct UpdateArgs {
/// Installs the specified tag instead of the latest version
#[clap(long)]
pub tag: Option<String>,
/// Installs the specified version instead of the latest version
#[clap(long)]
pub version: Option<String>,
/// Allows prereleases when just updating to "latest"
#[clap(long)]
pub prerelease: bool,
}

/// A type that can copy non-`None` values from other instances of itself.
pub trait Update {
/// Update self with the value from other, if present.
Expand Down
49 changes: 49 additions & 0 deletions hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use cli::FullCommands;
use cli::SchemaArgs;
use cli::SchemaCommand;
use cli::SetupArgs;
use cli::UpdateArgs;
use command_util::DependentProgram;
use config::WeightTreeNode;
use config::WeightTreeProvider;
Expand All @@ -61,12 +62,15 @@ use schemars::schema_for;
use std::env;
use std::fmt::Display;
use std::fmt::Formatter;
use std::io::Write;
use std::ops::Not as _;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::process::ExitCode;
use std::result::Result as StdResult;
use util::fs::create_dir_all;
use which::which;

fn init_logging() {
EnvLoggerBuilder::from_env(Env::new().filter("HC_LOG").write_style("HC_LOG_STYLE")).init();
Expand All @@ -91,6 +95,7 @@ fn main() -> ExitCode {
Some(FullCommands::Schema(args)) => cmd_schema(&args),
Some(FullCommands::Setup(args)) => return cmd_setup(&args, &config),
Some(FullCommands::Ready) => cmd_ready(&config),
Some(FullCommands::Update(args)) => cmd_update(&args),
Some(FullCommands::PrintConfig) => cmd_print_config(config.config()),
Some(FullCommands::PrintData) => cmd_print_data(config.data()),
Some(FullCommands::PrintCache) => cmd_print_home(config.cache()),
Expand Down Expand Up @@ -608,6 +613,50 @@ fn cmd_ready(config: &CliConfig) {
}
}

/// Run the Hipcheck self-updater to update to the latest release version.
/// If the updater is not found, returns an error
fn cmd_update(args: &UpdateArgs) {
let command_name;
// Because of a bug in cargo-dist's updater, it is possible for the updater to be installed as "hipcheck-update" instead of "hc-update"
if which("hc-update").is_ok() {
command_name = "hc-update";
} else if which("hipcheck-update").is_ok() {
command_name = "hipcheck-update";
} else {
// If neither possible updater command us found, print this error
print_error(&hc_error!("Updater tool not found. Did you install Hipcheck with the official release script (which will install the updater tool)? If you installed Hipcheck from source, you must update Hipcheck by installing a new version from source manually."));
return;
}

// Create the updater command, with optional arguments
let mut hc_command = updater_command(command_name, args);
match hc_command.output() {
// Panic: Safe to unwrap because if the updater command runs, it will always produce some output to stderr
Ok(output) => std::io::stdout().write_all(&output.stderr).unwrap(),
Err(..) => print_error(&hc_error!("Updater command failed to run. You may need to re-install Hipcheck with the official release script.")),
}
}

/// Creates an updater command, including any optional arguments
fn updater_command(command_name: &str, args: &UpdateArgs) -> Command {
let mut command = Command::new(command_name);

// If both the --tag and --version arguments are passed to the updater, it will exit with an error message instead of updating
if let Some(tag) = &args.tag {
command.args(["--tag", tag]);
}

if let Some(version) = &args.version {
command.args(["--version", version]);
}

if args.prerelease {
command.arg("--prerelease");
}

command
}

/// Print the current home directory for Hipcheck.
///
/// Exits `Ok` if home directory is specified, `Err` otherwise.
Expand Down