geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r700087 - in /geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main: java/org/apache/geronimo/gshell/commands/vfs/ resources/META-INF/spring/ resources/org/apache/geronimo/gshell/commands/vfs/
Date Mon, 29 Sep 2008 12:17:34 GMT
Author: jdillon
Date: Mon Sep 29 05:17:33 2008
New Revision: 700087

URL: http://svn.apache.org/viewvc?rev=700087&view=rev
Log:
Adding 'edit' command, which launches an external editor with a local file reference (copies
non-local files to tmp local files if needed, and updates originals etc)

Added:
    geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java
  (contents, props changed)
      - copied, changed from r700009, geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/CatAction.java
    geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties
  (contents, props changed)
      - copied, changed from r700009, geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/CatAction.properties
Modified:
    geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/VfsActionSupport.java
    geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml
    geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/ListDirectoryAction.properties

Copied: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java
(from r700009, geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/CatAction.java)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java?p2=geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java&p1=geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/CatAction.java&r1=700009&r2=700087&rev=700087&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/CatAction.java
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java
Mon Sep 29 05:17:33 2008
@@ -19,46 +19,45 @@
 
 package org.apache.geronimo.gshell.commands.vfs;
 
-import org.apache.commons.vfs.FileContent;
-import org.apache.commons.vfs.FileContentInfo;
 import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystem;
 import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.Selectors;
+import org.apache.commons.vfs.provider.local.LocalFileSystem;
+import org.apache.commons.vfs.provider.local.LocalFile;
+import org.apache.commons.vfs.util.Os;
 import org.apache.geronimo.gshell.clp.Argument;
 import org.apache.geronimo.gshell.clp.Option;
 import org.apache.geronimo.gshell.command.CommandContext;
 import org.apache.geronimo.gshell.io.IO;
-import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.util.StringUtils;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
+import java.util.List;
+import java.util.Arrays;
+import java.io.File;
+import java.lang.reflect.Field;
 
 /**
- * Displays the contents of a file.
+ * Edit a file with an external editor.
  *
  * @version $Rev$ $Date$
  */
-public class CatAction
+public class EditAction
     extends VfsActionSupport
 {
+    @Option(name="-e", aliases={"--editor"})
+    private String editor;
+    
     @Argument(required=true)
     private String path;
 
-    @Option(name="-n")
-    private boolean displayLineNumbers;
-
     public Object execute(final CommandContext context) throws Exception {
         assert context != null;
         IO io = context.getIo();
 
-        //
-        // TODO: Support multi-path cat, and the special '-' token (which is the default
if no paths are given)
-        //
-
         FileObject file = resolveFile(context, path);
 
         if (!file.exists()) {
+            // TODO: Allow creation of files which don't exist
             io.error("File not found: {}", file.getName());
             return Result.FAILURE;
         }
@@ -66,42 +65,135 @@
             io.error("File is a directory: {}", file.getName());
             return Result.FAILURE;
         }
+        else if (!file.isReadable()) {
+            io.error("File is not readble: {}", file.getName());
+            return Result.FAILURE;
+        }
+        else if (!file.isWriteable()) {
+            io.error("File is not writable: {}", file.getName());
+            return Result.FAILURE;
+        }
+        FileObject tmp = file;
+
+        FileSystem fs = file.getFileSystem();
+        log.debug("File system: {}", fs);
+
+        // If the file is not on the local file system, then create tmp file for editing
+        if (!(fs instanceof LocalFileSystem)) {
+            // Create a new temporary file, copy the contents for editing
+            // TODO: Ask branding for the app name er something here?
+            // TODO: Check if we need to put this timestamp in here or not, VFS might make
it unique anyways
+            tmp = resolveFile(context, "tmp:/gshell.edit-" + System.currentTimeMillis() +
".txt");
+            log.debug("Using temporary file for edit: {} ({})", tmp, tmp.getClass());
+            tmp.createFile();
+            tmp.copyFrom(file, Selectors.SELECT_SELF);
+        }
+
+        // Have to dereference the VFS file into a local file so the editor can access it
+        File localFile = getLocalFile(tmp);
+        Object result = edit(context, localFile);
+
+        // If we had to use a tmp file for editing, then copy back and clean up
+        if (tmp != file) {
+            log.debug("Updating original file with edited content");
+            file.copyFrom(tmp, Selectors.SELECT_SELF);
+            tmp.delete();
+        }
+
+        return result;
+    }
 
-        FileContent content = file.getContent();
-        FileContentInfo info = content.getContentInfo();
-        log.debug("Content type: {}", info.getContentType());
-        log.debug("Content encoding: {}", info.getContentEncoding());
+    private File getLocalFile(final FileObject file) throws Exception {
+        assert file != null;
+        assert file instanceof LocalFile;
 
         //
-        // TODO: Only cat files which we think are text
+        // HACK: Need to get access to the LocalFile's file field.
         //
 
-        log.debug("Displaying file: {}", file.getName());
+        // Force the file to attach if it hasn't already
+        file.exists();
+
+        Class type = file.getClass();
+        log.debug("File type: {}", type);
+
+        Field field = type.getDeclaredField("file");
+        log.debug("File field: {}", field);
+
+        File localFile;
 
-        BufferedReader reader = new BufferedReader(new InputStreamReader(content.getInputStream()));
         try {
-            cat(reader, io);
+            localFile = (File)field.get(file);
         }
-        finally {
-            IOUtil.close(reader);
+        catch (IllegalAccessException ignore) {
+            field.setAccessible(true);
+            localFile = (File) field.get(file);        
+        }
+
+        log.debug("Local file: {}", localFile);
+
+        return localFile;
+    }
+
+    private Object edit(final CommandContext context, final File localFile) throws Exception
{
+        assert context != null;
+        assert localFile != null;
+
+        log.debug("Editing file: {}", localFile);
+
+        List<String> editorCmd = selectEditor();
+
+        log.debug("Executing: {} {}", editorCmd, localFile);
+
+        ProcessBuilder builder = new ProcessBuilder();
+        for (String s : editorCmd) {
+            builder.command().add(s);
         }
+        builder.command().add(localFile.getAbsolutePath());
+
+        Process p = builder.start();
+
+        log.debug("Waiting for process to exit...");
+        int status = p.waitFor();
+        log.info("Process exited w/status: {}", status);
 
-        io.out.println();
+        return status;
+    }
+
+    private List<String> selectEditor() {
+        String cmd;
+
+        if (editor != null) {
+            cmd = editor;
+        }
+        else {
+            // TODO: Expose a configurable preference for this
+            cmd = getDefaultEditor();
+        }
 
-        return Result.SUCCESS;
+        // The editor configuration may need to set arguments or whatever, so we have to
return a list
+        return Arrays.asList(cmd.split("\\s"));
     }
 
-    private void cat(final BufferedReader reader, final IO io) throws IOException {
-        String line;
-        int lineno = 1;
-
-        while ((line = reader.readLine()) != null) {
-            if (displayLineNumbers) {
-                String gutter = StringUtils.leftPad(String.valueOf(lineno++), 6);
-                io.out.print(gutter);
-                io.out.print("  ");
+    private String getDefaultEditor() {
+        if (Os.isFamily(Os.OS_FAMILY_WINDOWS)) {
+            return "NOTEPAD";    
+        }
+        else if (Os.isFamily(Os.OS_FAMILY_UNIX)) {
+            if (System.getenv("DISPLAY") != null) {
+                String tmp = System.getenv("XEDITOR");
+                if (tmp != null) {
+                    return tmp;
+                }
+            }
+
+            String tmp = System.getenv("EDITOR");
+            if (tmp != null) {
+                return tmp;
             }
-            io.out.println(line);
+
         }
+
+        throw new RuntimeException("Unable to determine the default editor command");
     }
 }
\ No newline at end of file

Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/EditAction.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/VfsActionSupport.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/VfsActionSupport.java?rev=700087&r1=700086&r2=700087&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/VfsActionSupport.java
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/VfsActionSupport.java
Mon Sep 29 05:17:33 2008
@@ -47,11 +47,6 @@
         assert fileSystemAccess != null;
         return fileSystemAccess;
     }
-
-    //
-    // TODO: Hook up monitor support to display something while long operations or going
on.  Might not
-    //       be possible with VFS 1.0, as its on the list of things to be done.
-    //
     
     protected FileObject getCurrentDirectory(final CommandContext context) throws FileSystemException
{
         assert context != null;

Modified: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml?rev=700087&r1=700086&r2=700087&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml
Mon Sep 29 05:17:33 2008
@@ -29,22 +29,28 @@
 
     <gshell:plugin name="gshell-vfs">
         <gshell:command-bundle name="default">
-            <gshell:command name="cat">
-                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.CatAction"/>
+            <gshell:command name="cd">
+                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.ChangeDirectoryAction"/>
                 <gshell:completers>
                     <ref bean="fileObjectNameCompleter"/>
                     <null/>
                 </gshell:completers>
             </gshell:command>
 
-            <gshell:command name="cd">
-                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.ChangeDirectoryAction"/>
+            <gshell:command name="pwd" type="stateless">
+                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.CurrentDirectoryAction"/>
+            </gshell:command>
+
+            <gshell:command name="ls">
+                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.ListDirectoryAction"/>
                 <gshell:completers>
                     <ref bean="fileObjectNameCompleter"/>
                     <null/>
                 </gshell:completers>
             </gshell:command>
 
+            <gshell:link name="dir" target="ls"/>
+
             <gshell:command name="cp">
                 <gshell:action class="org.apache.geronimo.gshell.commands.vfs.CopyAction"/>
                 <gshell:completers>
@@ -55,30 +61,32 @@
             </gshell:command>
 
             <gshell:link name="copy" target="cp"/>
-
-            <gshell:command name="pwd" type="stateless">
-                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.CurrentDirectoryAction"/>
-            </gshell:command>
-
-            <gshell:command name="ls">
-                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.ListDirectoryAction"/>
+            
+            <gshell:command name="rm">
+                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.RemoveAction"/>
                 <gshell:completers>
                     <ref bean="fileObjectNameCompleter"/>
                     <null/>
                 </gshell:completers>
             </gshell:command>
 
-            <gshell:link name="dir" target="ls"/>
+            <gshell:link name="del" target="rm"/>
 
-            <gshell:command name="rm">
-                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.RemoveAction"/>
+            <gshell:command name="cat">
+                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.CatAction"/>
                 <gshell:completers>
                     <ref bean="fileObjectNameCompleter"/>
                     <null/>
                 </gshell:completers>
             </gshell:command>
 
-            <gshell:link name="del" target="rm"/>
+            <gshell:command name="edit">
+                <gshell:action class="org.apache.geronimo.gshell.commands.vfs.EditAction"/>
+                <gshell:completers>
+                    <ref bean="fileObjectNameCompleter"/>
+                    <null/>
+                </gshell:completers>
+            </gshell:command>
 
             <gshell:command name="touch">
                 <gshell:action class="org.apache.geronimo.gshell.commands.vfs.TouchAction"/>

Copied: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties
(from r700009, geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/CatAction.properties)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties?p2=geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties&p1=geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/CatAction.properties&r1=700009&r2=700087&rev=700087&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/CatAction.properties
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties
Mon Sep 29 05:17:33 2008
@@ -21,14 +21,15 @@
 ## $Rev$ $Date$
 ##
 
-command.name=cat
+command.name=edit
 
-command.description=Displays the contents of a file.
+command.description=Edit a file with an external editor.
 
-command.argument.path=Path to file to display
-command.argument.path.token=PATH
+command.option.editor=Path to external editor command
+command.option.editor.token=CMD
 
-command.option.displayLineNumbers=Number the output lines, starting at 1
+command.argument.path=Path to file to edit
+command.argument.path.token=PATH
 
 command.manual=\
-  TODO: cat manual
\ No newline at end of file
+  TODO: edit manual
\ No newline at end of file

Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties
------------------------------------------------------------------------------
    svn:mergeinfo = 

Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/EditAction.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/ListDirectoryAction.properties
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/ListDirectoryAction.properties?rev=700087&r1=700086&r2=700087&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/ListDirectoryAction.properties
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/ListDirectoryAction.properties
Mon Sep 29 05:17:33 2008
@@ -28,7 +28,7 @@
 command.argument.path=The file or directory path to list.
 command.argument.path.token=PATH
 
-command.option.includeHidden=Include directory entries whose names begin with a dot '.'
+command.option.includeHidden=Include hidden files
 
 command.option.recursive=List the contents of directories recursivly
 



Mime
View raw message