Skip to content

Commit

Permalink
Reusing CommandKit
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Jul 14, 2019
1 parent d5deada commit 3ebbece
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 50 deletions.
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
"repositoryURL": "https://github.com/Einstore/ShellKit.git",
"state": {
"branch": null,
"revision": "0e7e238feeac519211881afa66bfd75add84825c",
"version": "1.0.0"
"revision": "e0a36d3d1622de65f9572130e0824e57c9241273",
"version": "1.1.0"
}
},
{
Expand All @@ -105,8 +105,8 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "7f20464df31e85f86bedf4a8afdd1785e0cb5059",
"version": "2.3.0"
"revision": "334815f6580b5256409d48431751938c4e966e72",
"version": "2.4.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let package = Package(
.target(
name: "SystemManager",
dependencies: [
"ShellKit",
"CommandKit",
"SystemModel"
]
),
Expand Down
40 changes: 5 additions & 35 deletions Sources/SystemManager/Model/Os.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,10 @@
// Created by Ondrej Rafaj on 09/07/2019.
//

import Foundation
import CommandKit


public enum Os: Command {

case macOs

case linux

case cygwin

case minGW

case other(String)

public static var command: String {
return "uname -s"
}
extension Cmd.Os: Command {

public func serialize() -> String {
switch self {
Expand All @@ -39,7 +25,7 @@ public enum Os: Command {
}
}

public static func deserialize(_ string: String) -> Os {
public static func deserialize(_ string: String) -> Cmd.Os {
switch true {
case string == "macOs":
return .macOs
Expand All @@ -56,30 +42,14 @@ public enum Os: Command {
}
}

public static func parse(_ string: String) -> Os {
let string = string.trimmingCharacters(in: .whitespacesAndNewlines)
switch true {
case string == "Darwin":
return .macOs
case string == "Linux":
return .linux
case string == "CYGWIN":
return .cygwin
case string == "MINGW":
return .minGW
default:
return .other(string)
}
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
self = Os.deserialize(string)
self = Cmd.Os.deserialize(string)
}

public init(from string: String) {
self = Os.deserialize(string)
self = Cmd.Os.deserialize(string)
}

public func encode(to encoder: Encoder) throws {
Expand Down
14 changes: 4 additions & 10 deletions Sources/SystemManager/SystemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Ondrej Rafaj on 09/07/2019.
//

import ShellKit
import CommandKit
import SystemModel


Expand All @@ -25,13 +25,7 @@ public class SystemManager {
shell = try Shell(conn, on: eventLoop)
}

public func os() -> EventLoopFuture<Os> {
return shell.run(bash: Os.command).map { output in
return Os.parse(output)
}
}

public func cores(os: Os) -> EventLoopFuture<SystemInfo.CPU> {
public func cores(os: Cmd.Os) -> EventLoopFuture<SystemInfo.CPU> {
switch os {
case .macOs:
return self.shell.run(bash: Sysctl.command).map { output in
Expand Down Expand Up @@ -62,7 +56,7 @@ public class SystemManager {
}
}

public func stats(os: Os) -> EventLoopFuture<SystemInfo.Stats> {
public func stats(os: Cmd.Os) -> EventLoopFuture<SystemInfo.Stats> {
switch os {
case .macOs:
return self.shell.run(bash: SystemInfo.Stats.Mac.command).map { output in
Expand All @@ -80,7 +74,7 @@ public class SystemManager {
}

public func info() -> EventLoopFuture<SystemInfo> {
return os().flatMap { os in
return shell.cmd.os().flatMap { os in
switch os {
case .macOs:
return self.macOsInfo()
Expand Down

0 comments on commit 3ebbece

Please sign in to comment.