FELIX-3494 - suppress printing of motd

- make some of the arguments of Gosh itself available to the shell;
- add an "quiet" option to suppress printing of the motd.



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1725549 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
index c6b6eb3..c1051ef 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
@@ -343,40 +343,46 @@
     {
         boolean found = false;
         Formatter f = new Formatter();
-        f.close();
 
-        Method methods[] = b.getClass().getMethods();
-        for (Method m : methods)
+        try
         {
-            try
+            Method methods[] = b.getClass().getMethods();
+            for (Method m : methods)
             {
-                String name = m.getName();
-                if (!name.equals("getClass") && name.startsWith("get") && m.getParameterTypes().length == 0)
+                try
                 {
-                    m.setAccessible(true);
-                    Object value = m.invoke(b);
+                    String name = m.getName();
+                    if (!name.equals("getClass") && name.startsWith("get") && m.getParameterTypes().length == 0)
+                    {
+                        m.setAccessible(true);
+                        Object value = m.invoke(b);
 
-                    found = true;
-                    name = name.substring(3);
-                    f.format(COLUMN, name, format(value, Converter.LINE, this));
+                        found = true;
+                        name = name.substring(3);
+                        f.format(COLUMN, name, format(value, Converter.LINE, this));
+                    }
+                }
+                catch (IllegalAccessException e)
+                {
+                    // Ignore
+                }
+                catch (Exception e)
+                {
+                    e.printStackTrace();
                 }
             }
-            catch (IllegalAccessException e)
+            if (found)
             {
-                // Ignore
+                return (StringBuilder) f.out();
             }
-            catch (Exception e)
+            else
             {
-                e.printStackTrace();
+                return b.toString();
             }
         }
-        if (found)
+        finally
         {
-            return (StringBuilder) f.out();
-        }
-        else
-        {
-            return b.toString();
+            f.close();
         }
     }
 
diff --git a/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Posix.java b/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Posix.java
index 39b8005..e96d5aa 100644
--- a/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Posix.java
+++ b/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Posix.java
@@ -50,7 +50,7 @@
         }
 
         URI cwd = Shell.cwd(session);
-        
+
         for (String arg : args)
         {
             copy(cwd.resolve(arg), System.out);
@@ -141,7 +141,7 @@
             {
                 URI cwd = Shell.cwd(session);
                 in = (arg == null) ? System.in : cwd.resolve(arg).toURL().openStream();
-                
+
                 BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
                 int line = 0;
                 String s;
@@ -180,21 +180,26 @@
 
         return match && status;
     }
-    
-    public static void copy(URI source, OutputStream out) throws IOException {
+
+    public static void copy(URI source, OutputStream out) throws IOException
+    {
         InputStream in = source.toURL().openStream();
-        try {
+        try
+        {
             copy(in, out);
-        } finally {
+        }
+        finally
+        {
             in.close();
         }
     }
 
-
-    public static void copy(InputStream in, OutputStream out) throws IOException {
+    public static void copy(InputStream in, OutputStream out) throws IOException
+    {
         byte buf[] = new byte[10240];
         int len;
-        while ((len = in.read(buf)) > 0) {
+        while ((len = in.read(buf)) > 0)
+        {
             out.write(buf, 0, len);
         }
         out.flush();
diff --git a/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java b/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java
index 8344581..8c26ed0 100644
--- a/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java
+++ b/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java
@@ -71,6 +71,7 @@
                 "     --nointeractive       don't start interactive session",
                 "     --login               login shell (same session, reads etc/gosh_profile)",
                 "  -s --noshutdown          don't shutdown framework when script completes",
+                "  -q --quiet               don't display message-of-the-day",
                 "  -x --xtrace              echo commands before execution",
                 "  -? --help                show help",
                 "If no script-file, an interactive shell is started, type $D to exit." };
@@ -100,6 +101,10 @@
         }
 
         CommandSession newSession = (login ? session : processor.createSession(session.getKeyboard(), session.getConsole(), System.err));
+        // Make some of the given arguments available to the shell itself...
+        newSession.put(".gosh_login", login);
+        newSession.put(".gosh_interactive", interactive);
+        newSession.put(".gosh_quiet", opt.isSet("quiet"));
 
         if (opt.isSet("xtrace"))
         {
diff --git a/gogo/shell/src/main/resources/gosh_profile b/gogo/shell/src/main/resources/gosh_profile
index d27c97b..98eb0c5 100644
--- a/gogo/shell/src/main/resources/gosh_profile
+++ b/gogo/shell/src/main/resources/gosh_profile
@@ -30,8 +30,8 @@
   # set prompt
   prompt = 'g! '
 
-  # print welcome message
-  cat ($0 resolve motd)
+  # print welcome message, unless we're explicitly told not to...
+  if {$.gosh_quiet} {} { cat ($0 resolve motd) }
 } {
     echo "$0: ERROR: $exception"
 }