Skip to content

Commit 716db31

Browse files
committed
[CPFeature-009] Updated the CommitPrefix type to both conform to CPInterface and to incorporate Result types
1 parent 4dd0484 commit 716db31

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

Sources/CommitPrefix/CPInterfaceImpl.swift

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,29 @@ struct CommitPrefix {
3434

3535
static func interface() -> CPInterface { CommitPrefix() }
3636

37-
private func getInteractor() throws -> CPInteractor {
38-
guard Folder.current.containsSubfolder(named: FolderName.git) else {
39-
throw CPError.notAGitRepo(currentLocation: Folder.current.path)
40-
}
41-
let gitDirectory = try Folder.current.subfolder(named: FolderName.git)
42-
try CommitMessageHook.findOrCreate(with: gitDirectory)
43-
let cpInteractor = try CPInteractor(gitDirectory: gitDirectory)
44-
return cpInteractor
37+
private func getInteractor() -> Result<CPInteractor, CPError> {
38+
39+
guard
40+
Folder.current.containsSubfolder(named: FolderName.git),
41+
let gitDirectory = try? Folder.current.subfolder(named: FolderName.git)
42+
else { return .failure(.notAGitRepo(currentLocation: Folder.current.path)) }
43+
44+
return CommitMessageHook
45+
.findOrCreate(with: gitDirectory)
46+
.transform(gitDirectory)
47+
.flatMap(CPInteractor.create)
48+
4549
}
4650

4751
}
4852

4953
// MARK: - CPInterface Conformances
5054
extension CommitPrefix: CPInterface {
5155

52-
func outputPrefixes() throws -> ConslerOutput {
53-
let cpInteractor = try getInteractor()
54-
return try cpInteractor.outputPrefixes()
56+
func outputPrefixes() -> ConslerOutput {
57+
return getInteractor()
58+
.flatMap { $0.outputPrefixes() }
59+
.resolveOrExit()
5560
}
5661

5762
func outputVersion() -> ConslerOutput {
@@ -60,42 +65,49 @@ extension CommitPrefix: CPInterface {
6065
.describedBy(.normal, .cyan, .cyan)
6166
}
6267

63-
func viewState() throws -> ConslerOutput {
64-
let cpInteractor = try getInteractor()
65-
let cpState = try cpInteractor.getCommitPrefixState()
66-
switch cpState.mode {
67-
case .normal:
68-
return ConslerOutput(
69-
"CommitPrefix ", "MODE NORMAL",
70-
"- prefixes: ", cpState.normalPrefixes.joined())
71-
.describedBy(.normal, .cyanEndsLine, .normal, .cyan)
72-
case .branchParse:
73-
return ConslerOutput(
74-
"CommitPrefix ", "MODE BRANCH_PARSE",
75-
"- branch prefixes: ", cpState.branchPrefixes.joined(),
76-
"- stored prefixes: ", cpState.normalPrefixes.joined())
77-
.describedBy(.normal, .cyanEndsLine, .normal, .cyanEndsLine, .normal, .cyan)
68+
func viewState() -> ConslerOutput {
69+
return getInteractor()
70+
.flatMap { $0.getCommitPrefixState() }
71+
.map { cpState in
72+
switch cpState.mode {
73+
case .normal:
74+
return ConslerOutput(
75+
"CommitPrefix ", "MODE NORMAL",
76+
"- prefixes: ", cpState.normalPrefixes.joined())
77+
.describedBy(.normal, .cyanEndsLine, .normal, .cyan)
78+
case .branchParse:
79+
return ConslerOutput(
80+
"CommitPrefix ", "MODE BRANCH_PARSE",
81+
"- branch prefixes: ", cpState.branchPrefixes.joined(),
82+
"- stored prefixes: ", cpState.normalPrefixes.joined())
83+
.describedBy(.normal, .cyanEndsLine, .normal, .cyanEndsLine, .normal, .cyan)
84+
}
7885
}
86+
.resolveOrExit()
7987
}
8088

81-
func deletePrefixes() throws -> ConslerOutput {
82-
let cpInteractor = try getInteractor()
83-
return try cpInteractor.deletePrefixes()
89+
func deletePrefixes() -> ConslerOutput {
90+
return getInteractor()
91+
.flatMap { $0.deletePrefixes() }
92+
.resolveOrExit()
8493
}
8594

86-
func writeNew(prefixes rawValue: String) throws -> ConslerOutput {
87-
let cpInteractor = try getInteractor()
88-
return try cpInteractor.writeNew(prefixes: rawValue)
95+
func writeNew(prefixes rawValue: String) -> ConslerOutput {
96+
return getInteractor()
97+
.flatMap { $0.writeNew(prefixes: rawValue) }
98+
.resolveOrExit()
8999
}
90100

91-
func activateBranchMode(with validator: String) throws -> ConslerOutput {
92-
let cpInteractor = try getInteractor()
93-
return try cpInteractor.activateBranchMode(with: validator)
101+
func activateBranchMode(with validator: String) -> ConslerOutput {
102+
return getInteractor()
103+
.flatMap { $0.activateBranchMode(with: validator) }
104+
.resolveOrExit()
94105
}
95106

96-
func activateNormalMode() throws -> ConslerOutput {
97-
let cpInteractor = try getInteractor()
98-
return try cpInteractor.activateNormalMode()
107+
func activateNormalMode() -> ConslerOutput {
108+
return getInteractor()
109+
.flatMap { $0.activateNormalMode() }
110+
.resolveOrExit()
99111
}
100112

101113
}

0 commit comments

Comments
 (0)