@@ -231,36 +231,20 @@ fn prompt_new_package() -> Result<()> {
231231 execute_new_package ( & path)
232232}
233233
234- fn execute_new_workspace ( workspace : & str , repo : Option < & str > ) -> Result < ( ) > {
235- if get_workspace ( ) . is_some ( ) {
236- bail ! ( "Cannot create a workspace inside an existing workspace" ) ;
237- }
238-
239- validate_name ( workspace, "Workspace" ) ?;
240-
241- let repo =
242- repo. ok_or_else ( || anyhow:: anyhow!( "--repo is required when creating a workspace" ) ) ?;
243- let repository = clean_repo_url ( repo) ?;
244-
245- let workspace_path = Path :: new ( workspace) ;
246-
247- if workspace_path. exists ( ) {
248- bail ! ( "Directory '{}' already exists" , workspace) ;
249- }
250-
251- std:: fs:: create_dir_all ( workspace_path)
252- . with_context ( || format ! ( "Failed to create directory '{}'" , workspace) ) ?;
253-
254- let status = Command :: new ( "git" )
255- . args ( [ "init" , "-b" , "main" ] )
256- . current_dir ( workspace_path)
257- . stdout ( Stdio :: null ( ) )
258- . stderr ( Stdio :: null ( ) )
259- . status ( )
260- . context ( "Failed to run 'git init'" ) ?;
261-
262- if !status. success ( ) {
263- bail ! ( "'git init' failed with exit code: {:?}" , status. code( ) ) ;
234+ /// Initialize workspace scaffolding in an existing directory: pcb.toml, README,
235+ /// .gitignore, git init, and skill path. `repository` may be empty.
236+ pub ( crate ) fn init_workspace ( dir : & Path , repository : & str ) -> Result < ( ) > {
237+ if !dir. join ( ".git" ) . exists ( ) {
238+ let status = Command :: new ( "git" )
239+ . args ( [ "init" , "-b" , "main" ] )
240+ . current_dir ( dir)
241+ . stdout ( Stdio :: null ( ) )
242+ . stderr ( Stdio :: null ( ) )
243+ . status ( )
244+ . context ( "Failed to run 'git init'" ) ?;
245+ if !status. success ( ) {
246+ bail ! ( "'git init' failed with exit code: {:?}" , status. code( ) ) ;
247+ }
264248 }
265249
266250 let env = create_template_env ( ) ;
@@ -274,21 +258,44 @@ fn execute_new_workspace(workspace: &str, repo: Option<&str>) -> Result<()> {
274258 . unwrap ( )
275259 . render ( & ctx)
276260 . context ( "Failed to render pcb.toml template" ) ?;
277- std:: fs:: write ( workspace_path. join ( "pcb.toml" ) , pcb_toml_content)
278- . context ( "Failed to write pcb.toml" ) ?;
261+ std:: fs:: write ( dir. join ( "pcb.toml" ) , pcb_toml_content) . context ( "Failed to write pcb.toml" ) ?;
279262
280263 let readme_content = env
281264 . get_template ( "workspace_readme" )
282265 . unwrap ( )
283266 . render ( & ctx)
284267 . context ( "Failed to render README.md template" ) ?;
285- std:: fs:: write ( workspace_path. join ( "README.md" ) , readme_content)
286- . context ( "Failed to write README.md" ) ?;
268+ std:: fs:: write ( dir. join ( "README.md" ) , readme_content) . context ( "Failed to write README.md" ) ?;
287269
288- std:: fs:: write ( workspace_path . join ( ".gitignore" ) , GITIGNORE_TEMPLATE )
270+ std:: fs:: write ( dir . join ( ".gitignore" ) , GITIGNORE_TEMPLATE )
289271 . context ( "Failed to write .gitignore" ) ?;
290272
291- add_skill_to_path ( workspace_path) ?;
273+ add_skill_to_path ( dir) ?;
274+
275+ Ok ( ( ) )
276+ }
277+
278+ fn execute_new_workspace ( workspace : & str , repo : Option < & str > ) -> Result < ( ) > {
279+ if get_workspace ( ) . is_some ( ) {
280+ bail ! ( "Cannot create a workspace inside an existing workspace" ) ;
281+ }
282+
283+ validate_name ( workspace, "Workspace" ) ?;
284+
285+ let repo =
286+ repo. ok_or_else ( || anyhow:: anyhow!( "--repo is required when creating a workspace" ) ) ?;
287+ let repository = clean_repo_url ( repo) ?;
288+
289+ let workspace_path = Path :: new ( workspace) ;
290+
291+ if workspace_path. exists ( ) {
292+ bail ! ( "Directory '{}' already exists" , workspace) ;
293+ }
294+
295+ std:: fs:: create_dir_all ( workspace_path)
296+ . with_context ( || format ! ( "Failed to create directory '{}'" , workspace) ) ?;
297+
298+ init_workspace ( workspace_path, & repository) ?;
292299
293300 eprintln ! (
294301 "{} {} ({})" ,
0 commit comments