Package org.jboss.fresh.shell.commands

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

package org.jboss.fresh.shell.commands;

import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.vfs.VFS;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.OutBuffer;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.shell.Shell;

import java.io.PrintWriter;

// EX OK

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

  // cd and a param
  // we find out current dir
  // we absolutize param[0] on it
  // we check if it exists
  // if it does not exist we display an error

  /**
   Changes the current working directory.
   */

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

    if (helpRequested()) {
      PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
      out.print("Usage: cd [--help] DIRECTORY\n");
      out.print("    DIRECTORY : the subdirectory to be set as the current working directory\n");
      out.print("    --help : this help\n");
      out.close();
      log.debug("done");
      return;
    }

    OutBuffer out = getStdOut();
    if (params == null || params.length == 0) {
      out.put(" cp: No directory specified.\n", 10000L);
      log.debug("done");
      return;
    }

    Shell sh = getShell();
    VFS vfs = sh.getVFS();
    String to = params[0];
    FileName fname = new FileName(to);
    if (fname.isRelative())
      fname = new FileName(sh.getEnvProperty("PWD")).absolutize(fname);

//    System.out.println("****");
//    System.out.println("****  fname: " + fname);
//    System.out.println("****");
//    if(vfs.exists(null, fname, false)) {
//      sh._setPWD(fname.toString());
//    }


    FileInfo info = vfs.getFileInfo(null, fname, false);
    if (info == null || !info.isDirectory()) {
      if (canThrowEx()) {
        throw new RuntimeException("Can't find the path specified: " + fname + "\n\n");
      } else {
        out.put("Can't find the path specified: " + fname + "\n\n", 10000L);
      }
    } else {
      sh._setPWD(fname.toString());
    }

    log.debug("done");
  }
}
TOP

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

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.