Skip to content

Commit

Permalink
Clean JSON scheme for DeploymentDestination: use AbsolutePath instead…
Browse files Browse the repository at this point in the history
… of String
  • Loading branch information
Vladislav Alekseev committed Jun 20, 2021
1 parent 271ff7c commit fb51a5f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 32 deletions.
12 changes: 7 additions & 5 deletions Sources/Deployer/DeploymentDestination.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import PathLib
import QueueModels

public struct DeploymentDestination: Codable, CustomStringConvertible, Hashable {
Expand All @@ -7,7 +8,7 @@ public struct DeploymentDestination: Codable, CustomStringConvertible, Hashable
public let port: Int32
public let username: String
public let authentication: DeploymentDestinationAuthenticationType
public let remoteDeploymentPath: String
public let remoteDeploymentPath: AbsolutePath

enum CodingKeys: String, CodingKey {
case host
Expand All @@ -23,23 +24,24 @@ public struct DeploymentDestination: Codable, CustomStringConvertible, Hashable
let port = try container.decode(Int32.self, forKey: .port)
let username = try container.decode(String.self, forKey: .username)
let authentication = try container.decode(DeploymentDestinationAuthenticationType.self, forKey: .authentication)
let remoteDeploymentPath = try container.decode(String.self, forKey: .remoteDeploymentPath)
let remoteDeploymentPath = try container.decode(AbsolutePath.self, forKey: .remoteDeploymentPath)

self.init(
host: host,
port: port,
username: username,
authentication: authentication,
remoteDeploymentPath: remoteDeploymentPath)
remoteDeploymentPath: remoteDeploymentPath
)
}

public init(
host: String,
port: Int32,
username: String,
authentication: DeploymentDestinationAuthenticationType,
remoteDeploymentPath: String)
{
remoteDeploymentPath: AbsolutePath
) {
self.workerId = WorkerId(value: host)
self.host = host
self.port = port
Expand Down
24 changes: 7 additions & 17 deletions Sources/Deployer/DeploymentDestinationAuthenticationType.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Foundation
import PathLib

public enum DeploymentDestinationAuthenticationType: Codable, CustomStringConvertible, Equatable, Hashable {
case password(String)
case key(path: String)
case key(path: AbsolutePath)

public var description: String {
switch self {
Expand All @@ -14,37 +15,26 @@ public enum DeploymentDestinationAuthenticationType: Codable, CustomStringConver
}

private enum CodingKeys: String, CodingKey {
case type
case password
case path
}

private enum AuthType: String, Codable {
case plain
case key
case keyPath
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let type = try container.decode(AuthType.self, forKey: .type)

switch type {
case .plain:
do {
self = .password(try container.decode(String.self, forKey: .password))
case .key:
self = .key(path: try container.decode(String.self, forKey: .path))
} catch {
self = .key(path: try container.decode(AbsolutePath.self, forKey: .keyPath))
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .password(let password):
try container.encode(AuthType.plain, forKey: .type)
try container.encode(password, forKey: .password)
case .key(let path):
try container.encode(AuthType.key, forKey: .type)
try container.encode(path, forKey: .path)
try container.encode(path, forKey: .keyPath)
}
}
}
2 changes: 1 addition & 1 deletion Sources/SSHDeployer/DefaultSSHClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class DefaultSSHClient: SSHClient {
case .password(let password):
try ssh.authenticate(username: username, password: password)
case .key(let path):
try ssh.authenticate(username: username, privateKey: path)
try ssh.authenticate(username: username, privateKey: path.pathString)
}
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/SSHDeployer/SSHDeployer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public final class SSHDeployer: Deployer {
destination: DeploymentDestination,
deploymentId: String
) -> AbsolutePath {
return AbsolutePath(destination.remoteDeploymentPath)
.appending(components: [deploymentId, deployable.name])
return destination.remoteDeploymentPath.appending(
components: [deploymentId, deployable.name]
)
}

public static func remotePath(
Expand Down
5 changes: 3 additions & 2 deletions Tests/DeployerTestHelpers/DeploymentDestinationFixtures.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Deployer
import Foundation
import PathLib

public final class DeploymentDestinationFixtures {

public var host: String = "localhost"
public var port: Int32 = 42
public var username: String = "user"
public var authentication: DeploymentDestinationAuthenticationType = .password("pass")
public var remoteDeploymentPath: String = "/Users/username/path"
public var remoteDeploymentPath = AbsolutePath("/Users/username/path")

public init() {}

Expand All @@ -31,7 +32,7 @@ public final class DeploymentDestinationFixtures {
return self
}

public func with(remoteDeploymentPath: String) -> DeploymentDestinationFixtures {
public func with(remoteDeploymentPath: AbsolutePath) -> DeploymentDestinationFixtures {
self.remoteDeploymentPath = remoteDeploymentPath
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ final class QueueServerConfigurationTests: XCTestCase {
"port": 22,
"username": "q_user",
"authentication": {
"type": "plain",
"password": "pass"
},
"remoteDeploymentPath": "/remote/queue/depl/path"
Expand All @@ -54,7 +53,6 @@ final class QueueServerConfigurationTests: XCTestCase {
"port": 1,
"username": "username",
"authentication": {
"type": "plain",
"password": "pass"
},
"remoteDeploymentPath": "/remote/deployment/path"
Expand Down Expand Up @@ -107,8 +105,7 @@ final class QueueServerConfigurationTests: XCTestCase {
"port": 1,
"username": "username",
"authentication": {
"type": "key",
"path": "key"
"keyPath": "/path/to/key"
},
"remoteDeploymentPath": "/remote/deployment/path"
}
Expand All @@ -117,7 +114,7 @@ final class QueueServerConfigurationTests: XCTestCase {
let config = try JSONDecoder().decode(DeploymentDestination.self, from: data)
XCTAssertEqual(
config,
DeploymentDestination(host: "host", port: 1, username: "username", authentication: .key(path: "key"), remoteDeploymentPath: "/remote/deployment/path")
DeploymentDestination(host: "host", port: 1, username: "username", authentication: .key(path: "/path/to/key"), remoteDeploymentPath: "/remote/deployment/path")
)
}
}

0 comments on commit fb51a5f

Please sign in to comment.