Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.

Commit 57ec3c3

Browse files
committed
file/folder exists
1 parent a598d49 commit 57ec3c3

File tree

6 files changed

+80
-12
lines changed

6 files changed

+80
-12
lines changed

Sources/CommandKit/Commands/File+Cmd.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ extension Cmd {
2929
return shell.run(bash: "which \(command)").trimMap()
3030
}
3131

32+
/// Check is folder is empty
33+
/// - Parameter path: Command
34+
public func isEmpty(path: String) -> EventLoopFuture<Bool> {
35+
return shell.run(bash: "[ '$(ls -A /path/to/directory)' ] && echo 'not empty' || echo 'empty'").map { output in
36+
return output.trimmingCharacters(in: .whitespacesAndNewlines) == "empty"
37+
}
38+
}
39+
3240
/// Check is command exists
3341
/// - Parameter command: Command
3442
public func exists(command: String) -> EventLoopFuture<Bool> {
@@ -92,8 +100,14 @@ extension Cmd {
92100

93101
/// Check if file exists
94102
/// - Parameter path: Path to the file
95-
public func exists(path: String) ->EventLoopFuture<Bool> {
96-
return shell.executor.exists(path: path)
103+
public func file(exists path: String) ->EventLoopFuture<Bool> {
104+
return shell.executor.file(exists: path)
105+
}
106+
107+
/// Check if folder exists
108+
/// - Parameter path: Path to the folder
109+
public func folder(exists path: String) ->EventLoopFuture<Bool> {
110+
return shell.executor.folder(exists: path)
97111
}
98112

99113
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Ondrej Rafaj on 15/07/2019.
6+
//
7+
8+
import Foundation
9+
import NIO
10+
11+
12+
extension Cmd {
13+
14+
/// Who Am I (whoami)
15+
public func whoami() -> EventLoopFuture<String> {
16+
return shell.run(bash: "whoami").trimMap()
17+
}
18+
19+
}

Sources/ShellKit/Executors/Executor.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public protocol Executor {
2828

2929
/// Check if file exists
3030
/// - Parameter path: Path to the file
31-
func exists(path: String) ->EventLoopFuture<Bool>
31+
func file(exists path: String) ->EventLoopFuture<Bool>
32+
33+
/// Check if folder exists
34+
/// - Parameter path: Path to the file
35+
func folder(exists path: String) ->EventLoopFuture<Bool>
3236

3337
/// Set current working directory
3438
/// - Parameter path: Path

Sources/ShellKit/Executors/LocalExecutor.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,18 @@ public class LocalExecutor: Executor {
119119

120120
/// Check if file exists
121121
/// - Parameter path: Path to the file
122-
public func exists(path: String) -> EventLoopFuture<Bool> {
123-
let exists = FileManager.default.fileExists(atPath: path)
124-
return eventLoop.makeSucceededFuture(exists)
122+
public func file(exists path: String) -> EventLoopFuture<Bool> {
123+
var dir: ObjCBool = ObjCBool(false)
124+
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &dir)
125+
return eventLoop.makeSucceededFuture(exists && !dir.boolValue)
126+
}
127+
128+
/// Check if folder exists
129+
/// - Parameter path: Path to the file
130+
public func folder(exists path: String) -> EventLoopFuture<Bool> {
131+
var dir: ObjCBool = ObjCBool(false)
132+
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &dir)
133+
return eventLoop.makeSucceededFuture(exists && dir.boolValue)
125134
}
126135

127136
/// Set current working directory

Sources/ShellKit/Executors/SSHExecutor.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,33 @@ public class SSHExecutor: Executor {
7676

7777
/// Check if file exists
7878
/// - Parameter path: Path to the file
79-
public func exists(path: String) -> EventLoopFuture<Bool> {
79+
public func file(exists path: String) -> EventLoopFuture<Bool> {
8080
let command = """
8181
FILE=\(path)
8282
if [ -f "$FILE" ]; then
83-
echo "$FILE exists"
83+
echo "exists"
8484
else
85-
echo "$FILE does not exist"
85+
echo "does not exist"
8686
fi
8787
"""
8888
return run(bash: command).map { result in
89-
return result.contains("\(path) exists")
89+
return result.trimmingCharacters(in: .whitespacesAndNewlines) == "exists"
90+
}
91+
}
92+
93+
/// Check if folder exists
94+
/// - Parameter path: Path to the file
95+
public func folder(exists path: String) -> EventLoopFuture<Bool> {
96+
let command = """
97+
DIR=\(path)
98+
if [ -d "$DIR" ]; then
99+
echo "exists"
100+
else
101+
echo "does not exist"
102+
fi
103+
"""
104+
return run(bash: command).map { result in
105+
return result.trimmingCharacters(in: .whitespacesAndNewlines) == "exists"
90106
}
91107
}
92108

Sources/ShellKit/Shell.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,14 @@ public class Shell: Executor {
133133

134134
/// Check if file exists
135135
/// - Parameter path: Path to the file
136-
public func exists(path: String) ->EventLoopFuture<Bool> {
137-
return executor.exists(path: path)
136+
public func file(exists path: String) ->EventLoopFuture<Bool> {
137+
return executor.file(exists: path)
138+
}
139+
140+
/// Check if folder exists
141+
/// - Parameter path: Path to the file
142+
public func folder(exists path: String) -> EventLoopFuture<Bool> {
143+
return executor.folder(exists: path)
138144
}
139145

140146
/// Set current working directory

0 commit comments

Comments
 (0)