Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support "-v/--version" to get version of casbin-java-cli #28

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,49 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>generate-version-info</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/META-INF</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>pom.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.9.10</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<abbrevLength>7</abbrevLength>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/META-INF/git.properties</generateGitPropertiesFilename>
<format>properties</format>
</configuration>
</plugin>
</plugins>
</build>
</project>
7 changes: 7 additions & 0 deletions src/main/java/org/casbin/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.util.*;

import static org.casbin.util.VersionUtil.*;

public class Client {

public static String run(String... args) {
Expand All @@ -21,6 +23,11 @@ public static String run(String... args) {
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
printHelpMessage();
return result;
} else if(Objects.equals(commandName, "-v") || Objects.equals(commandName, "--version")){
String cliVersion = getCliVersion();
String jcasbinVersion = getCasbinVersion("org.casbin","jcasbin");
System.out.printf("casbin-java-cli %s\njcasbin %s%n",cliVersion,jcasbinVersion);
return result;
}

// processing line breaks in parameters
Expand Down
111 changes: 111 additions & 0 deletions src/main/java/org/casbin/util/DependencyHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2024 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.casbin.util;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

/***
* A custom handler for parsing XML dependencies (POM file format) using SAX (Simple API for XML).
* This handler looks for a specific dependency identified by the group ID and artifact ID,
* and extracts its version information.
*/
public class DependencyHandler extends DefaultHandler {
private final String targetGroupId;
private final String targetArtifactId;
private String currentElement;
private String currentGroupId;
private String currentArtifactId;
private String currentVersion;
private String dependencyVersion;

/**
* Constructor to initialize the handler with the target groupId and artifactId.
*
* @param groupId The groupId of the dependency to search for.
* @param artifactId The artifactId of the dependency to search for.
*/
public DependencyHandler(String groupId, String artifactId) {
this.targetGroupId = groupId;
this.targetArtifactId = artifactId;
}

/**
* Called when a new element is encountered during the XML parsing.
*
* @param uri The namespace URI (if any).
* @param localName The local name of the element.
* @param qName The qualified name of the element.
* @param attributes The attributes of the element.
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
currentElement = qName;
if ("dependency".equals(currentElement)) {
currentGroupId = null;
currentArtifactId = null;
currentVersion = null;
}
}

/**
* Called when the end of an element is reached during XML parsing.
*
* @param uri The namespace URI (if any).
* @param localName The local name of the element.
* @param qName The qualified name of the element.
*/
@Override
public void endElement(String uri, String localName, String qName) {
if ("dependency".equals(qName)) {
if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) {
dependencyVersion = currentVersion;
}
}
currentElement = null;
}

/**
* Called to process the character data inside an element during XML parsing.
*
* @param ch The character array containing the text.
* @param start The start index of the text.
* @param length The length of the text.
*/
@Override
public void characters(char[] ch, int start, int length) {
String content = new String(ch, start, length).trim();
if (content.isEmpty()) {
return;
}

if ("groupId".equals(currentElement)) {
currentGroupId = content;
} else if ("artifactId".equals(currentElement)) {
currentArtifactId = content;
} else if ("version".equals(currentElement)) {
currentVersion = content;
}
}

/**
* Returns the version of the dependency if found, otherwise returns "Unknown".
*
* @return The version of the target dependency.
*/
public String getDependencyVersion() {
return dependencyVersion != null ? dependencyVersion : "Unknown";
}
}
85 changes: 85 additions & 0 deletions src/main/java/org/casbin/util/VersionUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2024 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.casbin.util;

import org.casbin.Client;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/***
* Utility class for retrieving version-related information.
*/
public class VersionUtil {

/**
* Retrieves Cli version information.
*
* @return The Cli version info as a string (commit ID or tag name).
* @throws IOException If an error occurs while reading the git.properties file.
*/
public static String getCliVersion() throws IOException {
Properties properties = new Properties();
InputStream input = Client.class.getResourceAsStream("/META-INF/git.properties");
if (input != null) {
properties.load(input);
}

if (properties.isEmpty()) {
throw new IOException("Git properties not found!");
}

String commitId = properties.getProperty("git.commit.id.abbrev", "Unknown");
String tag = properties.getProperty("git.closest.tag.name", "Unknown");
String commitCount = properties.getProperty("git.closest.tag.commit.count", "Unknown");

if(tag.isEmpty()) {
tag = properties.getProperty("git.tags", "Unknown");
}
if (commitCount.isEmpty()) {
return tag;
}
return commitId;
}

/**
* Retrieves Casbin version information.
*
* @param groupId The group ID of the dependency to search for.
* @param artifactId The artifact ID of the dependency to search for.
* @return The version of the specified dependency, or "Unknown" if not found.
* @throws ParserConfigurationException If a configuration error occurs during parsing.
* @throws SAXException If a SAX error occurs during parsing.
* @throws IOException If an error occurs while reading the POM file.
*/
public static String getCasbinVersion(String groupId, String artifactId) throws ParserConfigurationException, SAXException, IOException {
InputStream inputStream = Client.class.getResourceAsStream("/META-INF/pom.xml");
if (inputStream == null) {
throw new IOException("POM file not found!");
}

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
DependencyHandler handler = new DependencyHandler(groupId, artifactId);
parser.parse(inputStream, handler);

return handler.getDependencyVersion();
}
}
Loading