Skip to content

Commit

Permalink
Add cli module
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jul 16, 2020
1 parent feaf3d0 commit a9a5bea
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ lazy val core = project
testOptions in Test := Seq(Tests.Argument(TestFrameworks.JUnit, "-a")),
automaticModuleName("dev.dirs")
)

lazy val cli = project
.dependsOn(core)
.settings(
sharedSettings,
name := "directories-cli",
libraryDependencies += "info.picocli" % "picocli" % "4.4.0",
automaticModuleName("dev.dirs.cli")
)
71 changes: 71 additions & 0 deletions cli/src/main/java/dev/dirs/cli/DirectoriesCli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.dirs.cli;

import dev.dirs.ProjectDirectories;
import picocli.CommandLine;
import picocli.CommandLine.*;

@Command(name = "directories", mixinStandardHelpOptions = true, version = "20")
public class DirectoriesCli implements Runnable {
@Option(names = {"--cache"}, description = "Prints cache directory")
boolean cache;
@Option(names = {"--config"}, description = "Prints config directory")
boolean config;
@Option(names = {"--data"}, description = "Prints data directory")
boolean data;
@Option(names = {"--data-local"}, description = "Prints local data directory")
boolean dataLocal;
@Option(names = {"--preferences"}, description = "Prints preferences directory")
boolean preferences;
@Option(names = {"--runtime"}, description = "Prints runtime directory")
boolean runtime;

@Parameters(index = "0")
String project;

@Override
public void run() {
int dirCount = 0;
if (cache) dirCount += 1;
if (config) dirCount += 1;
if (data) dirCount += 1;
if (dataLocal) dirCount += 1;
if (preferences) dirCount += 1;
if (runtime) dirCount += 1;

boolean all = dirCount == 0;

ProjectDirectories projDirs = ProjectDirectories.from(null, null, project);

if (dirCount == 1) {
if (cache)
System.out.println(projDirs.cacheDir);
if (config)
System.out.println(projDirs.configDir);
if (data)
System.out.println(projDirs.dataDir);
if (dataLocal)
System.out.println(projDirs.dataLocalDir);
if (preferences)
System.out.println(projDirs.preferenceDir);
if (runtime)
System.out.println(projDirs.runtimeDir);
} else {
if (all || cache)
System.out.println("cache: " + projDirs.cacheDir);
if (all || config)
System.out.println("config: " + projDirs.configDir);
if (all || data)
System.out.println("data: " + projDirs.dataDir);
if (all || dataLocal)
System.out.println("local data: " + projDirs.dataLocalDir);
if (all || preferences)
System.out.println("preference: " + projDirs.preferenceDir);
if (all || runtime)
System.out.println("runtime: " + projDirs.runtimeDir);
}
}

public static void main(String... args) {
System.exit(new CommandLine(new DirectoriesCli()).execute(args));
}
}

0 comments on commit a9a5bea

Please sign in to comment.