88 *******************************************************************************/
99package org .cryptomator .cli ;
1010
11- import java .io .IOException ;
12- import java .nio .file .Files ;
13- import java .nio .file .Path ;
1411import java .nio .file .Paths ;
12+ import java .util .HashMap ;
13+ import java .util .Map ;
1514import java .util .Properties ;
1615import java .util .Set ;
1716import java .util .stream .Collectors ;
18- import java .util .stream .Stream ;
1917
2018import org .apache .commons .cli .CommandLine ;
2119import org .apache .commons .cli .DefaultParser ;
2220import org .apache .commons .cli .HelpFormatter ;
2321import org .apache .commons .cli .Option ;
2422import org .apache .commons .cli .Options ;
2523import org .apache .commons .cli .ParseException ;
24+ import org .cryptomator .cli .pwd .PasswordFromFileStrategy ;
25+ import org .cryptomator .cli .pwd .PasswordFromStdInputStrategy ;
26+ import org .cryptomator .cli .pwd .PasswordStrategy ;
27+ import org .cryptomator .cli .pwd .PasswordFromPropertyStrategy ;
2628
2729/**
2830 * Parses program arguments. Does not validate them.
@@ -76,17 +78,15 @@ public class Args {
7678 private final Properties vaultPaths ;
7779 private final Properties vaultPasswords ;
7880 private final Properties vaultPasswordFiles ;
79-
80- private boolean hasPasswordOrPasswordFile (Object vaultPath ) {
81- return vaultPasswords .containsKey (vaultPath ) || vaultPasswordFiles .containsKey (vaultPath );
82- }
81+ private final Map <String , PasswordStrategy > passwordStrategies ;
8382
8483 public Args (CommandLine commandLine ) throws ParseException {
8584 this .bindAddr = commandLine .getOptionValue ("bind" , "localhost" );
8685 this .port = Integer .parseInt (commandLine .getOptionValue ("port" , "0" ));
8786 this .vaultPaths = commandLine .getOptionProperties ("vault" );
8887 this .vaultPasswords = commandLine .getOptionProperties ("password" );
8988 this .vaultPasswordFiles = commandLine .getOptionProperties ("passwordfile" );
89+ this .passwordStrategies = new HashMap <>();
9090 }
9191
9292 public String getBindAddr () {
@@ -98,32 +98,13 @@ public int getPort() {
9898 }
9999
100100 public Set <String > getVaultNames () {
101- return vaultPaths .keySet ().stream ().filter ( this :: hasPasswordOrPasswordFile ). map (String .class ::cast ).collect (Collectors .toSet ());
101+ return vaultPaths .keySet ().stream ().map (String .class ::cast ).collect (Collectors .toSet ());
102102 }
103103
104104 public String getVaultPath (String vaultName ) {
105105 return vaultPaths .getProperty (vaultName );
106106 }
107107
108- public String getVaultPasswordPath (String vaultName ) {
109- return vaultPasswordFiles .getProperty (vaultName );
110- }
111-
112- public String getVaultPassword (String vaultName ) {
113- if (vaultPasswords .getProperty (vaultName ) == null ) {
114- Path vaultPasswordPath = Paths .get (vaultPasswordFiles .getProperty (vaultName ));
115- if (Files .isReadable (vaultPasswordPath ) && Files .isRegularFile (vaultPasswordPath )) {
116- try (Stream <String > lines = Files .lines (vaultPasswordPath )) {
117- return lines .findFirst ().get ().toString ();
118- } catch (IOException e ) {
119- return null ;
120- }
121- }
122- return null ;
123- }
124- return vaultPasswords .getProperty (vaultName );
125- }
126-
127108 public static Args parse (String [] arguments ) throws ParseException {
128109 CommandLine commandLine = new DefaultParser ().parse (OPTIONS , arguments );
129110 return new Args (commandLine );
@@ -133,4 +114,26 @@ public static void printUsage() {
133114 new HelpFormatter ().printHelp (USAGE , OPTIONS );
134115 }
135116
117+ public PasswordStrategy addPasswortStrategy (final String vaultName ) {
118+ PasswordStrategy passwordStrategy = new PasswordFromStdInputStrategy (vaultName );
119+
120+ if (vaultPasswords .getProperty (vaultName ) != null ) {
121+ passwordStrategy = new PasswordFromPropertyStrategy (
122+ vaultName ,
123+ vaultPasswords .getProperty (vaultName )
124+ );
125+ } else if (vaultPasswordFiles .getProperty (vaultName ) != null ) {
126+ passwordStrategy = new PasswordFromFileStrategy (
127+ vaultName ,
128+ Paths .get (vaultPasswordFiles .getProperty (vaultName ))
129+ );
130+ }
131+
132+ this .passwordStrategies .put (vaultName , passwordStrategy );
133+ return passwordStrategy ;
134+ }
135+
136+ public PasswordStrategy getPasswordStrategy (final String vaultName ) {
137+ return passwordStrategies .get (vaultName );
138+ }
136139}
0 commit comments