https://github.com/p-x9/swift-sysctl
A Swift wrapper for sysctl.
https://github.com/p-x9/swift-sysctl
swift sysctl
Last synced: about 2 months ago
JSON representation
A Swift wrapper for sysctl.
- Host: GitHub
- URL: https://github.com/p-x9/swift-sysctl
- Owner: p-x9
- License: mit
- Created: 2024-04-14T15:06:12.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-26T05:00:50.000Z (almost 2 years ago)
- Last Synced: 2024-05-09T11:19:04.938Z (almost 2 years ago)
- Topics: swift, sysctl
- Language: Swift
- Homepage:
- Size: 214 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swift-sysctl
A Swift wrapper for `sysctl`.
[](https://github.com/p-x9/swift-sysctl/issues)
[](https://github.com/p-x9/swift-sysctl/network/members)
[](https://github.com/p-x9/swift-sysctl/stargazers)
[](https://github.com/p-x9/swift-sysctl/)
## Usage
The basic functions are defined in the Sysctl namespace.
### Access
You can access specific values in the same way as for an oid name, in a chain of name, as follows:
```swift
// "kern.osversion"
let osVersion = Sysctl.sysctl(kern.osversion)
// "machdep.cpu.vendor"
let cpuVendorName = Sysctl.sysctl(machdep.cpu.vendor)
```
Each field also holds the type information of the value associated with its OID.
Therefore, in the above example, the value is automatically obtained as a String.
(See [Node directory](./Sources/SwiftSysctl/Node/))
> [!NOTE]
> Some values cannot be read without root permission.
> [!NOTE]
> OIDs that exist vary greatly depending on the CPU architecture.
> Some OIDs may not exist except on macOS.
## Implementation
The following is a brief description of the implementation.
### OID
In the [OID directory](./Sources/SwiftSysctl/OID/), the name, id, and value type information of the OID for each node are defined.
### Node
The OIDs provided to sysctl are structured as a tree.
Each node in the tree is defined in the [Node directory](./Sources/SwiftSysctl/Node/).
If a node has children, the aggregate type of the child nodes is retained as Node\ type.
```swift
public let ipc = Node(
oid: OID.Kern.ipc
)
```
If it is a terminal node, it has type information for the value associated with the OID.
```swift
public let ostype = LeafNode(
oid: OID.Kern.ostype
)
```
### Field
Field is the path traced from the root node to the terminal node.
(The root node is defined in [TopNodes.swift](./Sources/SwiftSysctl/TopNodes.swift).)
From the root node to the terminal node, it is possible to access the nodes by dot-connecting.
```swift
let field: Field = kern.ostype
let field2: Field = kern.argmax
```
The value can be retrieved by giving this `Field` to the `Sysctl.sysctl` function.
```swift
let ostype: String = Sysctl.sysctl(field)
let argmax: CInt = Sysctl.sysctl(field)
```
## License
swift-sysctl is released under the MIT License. See [LICENSE](./LICENSE)