Skip to content

Commit

Permalink
file/folder exists
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Jul 15, 2019
1 parent a598d49 commit 57ec3c3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
18 changes: 16 additions & 2 deletions Sources/CommandKit/Commands/File+Cmd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ extension Cmd {
return shell.run(bash: "which \(command)").trimMap()
}

/// Check is folder is empty
/// - Parameter path: Command
public func isEmpty(path: String) -> EventLoopFuture<Bool> {
return shell.run(bash: "[ '$(ls -A /path/to/directory)' ] && echo 'not empty' || echo 'empty'").map { output in
return output.trimmingCharacters(in: .whitespacesAndNewlines) == "empty"
}
}

/// Check is command exists
/// - Parameter command: Command
public func exists(command: String) -> EventLoopFuture<Bool> {
Expand Down Expand Up @@ -92,8 +100,14 @@ extension Cmd {

/// Check if file exists
/// - Parameter path: Path to the file
public func exists(path: String) ->EventLoopFuture<Bool> {
return shell.executor.exists(path: path)
public func file(exists path: String) ->EventLoopFuture<Bool> {
return shell.executor.file(exists: path)
}

/// Check if folder exists
/// - Parameter path: Path to the folder
public func folder(exists path: String) ->EventLoopFuture<Bool> {
return shell.executor.folder(exists: path)
}

}
19 changes: 19 additions & 0 deletions Sources/CommandKit/Commands/System+Cmd.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// File.swift
//
//
// Created by Ondrej Rafaj on 15/07/2019.
//

import Foundation
import NIO


extension Cmd {

/// Who Am I (whoami)
public func whoami() -> EventLoopFuture<String> {
return shell.run(bash: "whoami").trimMap()
}

}
6 changes: 5 additions & 1 deletion Sources/ShellKit/Executors/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public protocol Executor {

/// Check if file exists
/// - Parameter path: Path to the file
func exists(path: String) ->EventLoopFuture<Bool>
func file(exists path: String) ->EventLoopFuture<Bool>

/// Check if folder exists
/// - Parameter path: Path to the file
func folder(exists path: String) ->EventLoopFuture<Bool>

/// Set current working directory
/// - Parameter path: Path
Expand Down
15 changes: 12 additions & 3 deletions Sources/ShellKit/Executors/LocalExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,18 @@ public class LocalExecutor: Executor {

/// Check if file exists
/// - Parameter path: Path to the file
public func exists(path: String) -> EventLoopFuture<Bool> {
let exists = FileManager.default.fileExists(atPath: path)
return eventLoop.makeSucceededFuture(exists)
public func file(exists path: String) -> EventLoopFuture<Bool> {
var dir: ObjCBool = ObjCBool(false)
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &dir)
return eventLoop.makeSucceededFuture(exists && !dir.boolValue)
}

/// Check if folder exists
/// - Parameter path: Path to the file
public func folder(exists path: String) -> EventLoopFuture<Bool> {
var dir: ObjCBool = ObjCBool(false)
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &dir)
return eventLoop.makeSucceededFuture(exists && dir.boolValue)
}

/// Set current working directory
Expand Down
24 changes: 20 additions & 4 deletions Sources/ShellKit/Executors/SSHExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,33 @@ public class SSHExecutor: Executor {

/// Check if file exists
/// - Parameter path: Path to the file
public func exists(path: String) -> EventLoopFuture<Bool> {
public func file(exists path: String) -> EventLoopFuture<Bool> {
let command = """
FILE=\(path)
if [ -f "$FILE" ]; then
echo "$FILE exists"
echo "exists"
else
echo "$FILE does not exist"
echo "does not exist"
fi
"""
return run(bash: command).map { result in
return result.contains("\(path) exists")
return result.trimmingCharacters(in: .whitespacesAndNewlines) == "exists"
}
}

/// Check if folder exists
/// - Parameter path: Path to the file
public func folder(exists path: String) -> EventLoopFuture<Bool> {
let command = """
DIR=\(path)
if [ -d "$DIR" ]; then
echo "exists"
else
echo "does not exist"
fi
"""
return run(bash: command).map { result in
return result.trimmingCharacters(in: .whitespacesAndNewlines) == "exists"
}
}

Expand Down
10 changes: 8 additions & 2 deletions Sources/ShellKit/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,14 @@ public class Shell: Executor {

/// Check if file exists
/// - Parameter path: Path to the file
public func exists(path: String) ->EventLoopFuture<Bool> {
return executor.exists(path: path)
public func file(exists path: String) ->EventLoopFuture<Bool> {
return executor.file(exists: path)
}

/// Check if folder exists
/// - Parameter path: Path to the file
public func folder(exists path: String) -> EventLoopFuture<Bool> {
return executor.folder(exists: path)
}

/// Set current working directory
Expand Down

0 comments on commit 57ec3c3

Please sign in to comment.