refactor CLI

This commit is contained in:
2026-07-13 11:43:37 +02:00
parent 909d890837
commit 1a2eddcd5d
7 changed files with 19 additions and 19 deletions
@@ -1,7 +1,7 @@
import ArgumentParser import ArgumentParser
extension phbar { extension PHBar {
struct refresh: ParsableCommand { struct Refresh: ParsableCommand {
static let configuration = CommandConfiguration( static let configuration = CommandConfiguration(
commandName: "refresh", commandName: "refresh",
abstract: "Refresh the running status bar." abstract: "Refresh the running status bar."
@@ -2,8 +2,8 @@ import AppKit
import ArgumentParser import ArgumentParser
import Foundation import Foundation
extension phbar { extension PHBar {
struct start: ParsableCommand { struct Start: ParsableCommand {
static let configuration = CommandConfiguration( static let configuration = CommandConfiguration(
commandName: "start", commandName: "start",
abstract: "Start the status bar.", abstract: "Start the status bar.",
@@ -19,7 +19,7 @@ extension PHBlock {
let setFile = base.appending(path: "blocks").appending(path: "\(blocks).toml") let setFile = base.appending(path: "blocks").appending(path: "\(blocks).toml")
guard FileManager.default.fileExists(atPath: setFile.relativePath) else { guard FileManager.default.fileExists(atPath: setFile.relativePath) else {
throw phbar.Error("Failed to load blocks '\(blocks)'") throw PHBar.Error("Failed to load blocks '\(blocks)'")
} }
return try load(from: setFile) return try load(from: setFile)
} }
@@ -30,11 +30,11 @@ extension PHBlock {
do { do {
let data = try Data(contentsOf: url) let data = try Data(contentsOf: url)
guard let contents = String(data: data, encoding: .utf8), !contents.isEmpty else { guard let contents = String(data: data, encoding: .utf8), !contents.isEmpty else {
throw phbar.Error("the file is empty") throw PHBar.Error("the file is empty")
} }
return try load(from: contents) return try load(from: contents)
} catch { } catch {
throw phbar.Error("Failed to load blocks file", underlyingError: error) throw PHBar.Error("Failed to load blocks file", underlyingError: error)
} }
} }
@@ -46,7 +46,7 @@ extension PHBlock {
let wrapper = try decoder.decode(PHBlock.Wrapper.self, from: contents) let wrapper = try decoder.decode(PHBlock.Wrapper.self, from: contents)
return wrapper.blocks return wrapper.blocks
} catch { } catch {
throw phbar.Error("Failed to parse blocks file", underlyingError: error) throw PHBar.Error("Failed to parse blocks file", underlyingError: error)
} }
} }
} }
@@ -24,7 +24,7 @@ extension PHConfig {
withExtension: "toml" withExtension: "toml"
)?.resolvingSymlinksInPath() )?.resolvingSymlinksInPath()
else { else {
throw phbar.Error("Failed to load config file") throw PHBar.Error("Failed to load config file")
} }
return try load(from: defaultConfig) return try load(from: defaultConfig)
} }
@@ -35,7 +35,7 @@ extension PHConfig {
guard let data = try? Data(contentsOf: url), guard let data = try? Data(contentsOf: url),
let contents = String(data: data, encoding: .utf8) let contents = String(data: data, encoding: .utf8)
else { else {
throw phbar.Error("Failed to load config file") throw PHBar.Error("Failed to load config file")
} }
return try load(from: contents) return try load(from: contents)
@@ -53,7 +53,7 @@ extension PHConfig {
// resolve the same set. // resolve the same set.
return config.resolvingEnv() return config.resolvingEnv()
} catch { } catch {
throw phbar.Error("Failed to parse config file", underlyingError: error) throw PHBar.Error("Failed to parse config file", underlyingError: error)
} }
} }
} }
@@ -28,7 +28,7 @@ extension PHTheme {
withExtension: "toml" withExtension: "toml"
)?.resolvingSymlinksInPath() )?.resolvingSymlinksInPath()
else { else {
throw phbar.Error("Failed to load theme file") throw PHBar.Error("Failed to load theme file")
} }
return try load(from: defaultConfig, environment: .process) return try load(from: defaultConfig, environment: .process)
} }
@@ -39,11 +39,11 @@ extension PHTheme {
do { do {
let data = try Data(contentsOf: url) let data = try Data(contentsOf: url)
guard let contents = String(data: data, encoding: .utf8), !contents.isEmpty else { guard let contents = String(data: data, encoding: .utf8), !contents.isEmpty else {
throw phbar.Error("the file is empty") throw PHBar.Error("the file is empty")
} }
return try load(from: contents, environment: environment) return try load(from: contents, environment: environment)
} catch { } catch {
throw phbar.Error("Failed to load theme file", underlyingError: error) throw PHBar.Error("Failed to load theme file", underlyingError: error)
} }
} }
@@ -55,7 +55,7 @@ extension PHTheme {
decoder.userInfo[PHEnvironment.userInfoKey] = environment decoder.userInfo[PHEnvironment.userInfoKey] = environment
return try decoder.decode(PHTheme.self, from: contents) return try decoder.decode(PHTheme.self, from: contents)
} catch { } catch {
throw phbar.Error("Failed to parse theme file", underlyingError: error) throw PHBar.Error("Failed to parse theme file", underlyingError: error)
} }
} }
} }
@@ -8,7 +8,7 @@ import ArgumentParser
import Foundation import Foundation
@main @main
struct phbar: ParsableCommand { struct PHBar: ParsableCommand {
static let configuration = CommandConfiguration( static let configuration = CommandConfiguration(
commandName: "phbar", commandName: "phbar",
abstract: "Modular status bar for macOS.", abstract: "Modular status bar for macOS.",
@@ -39,11 +39,11 @@ struct phbar: ParsableCommand {
the same command to start the bar automatically at login. the same command to start the bar automatically at login.
""", """,
version: "1.0.0", version: "1.0.0",
subcommands: [start.self, refresh.self] subcommands: [Start.self, Refresh.self]
) )
} }
extension phbar { extension PHBar {
struct Error: LocalizedError { struct Error: LocalizedError {
let message: String let message: String
let underlyingError: Swift.Error? let underlyingError: Swift.Error?
+1 -1
View File
@@ -7,7 +7,7 @@ import Testing
// MARK: - Refresh command // MARK: - Refresh command
@Test func refreshCommandConfigurationIsCorrect() async throws { @Test func refreshCommandConfigurationIsCorrect() async throws {
let config = phbar.refresh.configuration let config = PHBar.Refresh.configuration
#expect(config.commandName == "refresh") #expect(config.commandName == "refresh")
#expect(config.abstract == "Refresh the running status bar.") #expect(config.abstract == "Refresh the running status bar.")
} }