refactor: Deduplicate paths

This commit is contained in:
2026-10-04 05:39:22 +02:00
parent 1dae62846b
commit 101d690285
3 changed files with 38 additions and 20 deletions
+15 -9
View File
@@ -15,6 +15,7 @@ use tokio::process::Command;
use crate::environment::PathEnvironment;
use crate::node::{Node, OptionalNodeDeserialize};
use crate::patch::Patches;
use crate::talosconfig::TalosConfig;
use crate::{get_configs_path, get_talos_path, run_command};
#[optional_struct]
@@ -192,14 +193,18 @@ impl Cluster {
run_command(command).await
}
async fn talosconfig_add_endpoint(&self) {
let path = get_configs_path().join(&self.name).join("talosconfig");
pub fn talosconfig_path(&self) -> PathBuf {
get_configs_path().join(&self.name).join("talosconfig")
}
async fn talosconfig_add_endpoint(&self) {
let mut command = Command::new("talosctl");
command.args([
"config",
"--talosconfig",
path.to_str().expect("Path should be valid utf-8"),
self.talosconfig_path()
.to_str()
.expect("Path should be valid utf-8"),
"endpoint",
&self.control_plane_ip.to_string(),
]);
@@ -210,7 +215,7 @@ impl Cluster {
pub async fn talosconfig_write(&self) {
let config = self.talosconfig().await;
let path = get_configs_path().join(&self.name).join("talosconfig");
let path = self.talosconfig_path();
tokio::fs::create_dir_all(path.parent().expect("talosconfig should have parent"))
.await
.unwrap();
@@ -221,16 +226,17 @@ impl Cluster {
}
pub async fn talosconfig_merge(&self) {
let cluster_path = get_configs_path().join(&self.name).join("talosconfig");
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"),
"merge",
cluster_path.to_str().expect("Path should be valid utf-8"),
self.talosconfig_path()
.to_str()
.expect("Path should be valid utf-8"),
]);
run_command(command).await;
+9 -3
View File
@@ -1,4 +1,5 @@
use std::net::Ipv4Addr;
use std::path::PathBuf;
use minijinja::Environment;
use optional_struct::{Applicable, optional_struct};
@@ -218,12 +219,17 @@ impl Node {
run_command(command).await
}
pub async fn config_write(&self, cluster: &Cluster) {
let config = self.config(cluster).await;
pub fn config_path(&self, cluster: &Cluster) -> PathBuf {
let mut path = get_configs_path().join(&cluster.name).join(&self.hostname);
path.add_extension("yaml");
path
}
pub async fn config_write(&self, cluster: &Cluster) {
let config = self.config(cluster).await;
let path = self.config_path(cluster);
tokio::fs::create_dir_all(path.parent().expect("config should have parent"))
.await
.unwrap();
+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,
]);