Skip to content

Commit 26cfeaa

Browse files
committed
feat: support "-v/--version" to get version of casbin-java-cli
1 parent 49231e0 commit 26cfeaa

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/main/java/org/casbin/Client.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import org.casbin.jcasbin.util.function.CustomFunction;
77
import org.casbin.util.Util;
88

9-
import java.util.*;
9+
import java.io.File;
10+
import java.io.FileInputStream;
11+
import java.io.IOException;
12+
import java.util.Arrays;
13+
import java.util.Objects;
14+
import java.util.Properties;
1015

1116

1217
public class Client {
@@ -23,9 +28,19 @@ public static String run(String... args) {
2328
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
2429
printHelpMessage();
2530
return result;
31+
} else if(Objects.equals(commandName, "-v") || Objects.equals(commandName, "--version")){
32+
try{
33+
System.out.println("casbin-java-cli " + getVersion());
34+
}catch (IOException e) {
35+
System.out.println("Failed to retrieve version information.");
36+
e.printStackTrace();
37+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
38+
}
39+
return result;
2640
}
2741

2842
CommandLine cmd = getCmd(Arrays.copyOfRange(args, 1, args.length));
43+
2944
String model = cmd.getOptionValue("model");
3045
String policy = cmd.getOptionValue("policy");
3146
NewEnforcer enforcer = new NewEnforcer(model, policy);
@@ -101,6 +116,7 @@ private static void printHelpMessage() {
101116
" -m, --model <model> The path of the model file or model text. Please wrap it with \"\" and separate each line with \"|\"\n" +
102117
" -p, --policy <policy> The path of the policy file or policy text. Please wrap it with \"\" and separate each line with \"|\"\n" +
103118
" -AF, --addFunction <functionDefinition> Add custom function. Please wrap it with \"\" and separate each line with \"|\"\n" +
119+
" -v, --version The version of casbin-java-cli" +
104120
"\n" +
105121
" args:\n" +
106122
" Parameters required for the method\n" +
@@ -114,4 +130,26 @@ private static void printHelpMessage() {
114130
" For more information, visit https://github.com/casbin/casbin");
115131

116132
}
133+
134+
/***
135+
* Retrieves the version of the project.
136+
* @return The version of the project, or "Version not found" if the version is not found in the file.
137+
* @throws Exception If an error occurs while reading the file or loading the properties.
138+
*/
139+
private static String getVersion() throws Exception {
140+
String classesPath = Client.class
141+
.getProtectionDomain()
142+
.getCodeSource()
143+
.getLocation()
144+
.getPath();
145+
String targetPath = new File(classesPath).getParentFile().getCanonicalPath();
146+
String propertiesPath = targetPath + File.separator + "maven-archiver" + File.separator + "pom.properties";
147+
FileInputStream fileInputStream = new FileInputStream(propertiesPath);
148+
149+
Properties properties = new Properties();
150+
properties.load(fileInputStream);
151+
String version = properties.getProperty("version");
152+
153+
return version != null ? version : "Version not found";
154+
}
117155
}

0 commit comments

Comments
 (0)