Skip to content

Commit 5830e92

Browse files
authoredNov 2, 2022
Merge pull request #34 from cgwalters/arch-api
Add a `this_architecture()` API, drop nix dependency
2 parents 60a2220 + 24ed6b3 commit 5830e92

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed
 

‎Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ edition = "2018"
66
license = "Apache-2.0"
77

88
[dependencies]
9-
nix = { version = ">= 0.24, < 0.26", default-features = false, features = ["feature"] }
109
serde = { version = "^1.0", features = ["derive"] }
1110
strum = ">= 0.20, < 0.25"
1211
strum_macros = ">= 0.20, < 0.25"

‎src/lib.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,7 @@ pub struct Images {
164164
impl Stream {
165165
/// Returns the data for the CPU architecture matching the running process.
166166
pub fn this_architecture(&self) -> Option<&Arch> {
167-
self.architectures.get(
168-
// uname() shouldn't fail, and our return type assumes it won't.
169-
nix::sys::utsname::uname()
170-
.expect("couldn't get utsname")
171-
.machine()
172-
.to_str()
173-
.expect("utsname machine isn't UTF-8"),
174-
)
167+
self.architectures.get(this_architecture())
175168
}
176169

177170
/// Find a `disk` artifact.
@@ -192,3 +185,16 @@ impl Stream {
192185
.and_then(|(_fmt, v)| v.get("disk"))
193186
}
194187
}
188+
189+
/// Return the RPM/GNU architecture identifier for the current binary.
190+
///
191+
/// See also https://github.com/coreos/stream-metadata-go/blob/c5fe1b98ac1b1e6ab62a606b7580dc1f30703f83/arch/arch.go
192+
pub fn this_architecture() -> &'static str {
193+
match std::env::consts::ARCH {
194+
// Funny enough, PowerPC is so far the only weird case here.
195+
// For everything else, the Rust architecture is the same as RPM/GNU/Linux.
196+
"powerpc64" if cfg!(target_endian = "big") => "ppc64",
197+
"powerpc64" if cfg!(target_endian = "little") => "ppc64le",
198+
o => o,
199+
}
200+
}

‎tests/it/main.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ fn test_basic() {
1010
"https://builds.coreos.fedoraproject.org/streams/stable.json"
1111
);
1212

13-
let un = nix::sys::utsname::uname().unwrap();
14-
let myarch = un.machine().to_str().unwrap();
13+
let myarch = coreos_stream_metadata::this_architecture();
1514

1615
let st: Stream = serde_json::from_slice(STREAM_DATA).unwrap();
1716
assert_eq!(st.stream, "stable");

0 commit comments

Comments
 (0)