🆕 Add basic config lookup logic

This commit is contained in:
Anees Iqbal
2024-09-02 17:08:56 +04:00
parent 850ae2890b
commit eb22bb3d22
6 changed files with 179 additions and 3 deletions

52
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,52 @@
{
"configurations": [
{
"type": "lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"args": [],
"cwd": "${workspaceFolder:ffmpeg-over-ip-swift}",
"name": "Debug server",
"program": "${workspaceFolder:ffmpeg-over-ip-swift}/.build/debug/server",
"preLaunchTask": "swift: Build Debug server"
},
{
"type": "lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"args": [],
"cwd": "${workspaceFolder:ffmpeg-over-ip-swift}",
"name": "Release server",
"program": "${workspaceFolder:ffmpeg-over-ip-swift}/.build/release/server",
"preLaunchTask": "swift: Build Release server"
},
{
"type": "lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"args": [],
"cwd": "${workspaceFolder:ffmpeg-over-ip-swift}",
"name": "Debug client",
"program": "${workspaceFolder:ffmpeg-over-ip-swift}/.build/debug/client",
"preLaunchTask": "swift: Build Debug client"
},
{
"type": "lldb",
"request": "launch",
"sourceLanguages": [
"swift"
],
"args": [],
"cwd": "${workspaceFolder:ffmpeg-over-ip-swift}",
"name": "Release client",
"program": "${workspaceFolder:ffmpeg-over-ip-swift}/.build/release/client",
"preLaunchTask": "swift: Build Release client"
}
]
}

1
Sources/client/common Symbolic link
View File

@@ -0,0 +1 @@
../common

View File

@@ -0,0 +1,60 @@
import Foundation
enum Runtime {
case Server
case Client
}
let configServerLong: String = "ffmpeg-over-ip.server.json"
let configServerShort: String = "config.server.json"
let configClientLong: String = "ffmpeg-over-ip.client.json"
let configClientShort: String = "config.client.json"
func getConfigPaths(runtime: Runtime) -> [String] {
let configLong = runtime == .Server ? configServerLong : configClientLong
let configShort = runtime == .Server ? configServerShort : configClientShort
let currentDirectory: String = FileManager.default.currentDirectoryPath
let userDirectory: String = FileManager.default.homeDirectoryForCurrentUser.path
var paths: [(directory: String, file: String)] = [
(directory: currentDirectory, file: configLong),
]
if userDirectory != "" {
let userConfigDirectory: String = NSString.path(withComponents: [userDirectory, ".config"])
let userScopedConfigDirectory: String = NSString.path(withComponents: [userConfigDirectory, "ffmpeg-over-ip"])
paths.append((directory: userScopedConfigDirectory, file: configShort))
paths.append((directory: userConfigDirectory, file: configLong))
}
paths.append((directory: "/etc/ffmpeg-over-ip", file: configShort))
paths.append((directory: "/etc", file: configLong))
// Collapse the paths into a single array
let filePaths: [String] = paths.map { (directory: String, file: String) -> String in
NSString.path(withComponents: [directory, file])
}
return filePaths
}
func getConfig(filePaths: [String]) -> String? {
var configPath: String? = nil
// Find the first file that exists from the paths
for filePath in filePaths {
if FileManager.default.fileExists(atPath: filePath) {
configPath = filePath
break
}
}
if configPath == nil {
return nil
}
print(filePaths)
return configPath
}

29
Sources/common/help.swift Normal file
View File

@@ -0,0 +1,29 @@
let HELP_SERVER_VERSION: String = "ffmpeg-over-ip server 3.0.0"
let HELP_CLIENT_VERSION: String = "ffmpeg-over-ip client 3.0.0"
let HELP_TEXT_CLIENT: String = """
\(HELP_CLIENT_VERSION)
Usage: ffmpeg-over-ip-client [options]
ffmpeg-over-ip-client <...ffmpeg options>
Options:
--debug-help Show this help message and exit
--debug-version Show version number and exit
--debug-print-config Print the configuration of the client
--debug-print-config-paths
Print the paths of the configuration files
"""
let HELP_TEXT_SERVER: String = """
\(HELP_SERVER_VERSION)
Usage: ffmpeg-over-ip-server [options]
Options:
-h, --help, --debug-help
Show this help message and exit
-v, --version, --debug-version
Show version number and exit
--debug-print-config Print the configuration of the client
--debug-print-config-paths
Print the paths of the configuration files
"""

1
Sources/server/common Symbolic link
View File

@@ -0,0 +1 @@
../common

View File

@@ -1,4 +1,37 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book
print("Hello, world from server!")
func main() throws -> Void {
if CommandLine.arguments.contains("-h") || CommandLine.arguments.contains("--help") || CommandLine.arguments.contains("--debug-help") {
print(HELP_TEXT_SERVER)
return
}
if CommandLine.arguments.contains("-v") || CommandLine.arguments.contains("--version") || CommandLine.arguments.contains("--debug-version") {
print(HELP_SERVER_VERSION)
return
}
let configPaths: [String] = getConfigPaths(runtime: .Server)
if CommandLine.arguments.contains("--debug-print-config-paths") {
print("Config lookup paths: ")
for configPath in configPaths {
print(" \(configPath)")
}
exit(1)
}
let config = getConfig(filePaths: configPaths)
if config == nil {
print("No config file found. Try running with --debug-print-config-paths to print search paths")
exit(1)
}
}
// call main and try/catch it and log the error
do {
try main()
} catch {
print("Error: \(error)")
exit(1)
}