refactor: Deduplicate paths

This commit is contained in:
2026-10-04 05:53:18 +02:00
parent 88405a1e63
commit cae78a31a8
3 changed files with 38 additions and 20 deletions
+14 -8
View File
@@ -1,17 +1,23 @@
use std::path::PathBuf;
use serde::Deserialize;
use tokio::process::Command;
use crate::{get_configs_path, run_command};
#[derive(Debug, Deserialize)]
struct TalosConfig {
pub context: String,
pub struct TalosConfig {
context: String,
}
impl TalosConfig {
pub fn merged_path() -> PathBuf {
get_configs_path().join("talosconfig")
}
}
pub async fn get_current_context() -> Option<String> {
let path = get_configs_path().join("talosconfig");
tokio::fs::read(&path)
tokio::fs::read(&TalosConfig::merged_path())
.await
.ok()
.and_then(|config| serde_yaml::from_slice(&config).ok())
@@ -19,13 +25,13 @@ pub async fn get_current_context() -> Option<String> {
}
pub async fn set_context(context: String) {
let path = get_configs_path().join("talosconfig");
let mut command = Command::new("talosctl");
command.args([
"config",
"--talosconfig",
path.to_str().expect("Path should be valid utf-8"),
TalosConfig::merged_path()
.to_str()
.expect("Path should be valid utf-8"),
"context",
&context,
]);