55 */
66package io .jooby .cli ;
77
8- import picocli .CommandLine ;
8+ import java .nio .file .Files ;
9+ import java .nio .file .Path ;
10+ import java .nio .file .Paths ;
11+ import java .util .regex .Matcher ;
912
1013import javax .annotation .Nonnull ;
11- import java .nio .file .Path ;
14+
15+ import picocli .CommandLine ;
1216
1317/**
1418 * Set/configure options used by the tool (for now is just for workspace).
@@ -20,12 +24,31 @@ public class SetCmd extends Cmd {
2024 @ CommandLine .Spec
2125 private CommandLine .Model .CommandSpec spec ;
2226
23- @ CommandLine .Option (names = {"-w" , "--workspace" }, description = "Save the workspace directory. Projects are going to be created here" )
24- private Path workspace ;
27+ @ CommandLine .Option (names = {"-w" ,
28+ "--workspace" }, description = "Save the workspace directory. Projects are going to be created here" )
29+ private String workspace ;
30+
31+ @ CommandLine .Option (names = {"-f" ,
32+ "--force" }, description = "Force creation of workspace" )
33+ private boolean force ;
2534
2635 @ Override public void run (@ Nonnull Context ctx ) throws Exception {
2736 if (workspace != null ) {
28- ctx .setWorkspace (workspace );
37+ Path path = Paths .get (
38+ workspace .replaceFirst ("^~" , Matcher .quoteReplacement (System .getProperty ("user.home" ))))
39+ .normalize ()
40+ .toAbsolutePath ();
41+ if (Files .exists (path )) {
42+ ctx .setWorkspace (path );
43+ } else {
44+ if (force ) {
45+ Files .createDirectories (path );
46+ ctx .setWorkspace (path );
47+ } else {
48+ ctx .println ("Directory doesn't exist: " + path );
49+ ctx .println ("Use -f to force directory creation" );
50+ }
51+ }
2952 } else {
3053 ctx .println (spec .commandLine ().getUsageMessage ());
3154 }
0 commit comments