-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor][cli] [PIP-280] Create new pulsar-cli-utils module (#20782)
Co-authored-by: ran <[email protected]>
- Loading branch information
1 parent
e951cd0
commit 4f9b199
Showing
22 changed files
with
1,062 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
57 changes: 57 additions & 0 deletions
57
pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + ">!"); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.