use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
use crate::env;
pub(crate) async fn maybe_auto_update(
_args: &[String],
_original_cwd: Option<&std::path::Path>,
_command_eligible: bool,
) -> crate::Result<()> {
Ok(())
}
#[derive(Debug, Default, usage_rs::Args)]
pub(crate) struct SelfUpdate {
version: Option<String>,
#[usage(long, short)]
force: bool,
#[usage(long, short)]
yes: bool,
#[usage(long)]
no_plugins: bool,
}
impl SelfUpdate {
pub(crate) async fn run(self) -> crate::Result<()> {
if let Some(instructions) = upgrade_instructions_text() {
warn!("{}", instructions);
}
eyre::bail!("mise's self-update feature has been disabled at build time, cannot update");
}
pub(crate) fn is_available() -> bool {
false
}
}
#[derive(Debug, Default, serde::Deserialize)]
struct InstructionsToml {
message: Option<String>,
#[serde(flatten)]
commands: BTreeMap<String, String>,
}
fn read_instructions_file(path: &PathBuf) -> Option<String> {
let body = fs::read_to_string(path).ok()?;
let parsed: InstructionsToml = toml::from_str(&body).ok()?;
if let Some(msg) = parsed.message {
return Some(msg);
}
if let Some((_k, v)) = parsed.commands.into_iter().next() {
return Some(v);
}
None
}
pub(crate) fn upgrade_instructions_text() -> Option<String> {
if let Some(path) = &*env::MISE_SELF_UPDATE_INSTRUCTIONS
&& let Some(msg) = read_instructions_file(path)
{
return Some(msg);
}
None
}
pub(crate) const SELF_UPDATE_DISABLED_HINT: &str =
"self-update is disabled for this install, update mise the same way you installed it";
pub(crate) fn upgrade_instructions_or_hint() -> String {
upgrade_instructions_text().unwrap_or_else(|| SELF_UPDATE_DISABLED_HINT.to_string())
}
pub(crate) fn append_self_update_instructions(mut message: String) -> String {
message.push('\n');
message.push_str(&upgrade_instructions_or_hint());
message
}