SwiftSysctl

0.6.0

A Swift wrapper for sysctl.
p-x9/swift-sysctl

What's New

v0.6.0

2024-05-15T03:32:19Z

What's Changed

Full Changelog: 0.5.0...0.6.0

swift-sysctl

A Swift wrapper for sysctl.

Github issues Github forks Github stars Github top language

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:

// "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)

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, 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.

If a node has children, the aggregate type of the child nodes is retained as Node<Child> type.

public let ipc = Node<Ipc>(
    oid: OID.Kern.ipc
)

If it is a terminal node, it has type information for the value associated with the OID.

public let ostype = LeafNode<String>(
    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.)

From the root node to the terminal node, it is possible to access the nodes by dot-connecting.

let field: Field<String> = kern.ostype
let field2: Field<CInt> = kern.argmax

The value can be retrieved by giving this Field to the Sysctl.sysctl function.

let ostype: String = Sysctl.sysctl(field)
let argmax: CInt = Sysctl.sysctl(field)

License

swift-sysctl is released under the MIT License. See LICENSE

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri May 17 2024 14:51:19 GMT-0900 (Hawaii-Aleutian Daylight Time)