-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a69423
commit 6e092f0
Showing
5 changed files
with
87 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
target | ||
.idea | ||
Cargo.lock | ||
files/sealos | ||
files/kube_versions.json | ||
sealos.tar.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,54 @@ | ||
use std::fs::File; | ||
use std::io::copy; | ||
use std::path::Path; | ||
use std::process::Command; | ||
use std::{env, fs}; | ||
|
||
use anyhow::Error; | ||
use pulldown_cmark::HeadingLevel::H1; | ||
use pulldown_cmark::{Event, Options, Parser, Tag}; | ||
#[allow(clippy::single_component_path_imports)] | ||
use reqwest; | ||
use serde_json as json; | ||
use std::fs::File; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let os = env::consts::OS; | ||
if os == "macos" { | ||
println!("cargo:warning=sealos only support linux"); | ||
return Ok(()); // macos 不支持 sealos | ||
} else if os != "linux" { | ||
println!("cargo:warning=sealos only support linux"); | ||
return Err("sealos only support linux".into()); | ||
} | ||
let arch = env::consts::ARCH; | ||
let url = match arch { | ||
"x86_64" => "https://github.com/labring/sealos/releases/download/v4.3.7/sealos_4.3.7_linux_amd64.tar.gz", | ||
"arm64" => "https://github.com/labring/sealos/releases/download/v4.3.7/sealos_4.3.7_linux_arm64.tar.gz", | ||
"aarch64" => "https://github.com/labring/sealos/releases/download/v4.3.7/sealos_4.3.7_linux_arm64.tar.gz", | ||
_ => "", | ||
}; | ||
|
||
println!("cargo:rustc-env=ARCH={}", arch); | ||
println!("cargo:rustc-env=URL={}", url); | ||
let target_dir = Path::new("files"); | ||
let target_file = target_dir.join("sealos"); | ||
if target_file.exists() { | ||
println!("File already exists: {:?}", target_file); | ||
return Ok(()); | ||
async fn main() -> anyhow::Result<(), Error> { | ||
let _support_versions = vec![ | ||
"1.16", "1.17", "1.18", "1.19", "1.20", "1.21", "1.22", "1.23", "1.24", "1.25", "1.26", | ||
"1.27", "1.28", "1.29", "1.30", "1.31", | ||
]; | ||
let mut versions = Vec::new(); | ||
for version in _support_versions { | ||
let url = format!( | ||
"https://raw.githubusercontent.com/kubernetes/kubernetes/refs/heads/master/CHANGELOG/CHANGELOG-{}.md", | ||
version | ||
); | ||
println!("cargo:warning={}", url); | ||
let resp = reqwest::get(&url).await?; | ||
let body = resp.text().await?; | ||
let h1 = parse_md(&body)?; | ||
println!("cargo:warning=fetch version is {:?}", h1); | ||
versions.push(h1); | ||
} | ||
let output_file = "sealos.tar.gz"; | ||
let response = reqwest::get(url).await?; | ||
let path = Path::new(output_file); | ||
let mut file = File::create(path)?; | ||
let content = response.bytes().await?; | ||
copy(&mut content.as_ref(), &mut file)?; | ||
println!("临时文件已下载到: {:?}", path); | ||
Command::new("tar") | ||
.arg("-zxf") | ||
.arg(output_file) | ||
.arg("sealos") | ||
.status() | ||
.expect("Failed to execute tar"); | ||
|
||
// 删除下载的 tar.gz 文件 | ||
fs::remove_file(output_file).expect("Failed to remove sealos.tar.gz"); | ||
|
||
Command::new("chmod") | ||
.arg("a+x") | ||
.arg("sealos") | ||
.status() | ||
.expect("Failed to execute chmod"); | ||
let target_dir = Path::new("files"); | ||
|
||
fs::rename("sealos", target_dir.join("sealos")).expect("Failed to move sealos to files/"); | ||
let file = File::create("files/kube_versions.json")?; | ||
json::to_writer(&file, &versions)?; | ||
Ok(()) | ||
} | ||
#[allow(clippy::redundant_guards)] | ||
fn parse_md(markdown_input: &str) -> Result<Vec<String>, Error> { | ||
let options = Options::all(); | ||
let parser = Parser::new_ext(markdown_input, options); | ||
let mut titles = Vec::new(); | ||
let mut in_level_one_heading = false; | ||
for event in parser { | ||
match event { | ||
Event::Start(Tag::Heading { level, .. }) if level == H1 => { | ||
in_level_one_heading = true; | ||
} | ||
Event::Text(text) if in_level_one_heading => { | ||
in_level_one_heading = false; | ||
if text.contains('-') || !text.starts_with('v') { | ||
continue; | ||
} | ||
titles.push(text.to_string()); | ||
} | ||
_ => {} | ||
} | ||
} | ||
Ok(titles) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters