-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
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 ArgumentParser | ||
import EventKit | ||
import Foundation | ||
|
||
/// `plan config` | ||
/// | ||
/// Show config | ||
struct ShowConfig: ParsableCommand { | ||
static var configuration = CommandConfiguration( | ||
commandName: "config", | ||
abstract: "Show config", | ||
shouldDisplay: false | ||
) | ||
|
||
mutating func run() { | ||
let config = Loader.readConfig() | ||
StdOut.print("\(String(describing: config))") | ||
} | ||
} |
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 @@ | ||
struct Config: Codable { | ||
let iconize: [Rule]? | ||
let watcher: Watcher? | ||
} | ||
|
||
struct Rule: Codable { | ||
let field: String | ||
let regex: String | ||
let icon: String | ||
} | ||
|
||
struct Watcher: Codable { | ||
let hook: Hook? | ||
} | ||
|
||
struct Hook: Codable { | ||
let path: String | ||
let args: [String] | ||
} |
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,36 @@ | ||
import Foundation | ||
|
||
enum Loader { | ||
private static let appName = "plan" | ||
private static let defaultConfigHome = FileManager.default | ||
.homeDirectoryForCurrentUser | ||
.appendingPathComponent(".config") | ||
private static let xdgConfigHome = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] | ||
.map { URL(fileURLWithPath: $0) } | ||
private static let configHome = xdgConfigHome ?? defaultConfigHome | ||
|
||
private static let configDir = configHome.appendingPathComponent(appName) | ||
private static let configUrl = configDir.appendingPathComponent("config.json") | ||
|
||
private static func readLocalJSON<T: Codable>(from path: String, as _: T.Type) -> T? { | ||
let url = URL(fileURLWithPath: path) | ||
|
||
do { | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
return try decoder.decode(T.self, from: data) | ||
} catch { | ||
print("Error reading or decoding file at \(path): \(error)") | ||
return nil | ||
} | ||
} | ||
|
||
static func readConfig() -> Config? { | ||
if let config: Config = readLocalJSON(from: configUrl.path, as: Config.self) { | ||
return config | ||
} else { | ||
print("Failed to read JSON file.") | ||
return nil | ||
} | ||
} | ||
} |
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