refactor: Deduplicate paths
This commit is contained in:
+15
-9
@@ -15,6 +15,7 @@ use tokio::process::Command;
|
|||||||
use crate::environment::PathEnvironment;
|
use crate::environment::PathEnvironment;
|
||||||
use crate::node::{Node, OptionalNodeDeserialize};
|
use crate::node::{Node, OptionalNodeDeserialize};
|
||||||
use crate::patch::Patches;
|
use crate::patch::Patches;
|
||||||
|
use crate::talosconfig::TalosConfig;
|
||||||
use crate::{get_configs_path, get_talos_path, run_command};
|
use crate::{get_configs_path, get_talos_path, run_command};
|
||||||
|
|
||||||
#[optional_struct]
|
#[optional_struct]
|
||||||
@@ -192,14 +193,18 @@ impl Cluster {
|
|||||||
run_command(command).await
|
run_command(command).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn talosconfig_add_endpoint(&self) {
|
pub fn talosconfig_path(&self) -> PathBuf {
|
||||||
let path = get_configs_path().join(&self.name).join("talosconfig");
|
get_configs_path().join(&self.name).join("talosconfig")
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn talosconfig_add_endpoint(&self) {
|
||||||
let mut command = Command::new("talosctl");
|
let mut command = Command::new("talosctl");
|
||||||
command.args([
|
command.args([
|
||||||
"config",
|
"config",
|
||||||
"--talosconfig",
|
"--talosconfig",
|
||||||
path.to_str().expect("Path should be valid utf-8"),
|
self.talosconfig_path()
|
||||||
|
.to_str()
|
||||||
|
.expect("Path should be valid utf-8"),
|
||||||
"endpoint",
|
"endpoint",
|
||||||
&self.control_plane_ip.to_string(),
|
&self.control_plane_ip.to_string(),
|
||||||
]);
|
]);
|
||||||
@@ -210,7 +215,7 @@ impl Cluster {
|
|||||||
pub async fn talosconfig_write(&self) {
|
pub async fn talosconfig_write(&self) {
|
||||||
let config = self.talosconfig().await;
|
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"))
|
tokio::fs::create_dir_all(path.parent().expect("talosconfig should have parent"))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -221,16 +226,17 @@ impl Cluster {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn talosconfig_merge(&self) {
|
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");
|
let mut command = Command::new("talosctl");
|
||||||
command.args([
|
command.args([
|
||||||
"config",
|
"config",
|
||||||
"--talosconfig",
|
"--talosconfig",
|
||||||
path.to_str().expect("Path should be valid utf-8"),
|
TalosConfig::merged_path()
|
||||||
|
.to_str()
|
||||||
|
.expect("Path should be valid utf-8"),
|
||||||
"merge",
|
"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;
|
run_command(command).await;
|
||||||
|
|||||||
+9
-3
@@ -1,4 +1,5 @@
|
|||||||
use std::net::Ipv4Addr;
|
use std::net::Ipv4Addr;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use minijinja::Environment;
|
use minijinja::Environment;
|
||||||
use optional_struct::{Applicable, optional_struct};
|
use optional_struct::{Applicable, optional_struct};
|
||||||
@@ -218,12 +219,17 @@ impl Node {
|
|||||||
run_command(command).await
|
run_command(command).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn config_write(&self, cluster: &Cluster) {
|
pub fn config_path(&self, cluster: &Cluster) -> PathBuf {
|
||||||
let config = self.config(cluster).await;
|
|
||||||
|
|
||||||
let mut path = get_configs_path().join(&cluster.name).join(&self.hostname);
|
let mut path = get_configs_path().join(&cluster.name).join(&self.hostname);
|
||||||
path.add_extension("yaml");
|
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"))
|
tokio::fs::create_dir_all(path.parent().expect("config should have parent"))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|||||||
+14
-8
@@ -1,17 +1,23 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
|
|
||||||
use crate::{get_configs_path, run_command};
|
use crate::{get_configs_path, run_command};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct TalosConfig {
|
pub struct TalosConfig {
|
||||||
pub context: String,
|
context: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TalosConfig {
|
||||||
|
pub fn merged_path() -> PathBuf {
|
||||||
|
get_configs_path().join("talosconfig")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_current_context() -> Option<String> {
|
pub async fn get_current_context() -> Option<String> {
|
||||||
let path = get_configs_path().join("talosconfig");
|
tokio::fs::read(&TalosConfig::merged_path())
|
||||||
|
|
||||||
tokio::fs::read(&path)
|
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|config| serde_yaml::from_slice(&config).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) {
|
pub async fn set_context(context: String) {
|
||||||
let path = get_configs_path().join("talosconfig");
|
|
||||||
|
|
||||||
let mut command = Command::new("talosctl");
|
let mut command = Command::new("talosctl");
|
||||||
command.args([
|
command.args([
|
||||||
"config",
|
"config",
|
||||||
"--talosconfig",
|
"--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",
|
||||||
&context,
|
&context,
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user