Skip to content

Commit

Permalink
[refactor][cli] [PIP-280] Create new pulsar-cli-utils module (apache#…
Browse files Browse the repository at this point in the history
…20782)

Co-authored-by: ran <[email protected]>
(cherry picked from commit 4f9b199)
  • Loading branch information
JooHyukKim authored and thetumbled committed Feb 28, 2024
1 parent 3b43433 commit 7639c66
Show file tree
Hide file tree
Showing 22 changed files with 1,062 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,7 @@ flexible messaging model and an intuitive client API.</description>
<module>pulsar-common</module>
<module>pulsar-broker-common</module>
<module>pulsar-broker</module>
<module>pulsar-cli-utils</module>
<module>pulsar-client-api</module>
<module>pulsar-client</module>
<module>pulsar-client-shaded</module>
Expand Down
149 changes: 149 additions & 0 deletions pulsar-cli-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
-->
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar</artifactId>
<version>3.1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

<artifactId>pulsar-cli-utils</artifactId>
<name>Pulsar CLI Utils</name>
<description>Isolated CLI utility module</description>

<dependencies>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>${pulsar.client.compiler.release}</release>
</configuration>
</plugin>

<plugin>
<groupId>org.gaul</groupId>
<artifactId>modernizer-maven-plugin</artifactId>
<configuration>
<failOnViolations>true</failOnViolations>
<javaVersion>8</javaVersion>
</configuration>
<executions>
<execution>
<id>modernizer</id>
<phase>verify</phase>
<goals>
<goal>modernizer</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<id>git-info</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>false</skip>
<useNativeGit>true</useNativeGit>
<prefix>git</prefix>
<verbose>false</verbose>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
<format>properties</format>
<gitDescribe>
<skip>true</skip>
</gitDescribe>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filtering-java-templates</id>
<goals>
<goal>filter-sources</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>${spotbugs-maven-plugin.version}</version>
<executions>
<execution>
<id>spotbugs</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeFilterFile>${basedir}/src/main/resources/findbugsExclude.xml</excludeFilterFile>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<id>checkstyle</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.pulsar.cli;

import com.beust.jcommander.ParameterException;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

@UtilityClass
public class ValueValidationUtil {

public static void maxValueCheck(String paramName, long value, long maxValue) {
if (value > maxValue) {
throw new ParameterException(paramName + " cannot be bigger than <" + maxValue + ">!");
}
}

public static void positiveCheck(String paramName, long value) {
if (value <= 0) {
throw new ParameterException(paramName + " cannot be less than or equal to <0>!");
}
}

public static void positiveCheck(String paramName, int value) {
if (value <= 0) {
throw new ParameterException(paramName + " cannot be less than or equal to <0>!");
}
}

public static void emptyCheck(String paramName, String value) {
if (StringUtils.isEmpty(value)) {
throw new ParameterException("The value of " + paramName + " can't be empty");
}
}

public static void minValueCheck(String name, Long value, long min) {
if (value < min) {
throw new ParameterException(name + " cannot be less than <" + min + ">!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.pulsar.cli.converters;

import static org.apache.pulsar.cli.ValueValidationUtil.emptyCheck;
import static org.apache.pulsar.cli.converters.ByteUnitUtil.validateSizeString;
import com.beust.jcommander.converters.BaseConverter;

public class ByteUnitIntegerConverter extends BaseConverter<Integer> {

public ByteUnitIntegerConverter(String optionName) {
super(optionName);
}

@Override
public Integer convert(String argStr) {
return parseBytes(argStr).intValue();
}

Long parseBytes(String argStr) {
emptyCheck(getOptionName(), argStr);
long valueInBytes = validateSizeString(argStr);
return valueInBytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.pulsar.cli.converters;

import static org.apache.pulsar.cli.ValueValidationUtil.emptyCheck;
import com.beust.jcommander.converters.BaseConverter;

public class ByteUnitToLongConverter extends BaseConverter<Long> {

public ByteUnitToLongConverter(String optionName) {
super(optionName);
}

@Override
public Long convert(String argStr) {
return parseBytes(argStr);
}

Long parseBytes(String argStr) {
emptyCheck(getOptionName(), argStr);
return ByteUnitUtil.validateSizeString(argStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.pulsar.cli.converters;

import com.beust.jcommander.ParameterException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import lombok.experimental.UtilityClass;

@UtilityClass
class ByteUnitUtil {

private static Set<Character> sizeUnit = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList('k', 'K', 'm', 'M', 'g', 'G', 't', 'T')));

static long validateSizeString(String byteStr) {
if (byteStr.isEmpty()) {
throw new IllegalArgumentException("byte string cannot be empty");
}

char last = byteStr.charAt(byteStr.length() - 1);
String subStr = byteStr.substring(0, byteStr.length() - 1);
long size;
try {
size = sizeUnit.contains(last)
? Long.parseLong(subStr)
: Long.parseLong(byteStr);
} catch (IllegalArgumentException e) {
throw new ParameterException(String.format("Invalid size '%s'. Valid formats are: %s",
byteStr, "(4096, 100K, 10M, 16G, 2T)"));
}
switch (last) {
case 'k':
case 'K':
return size * 1024;

case 'm':
case 'M':
return size * 1024 * 1024;

case 'g':
case 'G':
return size * 1024 * 1024 * 1024;

case 't':
case 'T':
return size * 1024 * 1024 * 1024 * 1024;

default:
return size;
}
}
}
Loading

0 comments on commit 7639c66

Please sign in to comment.