Bump swift-actions/setup-swift from 1.22.0 to 1.23.0
Bumps swift-actions/setup-swift from 1.22.0 to 1.23.0.
updated-dependencies:
- dependency-name: swift-actions/setup-swift dependency-type: direct:production update-type: version-update:semver-minor …
Signed-off-by: dependabot[bot] support@github.com
Swift Sysctl
A Swift interface for reading (and writing)
sysctl
values.Installation
Add the following dependency to your
Package.swift
:Or add it via Xcode (as of Xcode 11).
Usage
Using Swift Sysctl is really easy. You just create a
SystemControl
instance and start accessing values. That’s it:Swift Sysctl contains a few common values and will also grow over time. If you find that you need a value that’s not yet present, you can easily add them. Just read through the following sections:
SysctlNamespace
As you might now,
sysctl
addresses values using a name that has dots in it. Swift Sysctl calls the parts between these dots “namespace” and represents them in Swiftstruct
s. So for example, there’sHardware
representing thehw
namespace insysctl
. Each namespace has a parent. If the namespace is located at the root, use theSysctlRootNamespace
as parent.When implementing your own namespaces, simply conform them to
SysctlNamespace
, define the parent and implementstatic var namePart: String { /*...*/ }
, returning the name part of your namespace (e.g.hw
forHardware
).Here’s an example for a new (imaginary) namespace that also has a child namespace:
SysctlNamespace.Field
To access a value from
sysctl
, there are (computed) properties on the namespaces. So for example forhw.machine
there’s a propertymachine
on theHardware
namespace. To access new values, simply declare a new (computed) property on the namespace the field is in (either in a namespace that you implemented on your own or by extending an existing one). The value type of these properties needs to beField<T>
whereT
is the type of value.Field
is a typealias forSysctlNamedField
inside a namespace. A field contains the last name part of the value’s name. Thus you simply return the name as string. If the value is writable, you also provide anonmutating set
implementation, which can be left empty.Continuing our example, here’s how fields on
Superpower
would look like:SysctlValue
andSysctlValueRepresentable
For Swift Sysctl to know how to read (or write) a value into
sysctl
, it needs to conform toSysctlValue
. However,sysctl
only supports very few value types, so it’s very unlikely that you need to conform another type to it.A bit more likely (but still not very likely) is that you want to have custom type that is represented by a value that already conforms to
SysctlValue
. In this case, theSysctlValueRepresentable
protocol is what your type needs to conform to. It behaves very similar to Swift’sRawRepresentable
protocol. You need to declare which underlyingSysctlValue
type your type will be using and implement the read-only propertyvar syctlValue: SysctlValue { get }
in which you return the underlyingSysctlValue
of your type. Then you ned to implementinit(sysctlValue:)
on your type. Swift Sysctl will pass the underlylingSysctlValue
to this initializer and you are left with initializing your type from it. With these requirements fulfilled, Swift Sysctl will provide the implementation forSysctlValue
for you and you can read and write your type from/tosysctl
.Again continuing our example, here’s how you would read a new type from
sysctl
:Possible Features
While not yet integrated, the following features might provide added value and could make it into this package in the future:
sysctl
(e.g. names depending on the number of cpus or network interfaces).sysctl
fails.Documentation
The API is documented using header doc. If you prefer to view the documentation as a webpage, there is an online version available for you.
Contributing
If you find a bug / like to see a new feature there are a few ways of helping out:
License
See LICENSE file.