-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0911904
commit 4a96402
Showing
21 changed files
with
297 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
AeroSpace.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Foundation | ||
import Darwin | ||
|
||
#if DEBUG | ||
let appId: String = "bobko.debug.aerospace" | ||
#else | ||
let appId: String = "bobko.aerospace" | ||
#endif | ||
|
||
public func error(_ message: String = "") -> Never { | ||
errorT(message) | ||
} | ||
|
||
public func errorT<T>(_ message: String = "") -> T { | ||
print(message) | ||
exit(1) | ||
} | ||
|
||
let cliClientVersionAndHash: String = "\(cliClientVersion) \(gitHash)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// BEWARE! This file is auto-updated by build-release.sh | ||
public let gitHash = "DEBUG" | ||
public let gitShortHash = "DEBUG" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,57 @@ | ||
import Socket | ||
import Foundation | ||
|
||
print("Hello, World!") | ||
let command: [String] = Array(CommandLine.arguments.dropFirst()) | ||
|
||
for word in command { | ||
if word.contains(" ") { | ||
error("Spaces in arguments are not permitted. '\(word)' argument contains spaces.") | ||
} | ||
} | ||
|
||
let usage = | ||
""" | ||
USAGE: \(CommandLine.arguments.first ?? "aerospace") COMMAND | ||
See https://github.com/nikitabobko/AeroSpace/blob/main/docs/commands.md for the list of all available commands | ||
""" | ||
if command.first == "--help" || command.first == "-h" { | ||
print(usage) | ||
} else { | ||
let socket = try! Socket.create(family: .unix, type: .stream, proto: .unix) | ||
defer { | ||
socket.close() | ||
} | ||
let socketFile = "/tmp/\(appId).sock" | ||
(try? socket.connect(to: socketFile)) ?? | ||
errorT("Can't connect to AeroSpace server. Is AeroSpace.app running?") | ||
|
||
func run(_ command: String) -> String { | ||
try! socket.write(from: command) | ||
_ = try! Socket.wait(for: [socket], timeout: 0, waitForever: true) | ||
return try! socket.readString() ?? errorT("fatal error: received nil from socket") | ||
} | ||
|
||
let serverVersionAndHash = run("version") | ||
if serverVersionAndHash != cliClientVersionAndHash { | ||
error( | ||
""" | ||
Corrupted AeroSpace installation | ||
- CLI client version: \(cliClientVersionAndHash) | ||
- AeroSpace.app server version: \(serverVersionAndHash) | ||
The versions don't match. Please reinstall AeroSpace | ||
""" | ||
) | ||
} | ||
|
||
if command.isEmpty { | ||
error(usage) | ||
} else { | ||
let output = run(command.joined(separator: " ")) | ||
if output != "PASS" { | ||
print(output) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// FILE IS GENERATED BY generate.sh | ||
let cliClientVersion = "0.4.0-Beta" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# CLI commands | ||
|
||
In addition to [regular commands](./commands.md), the CLI provides commands listed in this file | ||
|
||
**Table of contents** | ||
- [version](#version) | ||
|
||
## version | ||
|
||
``` | ||
version | ||
--version | ||
-v | ||
``` | ||
|
||
- Available since: 0.4.0-Beta | ||
|
||
Prints the version and commit hash to stdout | ||
|
||
This command doesn't have any arguments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
set -e # Exit if one of commands exit with non-zero exit code | ||
set -u # Treat unset variables and parameters other than the special parameters ‘@’ or ‘*’ as an error | ||
set -o pipefail # Any command failed in the pipe fails the whole pipe | ||
# set -x # Print shell commands as they are executed (or you can try -v which is less verbose) | ||
|
||
cd "$(dirname "$0")" | ||
|
||
version=$(head -1 ./version.txt) # Build number CFBundleVersion | ||
build_number=$(tail -1 ./version.txt) # User visible version CFBundleShortVersionString | ||
|
||
cat > cli/versionGenerated.swift <<EOF | ||
// FILE IS GENERATED BY generate.sh | ||
let cliClientVersion = "$version" | ||
EOF | ||
|
||
sed -i "s/CURRENT_PROJECT_VERSION.*/CURRENT_PROJECT_VERSION: $build_number # GENERATED BY generate.sh/" project.yml | ||
sed -i "s/MARKETING_VERSION.*/MARKETING_VERSION: $version # GENERATED BY generate.sh/" project.yml | ||
|
||
xcodegen # https://github.com/yonaskolb/XcodeGen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
struct VersionCommand: QueryCommand { | ||
@MainActor | ||
func run() -> String { | ||
check(Thread.current.isMainThread) | ||
return "\(Bundle.appVersion) \(gitHash)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Socket | ||
|
||
func startServer() { | ||
let socket = (try? Socket.create(family: .unix, type: .stream, proto: .unix)) ?? errorT("Can't create socket") | ||
let socketFile = "/tmp/\(Bundle.appId).sock" | ||
(try? socket.listen(on: socketFile, maxBacklogSize: 1)) ?? errorT("Can't listen to socket \(socketFile)") | ||
DispatchQueue.global().async { | ||
while true { | ||
guard let connection = try? socket.acceptClientConnection() else { continue } | ||
Task { await newConnection(connection) } | ||
} | ||
} | ||
} | ||
|
||
private func newConnection(_ socket: Socket) async { | ||
defer { | ||
debug("Close connection") | ||
socket.close() | ||
} | ||
while true { | ||
_ = try? Socket.wait(for: [socket], timeout: 0, waitForever: true) | ||
guard let string = (try? socket.readString()) else { return } | ||
let (action, error1) = parseSingleCommand(string).getOrNils() | ||
let (query, error2) = parseQueryCommand(string).getOrNils() | ||
if let error1, let error2 { | ||
_ = try? socket.write(from: error1 + "\n" + error2) | ||
continue | ||
} | ||
if action is ExecAndForgetCommand || action is ExecAndWaitCommand { | ||
_ = try? socket.write(from: "exec commands are prohibited from CLI") | ||
continue | ||
} | ||
if let action { | ||
await action.run() | ||
_ = try? socket.write(from: "PASS") | ||
continue | ||
} | ||
if let query { | ||
let result = await query.run() | ||
_ = try? socket.write(from: result) | ||
continue | ||
} | ||
error("Unreachable") | ||
} | ||
} |
Oops, something went wrong.