Skip to content

Commit

Permalink
✨ Basic config
Browse files Browse the repository at this point in the history
  • Loading branch information
oschrenk committed Jan 23, 2025
1 parent d896016 commit 26523ea
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"iconize": [
{
"field": "title.label",
"regex": "Movement Yoga"
"regex": "Movement Yoga",
"icon": "🪷"
}
],
"watch": {
"watcher": {
"hook": {
"path": "/opt/homebrew/bin/sketchybar"
"path": "/opt/homebrew/bin/sketchybar",
"args": ["--trigger", "calendar_changed"]
}
}
Expand Down
19 changes: 19 additions & 0 deletions Sources/Cli/ShowConfig.swift
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))")
}
}
19 changes: 19 additions & 0 deletions Sources/Config.swift
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]
}
36 changes: 36 additions & 0 deletions Sources/Loader.swift
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
}
}
}
1 change: 1 addition & 0 deletions Sources/Plan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Plan: ParsableCommand {
Add.self,
Calendars.self,
Next.self,
ShowConfig.self,
Today.self,
Usage.self,
Watch.self,
Expand Down

0 comments on commit 26523ea

Please sign in to comment.