Skip to content

Commit d337888

Browse files
authored
Merge pull request #13 from samzong:chore/clean-unused
refactor(ConfigForgeApp): streamline command group and improve menu item labels
2 parents e26d1d0 + c321309 commit d337888

35 files changed

Lines changed: 255 additions & 1458 deletions

ConfigForge/AppDelegate.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1313

1414
@MainActor
1515
func applicationDidFinishLaunching(_ notification: Notification) {
16-
// 读取保存的语言设置
1716
let savedLanguage = UserDefaults.standard.string(forKey: "appLanguage")
1817
let currentLanguage = Locale.preferredLanguages.first?.prefix(2) ?? "en"
19-
20-
// 检查语言设置是否已保存或与当前系统语言不同
18+
2119
if savedLanguage == nil || savedLanguage != String(currentLanguage) {
22-
// 如果未设置或不匹配,使用当前系统语言作为默认值
2320
UserDefaults.standard.set(String(currentLanguage), forKey: "appLanguage")
2421
}
25-
26-
// 设置应用的语言
2722
if let language = UserDefaults.standard.string(forKey: "appLanguage") {
2823
UserDefaults.standard.set([language], forKey: "AppleLanguages")
2924
UserDefaults.standard.synchronize()

ConfigForge/ConfigForgeApp.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ import SwiftUI
99

1010
@main
1111
struct ConfigForgeApp: App {
12-
// 添加 AppDelegate
1312
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
1413

1514
var body: some Scene {
1615
WindowGroup(content: {
1716
ContentView()
1817
.frame(minWidth: 800, minHeight: 600)
1918
})
20-
.windowStyle(.hiddenTitleBar) // 可选:使用更现代的窗口样式
19+
.windowStyle(.hiddenTitleBar)
2120
.commands {
22-
// 添加菜单命令
2321
CommandGroup(replacing: .newItem) {
2422
Button(L10n.Sidebar.Add.host) {
2523
NotificationCenter.default.post(name: NSNotification.Name("NewEntry"), object: nil)
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1-
// L10n 扩展 - 为项目中使用的但 SwiftGen 未直接生成的键提供标准访问
21

32
import Foundation
43

5-
// MARK: - 扩展 L10n 以包含缺失的键
6-
74
extension L10n {
8-
/// 错误相关的本地化字符串
95
public enum CustomError {
10-
/// 配置读取错误
116
public static let configReadError = NSLocalizedString("error.configReadError", comment: "Configuration read error")
12-
13-
/// 配置写入错误
147
public static let configWriteError = NSLocalizedString("error.configWriteError", comment: "Configuration write error")
15-
16-
/// 解析错误
178
public static let parsingError = NSLocalizedString("error.parsingError", comment: "Parsing error")
189
}
19-
20-
/// Kubernetes 相关的本地化字符串
2110
public enum Kube {
22-
/// Kubernetes 配置加载成功
2311
public static let configLoaded = NSLocalizedString("success.kubeConfigLoaded", comment: "Kubernetes configuration loaded successfully")
2412
}
2513
}

ConfigForge/ImportGenerated.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// SwiftGen L10n 导入
2-
// 此文件确保 SwiftGen 生成的代码可在整个项目中使用
1+
// Import of SwiftGen L10n
32

43
@_exported import struct SwiftUI.LocalizedStringKey

ConfigForge/Models/ConfigDocument.swift

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import SwiftUI
22
import UniformTypeIdentifiers
3-
4-
// Enum to represent the type of configuration content
53
enum ConfigContentType: Sendable {
64
case ssh
75
case kubernetes
8-
case unknown // Fallback
6+
case unknown
97

108
var utType: UTType {
119
switch self {
12-
case .ssh: return .text // Or a custom UTType if defined
13-
case .kubernetes: return .yaml // Kubeconfig is typically YAML
10+
case .ssh: return .text
11+
case .kubernetes: return .yaml
1412
case .unknown: return .data
1513
}
1614
}
1715

1816
var defaultFilenameExtension: String {
1917
switch self {
20-
case .ssh: return "txt" // SSH config usually has no extension or is txt
18+
case .ssh: return "txt"
2119
case .kubernetes: return "yaml"
2220
case .unknown: return "dat"
2321
}
@@ -32,47 +30,34 @@ enum ConfigContentType: Sendable {
3230
}
3331

3432
struct ConfigDocument: FileDocument, Sendable {
35-
static let readableContentTypes: [UTType] = [.text, .yaml] // Accept both text and YAML
33+
static let readableContentTypes: [UTType] = [.text, .yaml]
3634

3735
var content: String
3836
var contentType: ConfigContentType
39-
40-
// Initialize from file content during import
4137
init(configuration: ReadConfiguration) throws {
4238
guard let data = configuration.file.regularFileContents,
4339
let stringContent = String(data: data, encoding: .utf8) else {
4440
throw CocoaError(.fileReadCorruptFile)
4541
}
4642
self.content = stringContent
47-
48-
// Attempt to determine type based on content or UTType
4943
if configuration.contentType == .yaml || stringContent.contains("apiVersion:") {
5044
self.contentType = .kubernetes
5145
} else if configuration.contentType == .text {
52-
// Could potentially be SSH config, but might need better heuristic
53-
// For now, assume text means SSH if not clearly Kube
54-
// A robust solution might require user confirmation or smarter parsing
5546
self.contentType = .ssh
5647
}
5748
else {
5849
self.contentType = .unknown
59-
// Optionally throw an error if type cannot be determined
60-
// throw CocoaError(.fileReadUnknown)
6150
print("Warning: Could not determine config type during read.")
6251
}
6352

6453
}
65-
66-
// Initialize explicitly for export
6754
init(content: String, type: ConfigContentType) {
6855
self.content = content
6956
self.contentType = type
7057
}
71-
72-
// Create file wrapper for saving/exporting
7358
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
7459
guard let data = content.data(using: .utf8) else {
75-
throw CocoaError(.fileWriteUnknown) // Or a more specific error
60+
throw CocoaError(.fileWriteUnknown)
7661
}
7762
return FileWrapper(regularFileWithContents: data)
7863
}

ConfigForge/Models/ErrorMessage.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import Foundation
99

10-
// 添加一个可识别的错误消息结构体
1110
struct ErrorMessage: Identifiable {
1211
let id = UUID()
1312
let message: String

ConfigForge/Models/KubeConfigFile.swift

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,30 @@
11
import Foundation
22

3-
/// 表示单个 Kubernetes 配置文件的状态
43
enum KubeConfigFileStatus: Equatable {
5-
/// 配置文件有效
64
case valid
7-
/// 配置文件格式无效
85
case invalid(String)
9-
/// 配置文件未加载或状态未知
106
case unknown
117
}
128

13-
/// 表示单个配置文件的类型
149
enum KubeConfigFileType: Equatable {
15-
/// 主配置文件 (~/.kube/config)
1610
case active
17-
/// 备份配置文件 (~/.kube/config.bak)
1811
case backup
19-
/// 存储在 ~/.kube/configs/ 目录中的配置文件
2012
case stored
2113
}
2214

23-
/// 表示文件系统中的单个 Kubernetes 配置文件
2415
struct KubeConfigFile: Identifiable, Equatable {
25-
/// 唯一标识符,基于文件路径
2616
var id: String { filePath.path }
27-
28-
/// 文件名
2917
let fileName: String
30-
31-
/// 显示名称 (不带扩展名)
3218
var displayName: String {
3319
fileName.components(separatedBy: ".").first ?? fileName
3420
}
35-
36-
/// 完整的文件路径
3721
let filePath: URL
38-
39-
/// The raw YAML content of the config file
4022
private(set) var yamlContent: String?
41-
42-
/// 文件类型 (活动、备份、存储的)
4323
let fileType: KubeConfigFileType
44-
45-
/// 文件状态 (有效、无效等)
4624
var status: KubeConfigFileStatus = .unknown
47-
48-
/// 文件创建日期
4925
let creationDate: Date?
50-
51-
/// 文件最后修改日期
5226
var modificationDate: Date?
53-
54-
/// 是否为当前活动配置文件
5527
var isActive: Bool = false
56-
57-
/// 创建配置文件对象
58-
/// - Parameters:
59-
/// - fileName: 文件名
60-
/// - filePath: 文件路径
61-
/// - fileType: 文件类型
62-
/// - yamlContent: Raw YAML content (if loaded)
63-
/// - creationDate: 创建日期
64-
/// - modificationDate: 修改日期
6528
init(fileName: String, filePath: URL, fileType: KubeConfigFileType, yamlContent: String? = nil,
6629
creationDate: Date? = nil, modificationDate: Date? = nil, isActive: Bool = false) {
6730
self.fileName = fileName
@@ -72,13 +35,7 @@ struct KubeConfigFile: Identifiable, Equatable {
7235
self.modificationDate = modificationDate
7336
self.isActive = isActive
7437
}
75-
76-
/// Creates an instance from a file URL, attempting to read its content and attributes.
77-
/// - Parameters:
78-
/// - url: The file URL.
79-
/// - fileType: The type of the file.
80-
/// - fileManager: FileManager instance.
81-
/// - Returns: A new KubeConfigFile instance, potentially with nil content if read fails.
38+
8239
static func from(url: URL, fileType: KubeConfigFileType, fileManager: FileManager = .default, isActive: Bool = false) -> KubeConfigFile? {
8340
var creationDate: Date? = nil
8441
var modificationDate: Date? = nil
@@ -96,7 +53,6 @@ struct KubeConfigFile: Identifiable, Equatable {
9653
yamlContent = try String(contentsOf: url, encoding: .utf8)
9754
} catch {
9855
print("Warning: Could not read file content for \(url.path): \(error.localizedDescription)")
99-
// Content remains nil, status remains .unknown
10056
}
10157

10258
return KubeConfigFile(
@@ -109,23 +65,17 @@ struct KubeConfigFile: Identifiable, Equatable {
10965
isActive: isActive
11066
)
11167
}
112-
113-
/// Updates the raw YAML content and resets the status to unknown.
114-
/// - Parameter newContent: The new YAML content.
68+
11569
mutating func updateYamlContent(_ newContent: String) {
11670
self.yamlContent = newContent
117-
self.status = .unknown // Status needs re-validation after content change
118-
self.modificationDate = Date() // Update modification date
71+
self.status = .unknown
72+
self.modificationDate = Date()
11973
}
120-
121-
/// 标记为无效,并提供原因
122-
/// - Parameter reason: 无效的原因
74+
12375
mutating func markAsInvalid(_ reason: String) {
12476
self.status = .invalid(reason)
12577
}
12678

127-
// MARK: - Equatable
128-
12979
static func == (lhs: KubeConfigFile, rhs: KubeConfigFile) -> Bool {
13080
return lhs.filePath == rhs.filePath &&
13181
lhs.fileType == rhs.fileType

ConfigForge/Models/SSHConfigEntry.swift

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ struct SSHConfigEntry: Identifiable, Hashable, Sendable {
1111
let id = UUID()
1212
var host: String
1313
var properties: [String: String]
14-
15-
// 计算属性用于直接访问常用配置
1614
var hostname: String {
1715
properties["HostName"] ?? ""
1816
}
@@ -28,32 +26,24 @@ struct SSHConfigEntry: Identifiable, Hashable, Sendable {
2826
var identityFile: String {
2927
properties["IdentityFile"] ?? ""
3028
}
31-
32-
// 验证端口号是否有效
3329
var isPortValid: Bool {
3430
guard let portStr = properties["Port"], !portStr.isEmpty else {
35-
return true // 如果没有设置端口,使用默认值22是有效的
31+
return true
3632
}
3733

3834
guard let port = Int(portStr) else {
39-
return false // 端口必须是数字
35+
return false
4036
}
4137

42-
return port >= 1 && port <= 65535 // 有效端口范围
38+
return port >= 1 && port <= 65535
4339
}
44-
45-
// 验证主机名是否有效
4640
var isHostNameValid: Bool {
4741
guard let hostName = properties["HostName"], !hostName.isEmpty else {
48-
return true // 空主机名在技术上是有效的,尽管不太有用
42+
return true
4943
}
50-
51-
// 简单的主机名验证 - 不能仅包含空格
5244
let trimmed = hostName.trimmingCharacters(in: .whitespacesAndNewlines)
5345
return !trimmed.isEmpty
5446
}
55-
56-
// 实现Hashable协议
5747
func hash(into hasher: inout Hasher) {
5848
hasher.combine(id)
5949
}

0 commit comments

Comments
 (0)