Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.InfoExe

package org.jboss.fresh.shell.commands;

import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.vfs.NoSuchFileException;
import org.jboss.fresh.vfs.VFS;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;

import java.util.Iterator;
import java.util.LinkedList;


// EX OK

public class InfoExe extends AbstractExecutable {
  private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(InfoExe.class);

  /**
   Displays file information of the files specified with props.
   */

  private static final String DIRECT = "-d";

  public void process(String exename, String[] params) throws Exception {
    log.debug("entered");

    if (helpRequested()) {
      PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
      out.println("Usage: info [--help] [-ex] [-d] files");
      out.println("    -d = direct (if links are not resolved)");
      out.println("    -ex = if the executable is allowed to throw an exception or just print an error message.");
      out.println("    --help : this help");
      out.close();
      log.debug("done");
      return;
    }

    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));

    VFS vfs = shell.getVFS();
    FileName pwd = new FileName(shell.getEnvProperty("PWD"));

    boolean direct = false;
    LinkedList paths = new LinkedList();

    for (int i = 0; i < params.length; i++) {
      String param = params[i];

      if (!direct && param.equals(DIRECT))
        direct = true;

      else {
        FileName filepath = new FileName(param);
        if (filepath.isRelative())
          filepath = pwd.absolutize(filepath);

//        filepath = vfs.resolve(shell.getUserCtx(), filepath, true);
        if (vfs.exists(shell.getUserCtx(), filepath, true)) {
          paths.add(filepath);
        } else {
          if (canThrowEx()) {
            throw new NoSuchFileException("Path does not exist: " + filepath);
          } else {
            out.println("Path does not exist: " + filepath);
          }
        }
      }
    }

    if (paths.isEmpty()) {
      if (canThrowEx()) {
        throw new Exception("No path specified.");
      } else {
        out.println("Usage: info [-d] FILES");
        out.println("       -d = direct (links are not resolved)");
      }
    } else {
      Iterator it = paths.iterator();
      while (it.hasNext()) {
        FileName file = (FileName) it.next();

        FileInfo info = vfs.getFileInfo(shell.getUserCtx(), file, direct);
        log.debug("Got info: " + info);

        out.println(info);
      }
    }

    out.close();

    log.debug("done");

  }
}
TOP

Related Classes of org.jboss.fresh.shell.commands.InfoExe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.