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

Add Apple Sillicon build of kubie #57

Merged
merged 5 commits into from
Jan 9, 2021
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
28 changes: 24 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ jobs:
- run: strip target/x86_64-unknown-linux-musl/release/kubie
- run: ls -lh target/x86_64-unknown-linux-musl/release/

- name: Upload files to a GitHub release
uses: svenstaro/upload-release-action@v2
- uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: target/x86_64-unknown-linux-musl/release/kubie
Expand All @@ -45,12 +44,33 @@ jobs:
with:
command: build
args: --release
- run: strip target/release/kubie
- run: ls -lh target/release/

- name: Upload files to a GitHub release
uses: svenstaro/upload-release-action@v2
- uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: target/release/kubie
asset_name: kubie-darwin-amd64
tag: ${{ github.ref }}

build-macos-arm64:
runs-on: macos-11.0
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: aarch64-apple-darwin

- run: SDKROOT=$(xcrun -sdk macosx11.0 --show-sdk-path) MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx11.0 --show-sdk-platform-version) cargo build --release --target aarch64-apple-darwin
- run: strip target/aarch64-apple-darwin/release/kubie
- run: ls -lh target/aarch64-apple-darwin/release/

- uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: target/aarch64-apple-darwin/release/kubie
asset_name: kubie-darwin-arm64
tag: ${{ github.ref }}

2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable

- uses: actions-rs/cargo@v1
with:
command: test
Expand All @@ -22,6 +23,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable

- uses: actions-rs/cargo@v1
with:
command: test
Expand Down
15 changes: 2 additions & 13 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ license = "Zlib"
name = "kubie"
readme = "README.md"
repository = "https://github.com/sbstp/kubie"
version = "0.12.0"
version = "0.12.1"

[dependencies]
anyhow = "1"
atty = "0.2"
cfg-if = "1"
dirs = "2"
glob = "0.3"
lazy_static = "1"
libc = "0.2"
os_info = "2"
serde = {version = "1", features = ["derive"]}
serde_json = "1"
serde_yaml = "0.8"
Expand Down
78 changes: 47 additions & 31 deletions src/cmd/update.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::env;
use std::fs;
use std::fs::Permissions;
use std::os::unix::prelude::*;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;

use anyhow::{Context, Result};
use cfg_if::cfg_if;
use serde::Deserialize;

const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -18,34 +19,36 @@ pub struct Release {
}

impl Release {
fn get_linux_binary_url(&self) -> Option<&str> {
for asset in self.assets.iter() {
if asset.browser_download_url.contains("linux-amd64") {
return Some(&asset.browser_download_url);
}
}
None
pub fn get_latest() -> Result<Release> {
let latest_release = attohttpc::get(&LATEST_RELEASE_URL).send()?.json()?;
Ok(latest_release)
}

fn get_macos_binary_url(&self) -> Option<&str> {
for asset in self.assets.iter() {
if asset.browser_download_url.contains("darwin-amd64") {
return Some(&asset.browser_download_url);
// Get the right binary name based on which OS and architecture kubie was built-on.
// Names match the GitHub releases: https://github.com/sbstp/kubie/releases
fn get_binary_name() -> Option<&'static str> {
cfg_if! {
if #[cfg(all(target_os = "linux", target_arch = "x86_64"))] {
Some("kubie-linux-amd64")
} else if #[cfg(all(target_os = "macos", target_arch = "x86_64"))] {
Some("kubie-darwin-amd64")
} else if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] {
Some("kubie-darwin-arm64")
} else {
None
}
}
None
}

pub fn get_binary_url(&self) -> Option<&str> {
match os_info::get().os_type() {
os_info::Type::Macos => {
return self.get_macos_binary_url();
}
os_info::Type::Windows => None,
_ => {
return self.get_linux_binary_url();
let binary_name = Self::get_binary_name()?;

for asset in self.assets.iter() {
if asset.browser_download_url.contains(binary_name) {
return Some(&asset.browser_download_url);
}
}
None
}
}

Expand All @@ -57,15 +60,18 @@ struct Asset {
}

pub fn update() -> Result<()> {
let latest_release: Release = get_latest_release()?;
let latest_release = Release::get_latest()?;
if latest_release.tag_name == format!("v{}", VERSION) {
println!("Kubie is up-to-date : v{}", VERSION);
} else {
println!(
"A new version of Kubie is available ({}), the new version will be automatically installed...",
"A new version of Kubie is available ({}), the new version will be installed by replacing this binary.",
latest_release.tag_name
);

let download_url = latest_release.get_binary_url().context("Sorry, this release has no build for your OS, please create an issue : https://github.com/sbstp/kubie/issues")?;
println!("Download url is: {}", download_url);

let resp = attohttpc::get(download_url).send()?;
if resp.is_success() {
let temp_file = tempfile::Builder::new().prefix("kubie").tempfile()?;
Expand All @@ -74,23 +80,33 @@ pub fn update() -> Result<()> {
let old_file = env::current_exe().expect("Could not get own binary path");
replace_file(&old_file, temp_file.path()).context("Update failed. Consider using sudo?")?;

println!(
"Kubie has been updated successfully. Enjoy :) ({})",
Path::display(&old_file)
);
println!("Kubie has been updated successfully: {}", Path::display(&old_file));
}
}
Ok(())
}

pub fn get_latest_release() -> Result<Release> {
let latest_release = attohttpc::get(&LATEST_RELEASE_URL).send()?.json()?;
Ok(latest_release)
}

pub fn replace_file(old_file: &Path, new_file: &Path) -> std::io::Result<()> {
fs::set_permissions(new_file, Permissions::from_mode(0o755))?;
fs::remove_file(old_file)?;
fs::copy(&new_file, old_file)?;
Ok(())
}

#[test]
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
fn test_binary_name() {
assert_eq!(Release::get_binary_name(), Some("kubie-linux-amd64"))
}

#[test]
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
fn test_binary_name() {
assert_eq!(Release::get_binary_name(), Some("kubie-darwin-amd64"))
}

#[test]
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
fn test_binary_name() {
assert_eq!(Release::get_binary_name(), Some("kubie-darwin-arm64"))
}