-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve (nano & less) command configuration management
- Loading branch information
Showing
5 changed files
with
103 additions
and
18 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
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
75 changes: 75 additions & 0 deletions
75
reader/src/main/java/org/jline/reader/ConfigurationPath.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,75 @@ | ||
/* | ||
* Copyright (c) 2002-2019, the original author or authors. | ||
* | ||
* This software is distributable under the BSD license. See the terms of the | ||
* BSD license in the documentation provided with this software. | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
package org.jline.reader; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
public class ConfigurationPath { | ||
private Path appConfig; | ||
private Path userConfig; | ||
|
||
/** | ||
* Configuration class constructor. | ||
* @param appConfig Application configuration directory | ||
* @param userConfig User private configuration directory | ||
*/ | ||
public ConfigurationPath(Path appConfig, Path userConfig) { | ||
this.appConfig = appConfig; | ||
this.userConfig = userConfig; | ||
} | ||
|
||
/** | ||
* Search configuration file first from userConfig and then appConfig directory. Returns null if file is not found. | ||
* @param name Configuration file name. | ||
* @return Configuration file. | ||
* | ||
*/ | ||
public Path getConfig(String name) { | ||
Path out = null; | ||
if (userConfig != null && userConfig.resolve(name).toFile().exists()) { | ||
out = userConfig.resolve(name); | ||
} else if (appConfig != null && appConfig.resolve(name).toFile().exists()) { | ||
out = appConfig.resolve(name); | ||
} | ||
return out; | ||
} | ||
|
||
/** | ||
* Search configuration file from userConfig directory. Returns null if file is not found. | ||
* @param name Configuration file name. | ||
* @return Configuration file. | ||
* @throws IOException When we do not have read access to the file or directory. | ||
* | ||
*/ | ||
public Path getUserConfig(String name) throws IOException { | ||
return getUserConfig(name, false); | ||
} | ||
|
||
/** | ||
* Search configuration file from userConfig directory. Returns null if file is not found. | ||
* @param name Configuration file name | ||
* @param create When true configuration file is created if not found. | ||
* @return Configuration file. | ||
* @throws IOException When we do not have read/write access to the file or directory. | ||
*/ | ||
public Path getUserConfig(String name, boolean create) throws IOException { | ||
Path out = null; | ||
if (userConfig != null) { | ||
if (!userConfig.resolve(name).toFile().exists() && create) { | ||
userConfig.resolve(name).toFile().createNewFile(); | ||
} | ||
if (userConfig.resolve(name).toFile().exists()) { | ||
out = userConfig.resolve(name); | ||
} | ||
} | ||
return out; | ||
} | ||
|
||
} |