Skip to content

Commit 3155be9

Browse files
authored
Merge pull request #70 from toshi0383/refactor
Minor refactoring
2 parents 92fa541 + 8510f6c commit 3155be9

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

Sources/cmdshelf/Commands.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ final class CatCommand: Command {
7777
let aliases = try VaradicAliasArgument().parse(parser)
7878

7979
if aliases.isEmpty {
80-
exit(shellOut(to: "cat"))
80+
exit(shellOut("cat"))
8181

8282
} else {
8383

@@ -96,12 +96,12 @@ final class CatCommand: Command {
9696
}
9797

9898
if context.location.hasPrefix("curl ") {
99-
if shellOut(to: context.location) > 0 {
99+
if shellOut(context.location) > 0 {
100100
failure = true
101101
}
102102

103103
} else {
104-
if shellOut(to: "cat \(context.location)") > 0 {
104+
if shellOut("cat \(context.location)") > 0 {
105105
failure = true
106106
}
107107
}
@@ -153,9 +153,9 @@ final class RunCommand: Command {
153153

154154
let singleQuoted = parameters.map { "\'\($0)\'" }.joined(separator: " ")
155155
if context.location.hasPrefix("curl ") {
156-
exit(shellOut(to: "bash <(\(context.location))", argument: singleQuoted))
156+
exit(shellOut("bash <(\(context.location))", argument: singleQuoted))
157157
} else {
158-
exit(shellOut(to: context.location, argument: singleQuoted))
158+
exit(shellOut(context.location, argument: singleQuoted))
159159
}
160160
}
161161
}

Sources/cmdshelf/Configuration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Configuration {
9090

9191
queuedPrint("[\(repo.name)] Cloning ... ")
9292

93-
let status = silentShellOut(to: "git clone \(repo.url) \(workspace)")
93+
let status = silentShellOut("git clone \(repo.url) \(workspace)")
9494

9595
if status != 0 {
9696
queuedPrintlnError("error")
@@ -110,7 +110,7 @@ class Configuration {
110110

111111
queuedPrint("[\(repo.name)] Updating ... ")
112112

113-
let status = silentShellOut(to: "cd \(workspace) && git fetch origin master && git checkout origin/master")
113+
let status = silentShellOut("cd \(workspace) && git fetch origin master && git checkout origin/master")
114114

115115
if status != 0 {
116116
queuedPrintlnError("error")

Sources/cmdshelf/Functions.swift

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import Foundation
2-
import Poxis
3-
import Reporter
42

53
@discardableResult
6-
func silentShellOut(to: String, argument: String? = nil) -> Int32 {
7-
return shellOut(to: to, argument: argument, shouldPrintStdout: false, shouldPrintError: false)
4+
func silentShellOut(_ to: String, argument: String? = nil) -> Int32 {
5+
return shellOut(to, argument: argument, shouldPrintStdout: false, shouldPrintError: false)
86
}
97

108
@discardableResult
11-
func shellOut(to: String, argument: String? = nil, shouldPrintStdout: Bool = true, shouldPrintError: Bool = true) -> Int32 {
9+
func shellOut(_ to: String, argument: String? = nil, shouldPrintStdout: Bool = true, shouldPrintError: Bool = true) -> Int32 {
1210
let process = Process()
1311
process.launchPath = "/bin/bash"
1412
if let arg = argument {
@@ -43,21 +41,3 @@ func shellOut(to: String, argument: String? = nil, shouldPrintStdout: Bool = tru
4341
#endif
4442
return process.terminationStatus
4543
}
46-
47-
/// TODO: improve error handling
48-
@discardableResult
49-
func spawnPager(cmdString: String) -> Int32 {
50-
errno = 0
51-
guard let fpin = poxis_popen(cmdString, "r") else {
52-
return 1
53-
}
54-
guard let fpout = poxis_popen("${PAGER:-more}", "w") else {
55-
return 1
56-
}
57-
var line: [Int8] = []
58-
while fgets(&line, 4096, fpin) != nil {
59-
fputs(&line, fpout)// == EOF then error
60-
}
61-
poxis_pclose(fpout)
62-
return poxis_pclose(fpin)
63-
}

Sources/cmdshelf/SpawnPager.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Foundation
2+
import Poxis
3+
4+
/// TODO: improve error handling
5+
@discardableResult
6+
func spawnPager(cmdString: String) -> Int32 {
7+
errno = 0
8+
guard let fpin = poxis_popen(cmdString, "r") else {
9+
return 1
10+
}
11+
guard let fpout = poxis_popen("${PAGER:-more}", "w") else {
12+
return 1
13+
}
14+
var line: [Int8] = []
15+
while fgets(&line, 4096, fpin) != nil {
16+
fputs(&line, fpout)// == EOF then error
17+
}
18+
poxis_pclose(fpout)
19+
return poxis_pclose(fpin)
20+
}

0 commit comments

Comments
 (0)