You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2015/12/16 11:17:12 UTC

[2/5] mina-sshd git commit: Using a common verbosity arguments parsing method for 'ssh', 'scp', 'sftp' commands

Using a common verbosity arguments parsing method for 'ssh', 'scp', 'sftp' commands


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/4c106feb
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/4c106feb
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/4c106feb

Branch: refs/heads/master
Commit: 4c106febdc0bfd0d95d5e755b11fd86278bea192
Parents: 1b6d41b
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Wed Dec 16 12:12:38 2015 +0200
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Wed Dec 16 12:12:38 2015 +0200

----------------------------------------------------------------------
 .../java/org/apache/sshd/client/SshClient.java  | 80 +++++++++++++-------
 .../sshd/client/subsystem/sftp/SftpCommand.java |  6 +-
 2 files changed, 56 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/4c106feb/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java b/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java
index 5fce328..b60ebd2 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java
@@ -910,9 +910,26 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
         }
     }
 
-    //////////////////////////////////////////////////////////////////////////
+    public static Level resolveLoggingVerbosity(String ... args) {
+        return resolveLoggingVerbosity(args, GenericUtils.length(args));
+    }
 
-    public static void main(String[] args) throws Exception {
+    public static Level resolveLoggingVerbosity(String[] args, int maxIndex) {
+        for (int index = 0; index < maxIndex; index++) {
+            String argName = args[index];
+            if ("-v".equals(argName)) {
+                return Level.INFO;
+            } else if ("-vv".equals(argName)) {
+                return Level.FINE;
+            } else if ("-vvv".equals(argName)) {
+                return Level.FINEST;
+            }
+        }
+
+        return Level.WARNING;
+    }
+
+    public static Handler setupLogging(Level level) {
         Handler fh = new ConsoleHandler();
         fh.setLevel(Level.FINEST);
         fh.setFormatter(new Formatter() {
@@ -938,48 +955,61 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
             root.removeHandler(handler);
         }
         root.addHandler(fh);
+        root.setLevel(level);
+        return fh;
+    }
+
+    //////////////////////////////////////////////////////////////////////////
 
+    public static void main(String[] args) throws Exception {
         PrintStream stdout = System.out;
         PrintStream stderr = System.err;
         boolean agentForward = false;
         List<String> command = null;
-        int logLevel = 0;
         int socksPort = -1;
         int numArgs = GenericUtils.length(args);
         boolean error = false;
         String target = null;
+        Level level = Level.WARNING;
         for (int i = 0; i < numArgs; i++) {
             String argName = args[i];
-            if (command == null && "-D".equals(argName)) {
-                if (i + 1 >= numArgs) {
-                    System.err.println("option requires an argument: " + argName);
-                    error = true;
+            // handled by 'setupClientSession'
+            if ((command == null) && ("-i".equals(argName) || "-p".equals(argName) || "-o".equals(argName) || "-l".equals(argName))) {
+                if ((i + 1) >= numArgs) {
+                    error = showError(stderr, "option requires an argument: " + argName);
+                    break;
+                }
+
+                continue;
+            }
+
+            // verbosity handled separately
+            if ((command == null) && ("-v".equals(argName) || "-vv".equals(argName) || "-vvv".equals(argName))) {
+                continue;
+            }
+
+            if ((command == null) && "-D".equals(argName)) {
+                if ((i + 1) >= numArgs) {
+                    error = showError(stderr, "option requires an argument: " + argName);
                     break;
                 }
                 if (socksPort > 0) {
-                    stderr.println(argName + " option value re-specified: " + socksPort);
-                    error = true;
+                    error = showError(stderr, argName + " option value re-specified: " + socksPort);
                     break;
                 }
 
                 socksPort = Integer.parseInt(args[++i]);
                 if (socksPort <= 0) {
-                    stderr.println("Bad option value for " + argName + ": " + socksPort);
-                    error = true;
+                    error = showError(stderr, "Bad option value for " + argName + ": " + socksPort);
                     break;
                 }
-            } else if (command == null && "-v".equals(argName)) {
-                logLevel += 1;
-            } else if (command == null && "-vv".equals(argName)) {
-                logLevel += 2;
-            } else if (command == null && "-vvv".equals(argName)) {
-                logLevel += 3;
-            } else if (command == null && "-A".equals(argName)) {
+            } else if ((command == null) && "-A".equals(argName)) {
                 agentForward = true;
-            } else if (command == null && "-a".equals(argName)) {
+            } else if ((command == null) && "-a".equals(argName)) {
                 agentForward = false;
             } else {
-                if (command == null && target == null) {
+                level = resolveLoggingVerbosity(args, i);
+                if ((command == null) && target == null) {
                     target = argName;
                 } else {
                     if (command == null) {
@@ -990,15 +1020,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
             }
         }
 
-        if (logLevel <= 0) {
-            root.setLevel(Level.WARNING);
-        } else if (logLevel == 1) {
-            root.setLevel(Level.INFO);
-        } else if (logLevel == 2) {
-            root.setLevel(Level.FINE);
-        } else {
-            root.setLevel(Level.FINEST);
-        }
+        setupLogging(level);
 
         ClientSession session = null;
         try (BufferedReader stdin = new BufferedReader(new InputStreamReader(new NoCloseInputStream(System.in)))) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/4c106feb/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java
index eaf6f46..1fc041e 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java
@@ -30,6 +30,7 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.logging.Level;
 
 import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.session.ClientSession;
@@ -188,9 +189,12 @@ public class SftpCommand implements Channel {
         PrintStream stdout = System.out;
         PrintStream stderr = System.err;
         try (BufferedReader stdin = new BufferedReader(new InputStreamReader(new NoCloseInputStream(System.in)))) {
+            Level level = SshClient.resolveLoggingVerbosity(args);
+            SshClient.setupLogging(level);
+
             ClientSession session = SshClient.setupClientSession("-P", stdin, stdout, stderr, args);
             if (session == null) {
-                System.err.println("usage: sftp [-l login] [-P port] [-o option=value] hostname/user@host");
+                System.err.println("usage: sftp [-v[v][v]] [-i identity] [-l login] [-P port] [-o option=value] hostname/user@host");
                 System.exit(-1);
                 return;
             }