Skip to content

Commit 1e37427

Browse files
committed
improvements
1 parent 9b9833e commit 1e37427

11 files changed

Lines changed: 164 additions & 166 deletions

File tree

src/cli/src/cli.rs

Lines changed: 72 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use url_builder::URLBuilder;
1212

1313
use crate::input::{
1414
clone_setup, config_all, config_editor, config_host, config_owner, create_workspace,
15-
fork_setup, overwrite, select_repo,
15+
fork_setup, overwrite, select, select_repo,
1616
};
1717
use devmode::fork::ForkAction;
1818
use devmode::host::Host;
19-
use devmode::project::{find_paths, OpenAction};
19+
use devmode::project::{matching_paths_for, OpenAction};
2020
use devmode::settings::Settings;
2121
use devmode::{clone::CloneAction, constants::patterns::GIT_URL};
2222

@@ -93,8 +93,6 @@ pub enum Commands {
9393
arg_required_else_help = true
9494
)]
9595
Config {
96-
#[clap(help = "Map your project paths.", short = 'm', long = "map")]
97-
map: bool,
9896
#[clap(help = "Show the current configuration.", short = 's', long = "show")]
9997
show: bool,
10098
#[clap(help = "Configure your settings.", short = 'a', long = "all")]
@@ -141,6 +139,8 @@ pub enum Commands {
141139
remove: Option<String>,
142140
#[clap(help = "List all workspaces", short = 'l', long = "list")]
143141
list: bool,
142+
#[clap(help = "Show information about a workspace", long = "info")]
143+
info: bool,
144144
},
145145
}
146146

@@ -153,20 +153,18 @@ impl Cli {
153153
Commands::Update { project } => Cli::update(project),
154154
Commands::Fork { args, upstream } => Cli::fork(args, upstream, rx),
155155
Commands::Config {
156-
map,
157156
show,
158157
all,
159158
editor,
160159
owner,
161160
host,
162161
} => Cli::config(Config {
163-
map: *map,
164162
show: *show,
165163
all: *all,
166164
editor: *editor,
167165
owner: *owner,
168166
host: *host,
169-
none: !map && !show && !all && !editor && !owner && !host,
167+
none: !show && !all && !editor && !owner && !host,
170168
}),
171169
Commands::Workspace {
172170
name,
@@ -176,6 +174,7 @@ impl Cli {
176174
include,
177175
remove,
178176
list,
177+
info,
179178
} => Cli::workspace(WorkspaceOptions {
180179
name: name.to_owned(),
181180
add: *add,
@@ -184,6 +183,7 @@ impl Cli {
184183
include: include.to_owned(),
185184
remove: remove.to_owned(),
186185
list: *list,
186+
info: *info,
187187
}),
188188
}
189189
}
@@ -253,11 +253,11 @@ impl Cli {
253253
}
254254

255255
fn open(project: &str) -> Result<(), Error> {
256-
let paths = find_paths(project)?;
256+
let paths = matching_paths_for(project)?;
257257
if paths.is_empty() {
258258
Err(Error::Devmode(DevmodeError::NoProjectFound))
259259
} else if paths.len() > 1 {
260-
let path = select_repo(project, None)?;
260+
let path = select_repo(paths)?;
261261
OpenAction::new(project).open(path)
262262
} else {
263263
OpenAction::new(project).open(
@@ -270,11 +270,11 @@ impl Cli {
270270
}
271271

272272
fn update(project: &str) -> Result<(), Error> {
273-
let paths = find_paths(project)?;
273+
let paths = matching_paths_for(project)?;
274274
if paths.is_empty() {
275275
Err(Error::Devmode(DevmodeError::NoProjectFound))
276276
} else if paths.len() > 1 {
277-
let path = select_repo(project, None)?;
277+
let path = select_repo(paths)?;
278278
OpenAction::new(project).update(path)
279279
} else {
280280
OpenAction::new(project).update(
@@ -323,9 +323,6 @@ impl Cli {
323323
let settings = config_all()?;
324324
settings.write(false)?;
325325
}
326-
if config.map {
327-
OpenAction::make_dev_paths()?
328-
}
329326
if config.editor {
330327
let settings = config_editor()?;
331328
settings.write(false)?
@@ -349,56 +346,90 @@ impl Cli {
349346
let mut settings =
350347
Settings::current().ok_or(Error::Devmode(DevmodeError::AppSettingsNotFound))?;
351348
let Some(ref workspace_name) = arguments.name else {
352-
let workspaces = settings.workspaces.names;
353-
println!("Currently available workspaces: {workspaces:?}");
354-
return Ok(());
349+
return if arguments.list {
350+
let workspaces = settings.workspaces.names;
351+
println!("Currently available workspaces:");
352+
for workspace in workspaces {
353+
println!("- {}", workspace);
354+
}
355+
Ok(())
356+
} else {
357+
Err(Error::Devmode(DevmodeError::WorkspaceRequired))
358+
};
355359
};
356-
let mut workspace = Workspace::new(&workspace_name);
357-
if settings.workspaces.names.contains(workspace_name) {
360+
if arguments.add {
361+
if settings.workspaces.names.contains(workspace_name) {
362+
return Err(Error::Devmode(DevmodeError::WorkspaceExists(
363+
workspace_name.clone(),
364+
)));
365+
}
366+
let create = create_workspace()?;
367+
if create {
368+
settings.workspaces.names.push(workspace_name.clone());
369+
settings.write(true)?;
370+
println!("Workspace {workspace_name} was added.")
371+
}
372+
} else if settings.workspaces.names.contains(workspace_name) {
373+
let mut workspace = Workspace::new(&workspace_name);
358374
if arguments.delete {
359375
workspace.delete()?;
360376
println!("Workspace {workspace_name} was successfully deleted.");
361377
} else if let Some(ref to) = arguments.rename {
362378
workspace.rename(to)?;
363379
println!("Workspace renamed from {workspace_name} to {to}.");
364-
} else if let Some(ref project) = arguments.include {
365-
let paths: Vec<PathBuf> = find_paths(project)?
380+
} else if let Some(ref project_name) = arguments.include {
381+
let paths: Vec<PathBuf> = matching_paths_for(project_name)?
366382
.iter()
383+
.cloned()
367384
.filter(|path| !path.display().to_string().contains(workspace_name))
368-
.map(PathBuf::from)
369385
.collect();
370-
let project = if paths.len() > 0 {
371-
select_repo(project, Some(workspace_name))?
386+
387+
let project = if paths.len() == 0 {
388+
return Err(Error::Devmode(DevmodeError::ProjectNotFound));
389+
} else if paths.len() > 1 {
390+
let paths: Vec<String> =
391+
paths.iter().map(|s| s.display().to_string()).collect();
392+
PathBuf::from(select(
393+
"repo",
394+
"Select the repository you want to open:",
395+
paths,
396+
)?)
372397
} else {
373-
paths
374-
.get(0)
375-
.ok_or(Error::Devmode(DevmodeError::ProjectNotFound))?
376-
.clone()
398+
paths[0].clone()
377399
};
378400
let mut options = CopyOptions::new();
379401
let destination = project
380402
.parent()
381403
.ok_or(Error::Devmode(DevmodeError::PathNotFound))?
382404
.join(&workspace_name);
383405

384-
if destination.exists() {
406+
if destination.join(project_name).exists() {
385407
options.overwrite = overwrite()?;
386408
}
387409

410+
if !destination.exists() {
411+
std::fs::create_dir_all(&destination)?;
412+
}
413+
388414
move_items(&[project], destination, &options)?;
389415
} else if let Some(ref project_name) = arguments.remove {
390-
let paths: Vec<PathBuf> = find_paths(project_name)?
416+
let paths: Vec<PathBuf> = matching_paths_for(project_name)?
391417
.iter()
392-
.filter(|path| !path.display().to_string().contains(workspace_name))
393-
.map(PathBuf::from)
418+
.cloned()
419+
.filter(|path| path.display().to_string().contains(workspace_name))
394420
.collect();
395-
let project = if paths.len() > 0 {
396-
select_repo(project_name, Some(workspace_name))?
421+
let project = if paths.len() == 0 {
422+
return Err(Error::Devmode(DevmodeError::ProjectNotFound));
423+
} else if paths.len() > 1 {
424+
let paths: Vec<String> =
425+
paths.iter().map(|s| s.display().to_string()).collect();
426+
PathBuf::from(select(
427+
"repo",
428+
"Select the repository you want to open:",
429+
paths,
430+
)?)
397431
} else {
398-
paths
399-
.get(0)
400-
.ok_or(Error::Devmode(DevmodeError::ProjectNotFound))?
401-
.clone()
432+
paths[0].clone()
402433
};
403434
let mut options = dir::CopyOptions::new();
404435
let to = project
@@ -407,27 +438,16 @@ impl Cli {
407438
.parent()
408439
.ok_or(Error::Devmode(DevmodeError::PathNotFound))?;
409440

410-
if to.join(&project).exists() {
441+
if to.join(project_name).exists() {
411442
options.overwrite = overwrite()?;
412443
}
413444

414445
move_items(&[project.clone()], to, &options)?;
446+
} else if arguments.info {
447+
workspace.info()?;
415448
} else {
416449
println!("Workspace `{workspace_name}` found.");
417450
}
418-
} else if arguments.delete || arguments.rename.is_some() {
419-
return devmode::error("Couldn't find a workspace that matches {name}.");
420-
} else if arguments.add {
421-
settings.workspaces.names.push(workspace_name.clone());
422-
settings.write(true)?;
423-
println!("Workspace {workspace_name} was added.")
424-
} else {
425-
let create = create_workspace()?;
426-
if create {
427-
settings.workspaces.names.push(workspace_name.clone());
428-
settings.write(true)?;
429-
println!("Workspace {workspace_name} was added.")
430-
}
431451
}
432452
Ok(())
433453
}

src/cli/src/input.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use devmode::constants::names::{CUSTOM_NAME, NONE, VIM_NAME, VSCODE_NAME};
55
use devmode::editor::Editor;
66
use devmode::fork::ForkAction;
77
use devmode::host::Host;
8-
use devmode::project::find_paths;
98
use devmode::settings::Settings;
109
use devmode::DevmodeError;
1110
use devmode::{application::Application, Error};
@@ -148,29 +147,23 @@ pub fn config_editor() -> Result<Settings, Error> {
148147
Ok(settings)
149148
}
150149

151-
pub fn select_repo(project: &str, workspace: Option<&str>) -> Result<PathBuf, Error> {
152-
let paths = if let Some(workspace) = workspace {
153-
find_paths(project)?
154-
.iter()
155-
.filter(|path| path.display().to_string().contains(&workspace))
156-
.map(|path| path.display().to_string().to_owned())
157-
.collect::<Vec<String>>()
150+
pub fn select_repo(paths: Vec<PathBuf>) -> Result<PathBuf, Error> {
151+
let paths: Vec<String> = paths.iter().map(|s| s.display().to_string()).collect();
152+
let repo = if paths.len() > 1 {
153+
select("repo", "Select the repository you want to open:", paths)?
158154
} else {
159-
find_paths(project)?
160-
.iter()
161-
.map(|path| path.display().to_string().to_owned())
162-
.collect::<Vec<String>>()
155+
paths[0].clone()
163156
};
164-
let repo = select("repo", "Select the repository you want to open:", paths)?;
157+
165158
Ok(PathBuf::from(repo))
166159
}
167160

168161
pub fn create_workspace() -> Result<bool, Error> {
169-
let create = confirm("workspace", "Would you like to create this workspace?")?;
162+
let create = confirm("Would you like to create this workspace?", "workspace")?;
170163
Ok(create)
171164
}
172165

173166
pub fn overwrite() -> Result<bool, Error> {
174-
let overwrite = confirm("overwrite", "Found existing repository, overwrite it?")?;
167+
let overwrite = confirm("Found existing repository, overwrite it?", "overwrite")?;
175168
Ok(overwrite)
176169
}

src/shared/application.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ use serde::{Deserialize, Serialize};
99
use crate::constants::commands::*;
1010
use crate::constants::names::*;
1111

12-
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
12+
#[derive(Serialize, Default, Deserialize, Debug, Clone, Eq, PartialEq)]
1313
pub enum Application {
1414
VSCode,
1515
Vim,
1616
Custom,
17+
#[default]
1718
None,
1819
}
1920

@@ -59,19 +60,13 @@ impl Application {
5960
}
6061
}
6162

62-
impl Default for Application {
63-
fn default() -> Self {
64-
Application::None
65-
}
66-
}
67-
6863
impl Display for Application {
6964
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
7065
match self {
7166
Application::VSCode => write!(f, "{}", VSCODE_NAME),
7267
Application::Vim => write!(f, "{}", VIM_NAME),
7368
Application::Custom => write!(f, "{}", CUSTOM_NAME),
74-
_ => write!(f, "{}", DevmodeStatus::NoEditorSet.to_string()),
69+
_ => write!(f, "{}", DevmodeStatus::NoEditorSet),
7570
}
7671
}
7772
}

src/shared/clone.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use libset::routes::home;
99

1010
use crate::action::Action;
1111
use crate::host::Host;
12-
use crate::project::OpenAction;
1312
use crate::{error, git_pull, Error};
1413

1514
#[derive(Debug, Default, Clone, Setters)]
@@ -51,7 +50,7 @@ impl CloneAction {
5150
if let Some(parent) = path.parent() {
5251
let children: Vec<_> = std::fs::read_dir(parent)?.collect();
5352
if children.is_empty() {
54-
remove_dir_all(&parent)?;
53+
remove_dir_all(parent)?;
5554
}
5655
}
5756
}
@@ -60,7 +59,6 @@ impl CloneAction {
6059
}
6160

6261
git_pull::status_short(path.to_str().unwrap().to_string())?;
63-
OpenAction::make_dev_paths()?;
6462
Ok(())
6563
}
6664

src/shared/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[derive(Debug)]
22
pub struct Config {
3-
pub map: bool,
43
pub show: bool,
54
pub all: bool,
65
pub editor: bool,

src/shared/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ pub mod patterns {
44
pub const ORG_GIT_URL: &str = r#"(?:git@|https://)(?P<host>gitlab[.][^\s]+[.][^\s]+)[:/](?P<owner>[^\s]+)[/](?P<repo>[^\s,.]+)([.]git)?"#;
55
}
66

7+
pub const OS_SLASH: &str = if cfg!(target_os = "windows") {
8+
"\\"
9+
} else {
10+
"/"
11+
};
12+
713
pub mod names {
814
pub const VSCODE_NAME: &str = "Visual Studio Code";
915
pub const VIM_NAME: &str = "Vim";

src/shared/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ pub enum DevmodeError {
5656
FailedToGetBranch,
5757
#[error("Failed to find workspace")]
5858
WorkspaceMissing,
59+
#[error("Please provide a workspace")]
60+
WorkspaceRequired,
61+
#[error("Workspace {0} already exists")]
62+
WorkspaceExists(String),
5963
#[error("Failed to find project")]
6064
ProjectNotFound,
6165
#[error("Multiple projects found. Please specify the project name.")]
6266
MultipleProjectsFound,
6367
#[error("Path not found")]
6468
PathNotFound,
69+
#[error("File name not found")]
70+
FileNameNotFound,
6571
}

0 commit comments

Comments
 (0)