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

Commit

Permalink
Remove force cast in KubeSwitch code
Browse files Browse the repository at this point in the history
It avoids the app to crash if the kubeconfig file is unexpected.
  • Loading branch information
guillaumerose authored and anjannath committed Jun 21, 2021
1 parent a6357de commit 3e37097
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
20 changes: 14 additions & 6 deletions CodeReady Containers/KubeSwitch/KubeConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,35 @@ class KubeConfig {
}

func currentContext() -> String {
return (self.yamlContent["current-context"] != nil)
? self.yamlContent["current-context"] as! String : ""
if let ret = self.yamlContent["current-context"] as? String {
return ret
} else {
return ""
}
}

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

func contexts() -> [AnyObject] {
return (self.yamlContent["contexts"] != nil)
? self.yamlContent["contexts"] as! [AnyObject] : []
if let ret = self.yamlContent["contexts"] as? [AnyObject] {
return ret
} else {
return []
}
}

func contextNames() -> [String] {
return self.contexts()
.map {
$0 as! [String: Any]
$0 as? [String: Any]
}
.compactMap { $0 }
.map {
$0["name"] as! String
$0["name"] as? String
}
.compactMap { $0 }
}

func changeContext(newContext: String) {
Expand Down
7 changes: 5 additions & 2 deletions CodeReady Containers/KubeSwitch/YamlReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ class YamlReader {
func loadKubeConfig(yaml: String) -> KubeConfig {
do {
let readYaml = try Yams.load(yaml: yaml)
let yamlContent = readYaml != nil ? readYaml as! [String: Any] : [:]
return KubeConfig(yamlContent: yamlContent)
if let ret = readYaml as? [String: Any] {
return KubeConfig(yamlContent: ret)
} else {
return KubeConfig(yamlContent: [:])
}
} catch {
os_log("Could not load yaml string as dictionary", type: .error)
let errorDetails = "\(error)"
Expand Down

0 comments on commit 3e37097

Please sign in to comment.