11import Foundation
22
3- /// 表示单个 Kubernetes 配置文件的状态
43enum KubeConfigFileStatus : Equatable {
5- /// 配置文件有效
64 case valid
7- /// 配置文件格式无效
85 case invalid( String )
9- /// 配置文件未加载或状态未知
106 case unknown
117}
128
13- /// 表示单个配置文件的类型
149enum 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 配置文件
2415struct 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
0 commit comments