-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add password input from environment variable and file (#107)
Add password input from environment variable and file
- Loading branch information
Showing
7 changed files
with
127 additions
and
16 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
38 changes: 38 additions & 0 deletions
38
src/main/java/com/hivemq/cli/converters/EnvVarToByteBufferConverter.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,38 @@ | ||
/* | ||
* Copyright 2019 HiveMQ and the HiveMQ Community | ||
* | ||
* 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 com.hivemq.cli.converters; | ||
|
||
import picocli.CommandLine; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class EnvVarToByteBufferConverter implements CommandLine.ITypeConverter<ByteBuffer> { | ||
|
||
static final String ENVIRONMENT_VARIABLE_IS_NULL = "The given environment variable is not defined."; | ||
|
||
@Override | ||
public ByteBuffer convert(String value) throws Exception { | ||
final ByteBufferConverter converter = new ByteBufferConverter(); | ||
final String envVar = System.getenv(value); | ||
|
||
if (envVar == null) { | ||
throw new NullPointerException(ENVIRONMENT_VARIABLE_IS_NULL); | ||
} | ||
|
||
return converter.convert(envVar); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/hivemq/cli/converters/FileConverter.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,42 @@ | ||
package com.hivemq.cli.converters; | ||
/* | ||
* Copyright 2019 HiveMQ and the HiveMQ Community | ||
* | ||
* 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. | ||
* | ||
*/ | ||
import picocli.CommandLine; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
|
||
class FileConverter implements CommandLine.ITypeConverter<File> { | ||
|
||
static final String FILE_NOT_FOUND = "The given file was not found."; | ||
static final String NOT_A_FILE = "The given path does not lead to a valid file."; | ||
|
||
@Override | ||
public File convert(String value) throws Exception { | ||
|
||
final File file = new File(value); | ||
|
||
if (!file.exists()) | ||
throw new FileNotFoundException(FILE_NOT_FOUND); | ||
|
||
if (!file.isFile()) | ||
throw new IllegalArgumentException(NOT_A_FILE); | ||
|
||
|
||
return file; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/hivemq/cli/converters/FileToByteBufferConverter.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,34 @@ | ||
/* | ||
* Copyright 2019 HiveMQ and the HiveMQ Community | ||
* | ||
* 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 com.hivemq.cli.converters; | ||
|
||
import picocli.CommandLine; | ||
|
||
import java.io.File; | ||
import java.nio.ByteBuffer; | ||
import java.nio.file.Files; | ||
|
||
public class FileToByteBufferConverter implements CommandLine.ITypeConverter<ByteBuffer> { | ||
@Override | ||
public ByteBuffer convert(String value) throws Exception { | ||
final FileConverter fileConverter = new FileConverter(); | ||
|
||
final File file = fileConverter.convert(value); | ||
|
||
return ByteBuffer.wrap(Files.readAllBytes(file.toPath())); | ||
} | ||
} |
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
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