Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.

Commit 3e37097

Browse files
guillaumeroseanjannath
authored andcommitted
Remove force cast in KubeSwitch code
It avoids the app to crash if the kubeconfig file is unexpected.
1 parent a6357de commit 3e37097

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

CodeReady Containers/KubeSwitch/KubeConfig.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,35 @@ class KubeConfig {
1919
}
2020

2121
func currentContext() -> String {
22-
return (self.yamlContent["current-context"] != nil)
23-
? self.yamlContent["current-context"] as! String : ""
22+
if let ret = self.yamlContent["current-context"] as? String {
23+
return ret
24+
} else {
25+
return ""
26+
}
2427
}
2528

2629
func isCurrentContext(otherContextName: String) -> Bool {
2730
return otherContextName == self.currentContext()
2831
}
2932

3033
func contexts() -> [AnyObject] {
31-
return (self.yamlContent["contexts"] != nil)
32-
? self.yamlContent["contexts"] as! [AnyObject] : []
34+
if let ret = self.yamlContent["contexts"] as? [AnyObject] {
35+
return ret
36+
} else {
37+
return []
38+
}
3339
}
3440

3541
func contextNames() -> [String] {
3642
return self.contexts()
3743
.map {
38-
$0 as! [String: Any]
44+
$0 as? [String: Any]
3945
}
46+
.compactMap { $0 }
4047
.map {
41-
$0["name"] as! String
48+
$0["name"] as? String
4249
}
50+
.compactMap { $0 }
4351
}
4452

4553
func changeContext(newContext: String) {

CodeReady Containers/KubeSwitch/YamlReader.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ class YamlReader {
1818
func loadKubeConfig(yaml: String) -> KubeConfig {
1919
do {
2020
let readYaml = try Yams.load(yaml: yaml)
21-
let yamlContent = readYaml != nil ? readYaml as! [String: Any] : [:]
22-
return KubeConfig(yamlContent: yamlContent)
21+
if let ret = readYaml as? [String: Any] {
22+
return KubeConfig(yamlContent: ret)
23+
} else {
24+
return KubeConfig(yamlContent: [:])
25+
}
2326
} catch {
2427
os_log("Could not load yaml string as dictionary", type: .error)
2528
let errorDetails = "\(error)"

0 commit comments

Comments
 (0)