Skip to content

Commit d9a664d

Browse files
committed
add: implement edit
1 parent 2c26aec commit d9a664d

File tree

6 files changed

+112
-41
lines changed

6 files changed

+112
-41
lines changed

src/api/workspace.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ use std::path::Path;
1010
use crate::{
1111
age_utils::{self, Variable},
1212
api::WorkspaceApi,
13+
cli::{WorkParams, WorkspacePersistence},
1314
constants,
1415
labels::{self, Labels, ROLE},
1516
model::{
16-
config::{FileFormat, FinalCfg, RoozCfg},
17+
config::{ConfigSource, FileFormat, FinalCfg, RoozCfg},
1718
types::{AnyError, ContainerResult, RunSpec, WorkSpec, WorkspaceResult},
1819
volume::{RoozVolume, CACHE_ROLE, WORK_ROLE},
1920
},
@@ -270,7 +271,7 @@ impl<'a> WorkspaceApi<'a> {
270271
let labels = Labels::new(Some(workspace_key), Some(WORK_ROLE));
271272
for c in self.api.container.get_all(&labels).await? {
272273
if let Some(labels) = c.labels {
273-
let config_source = &labels[labels::CONFIG_SOURCE];
274+
let config_source = &labels[labels::CONFIG_ORIGIN];
274275
let format = FileFormat::from_path(config_source);
275276
let config =
276277
RoozCfg::deserialize_config(&labels[labels::CONFIG_BODY], format)?.unwrap();
@@ -303,9 +304,22 @@ impl<'a> WorkspaceApi<'a> {
303304
..edited_config
304305
};
305306

306-
println!("{}", encrypted_config.to_string(format)?)
307-
// save to label
308-
// apply
307+
self.new(
308+
&WorkParams {
309+
..Default::default()
310+
},
311+
Some(ConfigSource::Body {
312+
value: encrypted_config,
313+
origin: config_source.to_string(),
314+
format,
315+
}),
316+
Some(WorkspacePersistence {
317+
name: labels[labels::WORKSPACE_KEY].to_string(),
318+
replace: false,
319+
apply: true,
320+
}),
321+
)
322+
.await?;
309323
}
310324
}
311325
Ok(())

src/cli.rs

+18
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ pub struct WorkParams {
105105
pub start: Option<bool>,
106106
}
107107

108+
impl Default for WorkParams {
109+
fn default() -> Self {
110+
Self {
111+
git_ssh_url: Default::default(),
112+
env_image: Default::default(),
113+
image: Default::default(),
114+
pull_image: Default::default(),
115+
env_user: Default::default(),
116+
env_shell: Default::default(),
117+
user: Default::default(),
118+
env_caches: Default::default(),
119+
caches: Default::default(),
120+
privileged: Default::default(),
121+
start: Default::default(),
122+
}
123+
}
124+
}
125+
108126
#[derive(Parser, Debug)]
109127
#[command(about = "Creates a new workspace (container + volumes)")]
110128
pub struct NewParams {

src/cmd/new.rs

+44-32
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
git::{CloneEnv, RootRepoCloneResult},
88
labels::{self, Labels},
99
model::{
10-
config::{ConfigPath, FileFormat, FinalCfg, RoozCfg},
10+
config::{ConfigPath, ConfigSource, FileFormat, FinalCfg, RoozCfg},
1111
types::{AnyError, EnterSpec, WorkSpec},
1212
},
1313
};
@@ -90,7 +90,7 @@ impl<'a> WorkspaceApi<'a> {
9090
pub async fn new(
9191
&self,
9292
cli_params: &WorkParams,
93-
cli_config_path: Option<String>,
93+
cli_config_path: Option<ConfigSource>,
9494
persistence: Option<WorkspacePersistence>,
9595
) -> Result<EnterSpec, AnyError> {
9696
let ephemeral = persistence.is_none();
@@ -129,45 +129,57 @@ impl<'a> WorkspaceApi<'a> {
129129
..Default::default()
130130
};
131131

132-
let cli_cfg = if let Some(path) = &cli_config_path {
133-
let parsed_path = ConfigPath::from_str(&path)?;
134-
log::debug!("Loading cli config from: {}", path);
135-
match parsed_path {
136-
ConfigPath::File { path } => {
137-
let body = fs::read_to_string(&path)?;
132+
let cli_cfg = if let Some(source) = &cli_config_path {
133+
match source {
134+
ConfigSource::Body {
135+
value,
136+
origin,
137+
format,
138+
} => {
138139
labels = Labels {
139-
config_source: Labels::config_source(&path),
140-
config_body: Labels::config_body(&body),
140+
config_source: Labels::config_origin(&origin),
141+
config_body: Labels::config_body(&value.to_string(format.clone())?),
141142
..labels
142143
};
143-
RoozCfg::deserialize_config(&body, FileFormat::from_path(&path))?
144+
Some(value.clone())
144145
}
145-
ConfigPath::Git { url, file_path } => {
146-
let body = self
147-
.git
148-
.clone_config_repo(clone_env.clone(), &url, &file_path)
149-
.await?;
150-
151-
labels = Labels {
152-
config_source: Labels::config_source(&path),
153-
..labels
154-
};
146+
ConfigSource::Path { value: path } => match path {
147+
ConfigPath::File { path } => {
148+
let body = fs::read_to_string(&path)?;
149+
labels = Labels {
150+
config_source: Labels::config_origin(&path),
151+
config_body: Labels::config_body(&body),
152+
..labels
153+
};
154+
RoozCfg::deserialize_config(&body, FileFormat::from_path(&path))?
155+
}
156+
ConfigPath::Git { url, file_path } => {
157+
let body = self
158+
.git
159+
.clone_config_repo(clone_env.clone(), &url, &file_path)
160+
.await?;
155161

156-
if let Some(b) = &body {
157162
labels = Labels {
158-
config_body: Labels::config_body(&b),
163+
config_source: Labels::config_origin(&path.to_string()),
159164
..labels
160-
}
161-
};
165+
};
166+
167+
if let Some(b) = &body {
168+
labels = Labels {
169+
config_body: Labels::config_body(&b),
170+
..labels
171+
}
172+
};
162173

163-
match body {
164-
Some(body) => {
165-
let fmt = FileFormat::from_path(&file_path);
166-
RoozCfg::deserialize_config(&body, fmt)?
174+
match body {
175+
Some(body) => {
176+
let fmt = FileFormat::from_path(&file_path);
177+
RoozCfg::deserialize_config(&body, fmt)?
178+
}
179+
None => None,
167180
}
168-
None => None,
169181
}
170-
}
182+
},
171183
}
172184
} else {
173185
None
@@ -213,7 +225,7 @@ impl<'a> WorkspaceApi<'a> {
213225
log::debug!("Config file applied.");
214226
let source = format!("{}//.rooz.{}", url, format.to_string());
215227
labels = Labels {
216-
config_source: Labels::config_source(&source),
228+
config_source: Labels::config_origin(&source),
217229
config_body: Labels::config_body(&body),
218230
..labels
219231
};

src/labels.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub const WORKSPACE_KEY: &'static str = "dev.rooz.workspace";
66
pub const CONTAINER: &'static str = "dev.rooz.workspace.container";
77
pub const ROLE: &'static str = "dev.rooz.role";
88
pub const RUNTIME_CONFIG: &'static str = "dev.rooz.config.runtime";
9-
pub const CONFIG_SOURCE: &'static str = "dev.rooz.config.source";
9+
pub const CONFIG_ORIGIN: &'static str = "dev.rooz.config.origin";
1010
pub const CONFIG_BODY: &'static str = "dev.rooz.config.body";
1111
const ROOZ: &'static str = "dev.rooz";
1212
const LABEL_KEY: &'static str = "label";
@@ -84,8 +84,8 @@ impl Labels {
8484
Some(KeyValue::new(ROLE, role))
8585
}
8686

87-
pub fn config_source(path: &str) -> Option<KeyValue> {
88-
Some(KeyValue::new(CONFIG_SOURCE, path))
87+
pub fn config_origin(path: &str) -> Option<KeyValue> {
88+
Some(KeyValue::new(CONFIG_ORIGIN, path))
8989
}
9090

9191
pub fn config_body(body: &str) -> Option<KeyValue> {

src/main.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use bollard::Docker;
2929
use clap::{CommandFactory, Parser};
3030
use clap_complete::generate;
3131
use cli::EnterParams;
32+
use model::config::{ConfigPath, ConfigSource};
3233

3334
#[tokio::main]
3435
async fn main() -> Result<(), AnyError> {
@@ -104,8 +105,15 @@ async fn main() -> Result<(), AnyError> {
104105
}),
105106
..
106107
} => {
108+
let config_source = match config_path {
109+
Some(path) => Some(ConfigSource::Path {
110+
value: ConfigPath::from_str(&path)?,
111+
}),
112+
None => None,
113+
};
114+
107115
workspace
108-
.new(&work, config_path, Some(persistence.clone()))
116+
.new(&work, config_source, Some(persistence.clone()))
109117
.await?;
110118
println!(
111119
"\nThe workspace is ready. Run 'rooz enter {}' to enter.",

src/model/config.rs

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ use linked_hash_map::LinkedHashMap;
66
use serde::{Deserialize, Serialize};
77
use std::{collections::HashMap, ffi::OsStr, fs, path::Path};
88

9+
#[derive(Debug, Clone)]
10+
pub enum ConfigSource {
11+
Body {
12+
value: RoozCfg,
13+
origin: String,
14+
format: FileFormat,
15+
},
16+
Path {
17+
value: ConfigPath,
18+
},
19+
}
20+
921
#[derive(Debug, Clone)]
1022
pub enum ConfigPath {
1123
File { path: String },
@@ -29,6 +41,13 @@ impl<'a> ConfigPath {
2941
})
3042
}
3143
}
44+
45+
pub fn to_string(&self) -> String {
46+
match self {
47+
ConfigPath::File { path } => path.to_string(),
48+
ConfigPath::Git { url, file_path } => format!("{}//{}", url, file_path),
49+
}
50+
}
3251
}
3352

3453
#[derive(Debug, Clone, Copy)]

0 commit comments

Comments
 (0)