Skip to content

Commit

Permalink
rename to Dpkg
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed Dec 28, 2024
1 parent da808d2 commit de1cbf6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
10 changes: 5 additions & 5 deletions Sources/Formic/ResourceTypes/Dpkg+Parsers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Parsing
// https://swiftpackageindex.com/pointfreeco/swift-parsing#user-content-getting-started
// https://pointfreeco.github.io/swift-parsing/0.10.0/documentation/parsing/

extension DpkgState {
extension Dpkg {
// parsing individual lines
struct PackageCodes: Parser {
var body: some Parser<Substring, (DesiredState, StatusCode, ErrCode)> {
Expand Down Expand Up @@ -32,8 +32,8 @@ extension DpkgState {
}

struct PackageStatus: Parser {
var body: some Parser<Substring, DpkgState> {
Parse(DpkgState.init) {
var body: some Parser<Substring, Dpkg> {
Parse(Dpkg.init) {
PackageCodes()
Skip {
// whitespace
Expand Down Expand Up @@ -82,8 +82,8 @@ extension DpkgState {
}

struct PackageList: Parser {
var body: some Parser<Substring, [DpkgState]> {
DpkgState.DpkgHeader()
var body: some Parser<Substring, [Dpkg]> {
Dpkg.DpkgHeader()
Many(1...) {
PackageStatus()
} separator: {
Expand Down
26 changes: 13 additions & 13 deletions Sources/Formic/ResourceTypes/Dpkg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Parsing
//ii docker-ce 5:27.3.1-1~ubuntu.24.04~noble arm64 Docker: the open-source application container engine

/// A debian package resource.
public struct DpkgState: Sendable, Hashable, Resource {
public struct Dpkg: Sendable, Hashable, Resource {
/// Returns an inquiry command that retrieves the output to parse into a resource.
/// - Parameter name: The name of the resource to find.
public static func namedInquiry(_ name: String) -> (any Command) {
Expand All @@ -31,11 +31,11 @@ public struct DpkgState: Sendable, Hashable, Resource {
/// Returns the state of the resource from the output of the shell command.
/// - Parameter output: The string output of the shell command.
/// - Throws: Any errors parsing the output.
public static func parse(_ output: Data) throws -> DpkgState {
public static func parse(_ output: Data) throws -> Dpkg {
guard let stringFromData: String = String(data: output, encoding: .utf8) else {
throw ResourceError.notAString
}
let _ = try DpkgState.PackageList().parse(Substring(stringFromData))
let _ = try Dpkg.PackageList().parse(Substring(stringFromData))
fatalError("not implemented")
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public struct DpkgState: Sendable, Hashable, Resource {
public var retry: Backoff
public var executionTimeout: Duration
public func run(host: Host) async throws -> CommandOutput {
if try await DpkgState.resolve(state: self, on: host) {
if try await Dpkg.resolve(state: self, on: host) {
return .generalSuccess(msg: "Resolved")
} else {
return .generalFailure(msg: "Failed")
Expand Down Expand Up @@ -127,45 +127,45 @@ public struct DpkgState: Sendable, Hashable, Resource {
}
}

extension DpkgState: CollectionResource {
extension Dpkg: CollectionResource {
/// The shell command to use to get the state for this resource.
public static var collectionInquiry: (any Command) {
ShellCommand("dpkg -l")
}

/// Returns a list of resources from the string output from a command.
/// - Parameter output: The output from the command.
public static func collectionParse(_ output: Data) throws -> [DpkgState] {
public static func collectionParse(_ output: Data) throws -> [Dpkg] {
guard let stringFromData: String = String(data: output, encoding: .utf8) else {
throw ResourceError.notAString
}
let collection = try DpkgState.PackageList().parse(Substring(stringFromData))
let collection = try Dpkg.PackageList().parse(Substring(stringFromData))
return collection
}
}

extension DpkgState: StatefulResource {
extension Dpkg: StatefulResource {
/// Queries and returns the state of the resource identified by a declaration you provide.
/// - Parameters:
/// - state: The declaration that identifies the resource.
/// - host: The host on which to find the resource.
public static func query(state: DebianPackageDeclaration, from host: Host) async throws -> (DpkgState, Date) {
return try await DpkgState.query(state.name, from: host)
public static func query(state: DebianPackageDeclaration, from host: Host) async throws -> (Dpkg, Date) {
return try await Dpkg.query(state.name, from: host)
}

/// Queries and attempts to resolve the update to the desired state you provide.
/// - Parameters:
/// - state: The declaration that identifies the resource and its desired state.
/// - host: The host on which to resolve the resource.
public static func resolve(state: DebianPackageDeclaration, on host: Host) async throws -> Bool {
let (currentState, _) = try await DpkgState.query(state.name, from: host)
let (currentState, _) = try await Dpkg.query(state.name, from: host)
switch state.declaredState {
case .present:
if currentState.desiredState == .install && currentState.statusCode == .installed {
return true
} else {
try await ShellCommand("apt-get install \(state.name)").run(host: host)
let (updatedState, _) = try await DpkgState.query(state.name, from: host)
let (updatedState, _) = try await Dpkg.query(state.name, from: host)
if updatedState.desiredState == .install && updatedState.statusCode == .installed {
return true
} else {
Expand All @@ -181,7 +181,7 @@ extension DpkgState: StatefulResource {
} else {
// do the removal
try await ShellCommand("apt-get remove \(state.name)").run(host: host)
let (updatedState, _) = try await DpkgState.query(state.name, from: host)
let (updatedState, _) = try await Dpkg.query(state.name, from: host)
if (updatedState.desiredState == .unknown || updatedState.desiredState == .remove)
&& updatedState.statusCode == .notInstalled
{
Expand Down
10 changes: 5 additions & 5 deletions Tests/formicTests/Resources/PackagesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let bigSample = """
func verifyParsingOneLine() async throws {
let sample =
"ii apt 2.7.14build2 arm64 commandline package manager"
let result = try DpkgState.PackageStatus().parse(sample)
let result = try Dpkg.PackageStatus().parse(sample)
//print(result)
#expect(result.name == "apt")
#expect(result.version == "2.7.14build2")
Expand All @@ -54,7 +54,7 @@ func verifyHeaderParse() async throws {
what?
"""
var x: Substring = headerSample[...]
try DpkgState.DpkgHeader().parse(&x)
try Dpkg.DpkgHeader().parse(&x)
#expect(x == "what?")
}

Expand All @@ -69,7 +69,7 @@ func verifyParsingUnknownSinglePackage() async throws {
un docker <none> <none> (no description available)
"""

let result: [DpkgState] = try DpkgState.PackageList().parse(sampleOutput)
let result: [Dpkg] = try Dpkg.PackageList().parse(sampleOutput)
//print(result)
#expect(result.count == 1)
}
Expand All @@ -86,14 +86,14 @@ func verifyParsingKnownSinglePackage() async throws {
ii docker-ce 5:27.4.0-1~ubuntu.24.04~noble arm64 Docker: the open-source application container engine
"""

let result: [DpkgState] = try DpkgState.PackageList().parse(sampleOutput)
let result: [Dpkg] = try Dpkg.PackageList().parse(sampleOutput)
//print(result)
#expect(result.count == 1)
}

@Test("package parsing - dpkg output")
func verifyParsingMultilineOutputString() async throws {
let result: [DpkgState] = try DpkgState.PackageList().parse(bigSample)
let result: [Dpkg] = try Dpkg.PackageList().parse(bigSample)
//print(result)
#expect(result.count == 19)
}

0 comments on commit de1cbf6

Please sign in to comment.