cat, ls: move inside our main program package
I've never liked having these be special cases. Lets move them to the pgm package where they aren't special anymore. Change-Id: Ib76fd6c7fe806b92ee5658ece4c788e67bcacdbc Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
parent
f0b848506f
commit
dc5d1ee12d
|
@ -69,42 +69,21 @@ public final class GerritLauncher {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Special cases, a few global options actually are programs.
|
||||
//
|
||||
if ("-v".equals(argv[0]) || "--version".equals(argv[0])) {
|
||||
// Special case, jump into the "Version" command which is
|
||||
// compiled somewhere in our library packages.
|
||||
//
|
||||
argv[0] = "Version";
|
||||
argv[0] = "version";
|
||||
} else if ("-p".equals(argv[0]) || "--cat".equals(argv[0])) {
|
||||
argv[0] = "cat";
|
||||
} else if ("-l".equals(argv[0]) || "--ls".equals(argv[0])) {
|
||||
argv[0] = "ls";
|
||||
}
|
||||
|
||||
final String cmd = argv[0];
|
||||
if ("cat".equals(cmd) || "-p".equals(cmd) || "--cat".equals(cmd)) {
|
||||
// Copy the contents of a file to System.out
|
||||
//
|
||||
if (argv.length == 2) {
|
||||
return cat(argv[1]);
|
||||
} else {
|
||||
System.err.println("usage: cat FILE");
|
||||
return 1;
|
||||
}
|
||||
|
||||
} else if ("ls".equals(cmd) || "-l".equals(cmd) || "--ls".equals(cmd)) {
|
||||
// List the available files under WEB-INF/
|
||||
//
|
||||
if (argv.length == 1) {
|
||||
ls();
|
||||
return 0;
|
||||
} else {
|
||||
System.err.println("usage: ls");
|
||||
return 1;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Run an arbitrary application class
|
||||
//
|
||||
final ClassLoader cl = libClassLoader();
|
||||
Thread.currentThread().setContextClassLoader(cl);
|
||||
return invokeProgram(cl, argv);
|
||||
}
|
||||
// Run the application class
|
||||
//
|
||||
final ClassLoader cl = libClassLoader();
|
||||
Thread.currentThread().setContextClassLoader(cl);
|
||||
return invokeProgram(cl, argv);
|
||||
}
|
||||
|
||||
private static String getVersion(final File me) {
|
||||
|
@ -127,67 +106,6 @@ public final class GerritLauncher {
|
|||
}
|
||||
}
|
||||
|
||||
private static int cat(String fileName) throws IOException {
|
||||
while (fileName.startsWith("/")) {
|
||||
fileName = fileName.substring(1);
|
||||
}
|
||||
|
||||
String name;
|
||||
if (fileName.equals("LICENSES.txt")) {
|
||||
name = fileName;
|
||||
} else {
|
||||
name = "WEB-INF/" + fileName;
|
||||
}
|
||||
|
||||
final InputStream in = GerritLauncher.class.getResourceAsStream(name);
|
||||
if (in == null) {
|
||||
System.err.println("error: no such file " + fileName);
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
final byte[] buf = new byte[4096];
|
||||
int n;
|
||||
while ((n = in.read(buf)) >= 0) {
|
||||
System.out.write(buf, 0, n);
|
||||
}
|
||||
} finally {
|
||||
System.out.flush();
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void ls() throws IOException {
|
||||
final ZipFile zf = new ZipFile(getDistributionArchive());
|
||||
try {
|
||||
final Enumeration<? extends ZipEntry> e = zf.entries();
|
||||
while (e.hasMoreElements()) {
|
||||
final ZipEntry ze = e.nextElement();
|
||||
String name = ze.getName();
|
||||
boolean show = false;
|
||||
show |= name.startsWith("WEB-INF/");
|
||||
show |= name.equals("LICENSES.txt");
|
||||
|
||||
show &= !ze.isDirectory();
|
||||
show &= !name.startsWith("WEB-INF/classes/");
|
||||
show &= !name.startsWith("WEB-INF/lib/");
|
||||
show &= !name.equals("WEB-INF/web.xml");
|
||||
if (show) {
|
||||
if (name.startsWith("WEB-INF/")) {
|
||||
name = name.substring("WEB-INF/".length());
|
||||
}
|
||||
System.out.println(name);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
zf.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static int invokeProgram(final ClassLoader loader,
|
||||
final String[] origArgv) throws Exception {
|
||||
String name = origArgv[0];
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.pgm;
|
||||
|
||||
import com.google.gerrit.main.GerritLauncher;
|
||||
import com.google.gerrit.pgm.util.AbstractProgram;
|
||||
|
||||
import org.kohsuke.args4j.Argument;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/** Dump the contents of a file in our archive. */
|
||||
public class Cat extends AbstractProgram {
|
||||
@Argument(index = 0, required = true, metaVar = "FILE", usage = "file to output")
|
||||
private String fileName;
|
||||
|
||||
@Override
|
||||
public int run() throws IOException {
|
||||
while (fileName.startsWith("/")) {
|
||||
fileName = fileName.substring(1);
|
||||
}
|
||||
|
||||
String name;
|
||||
if (fileName.equals("LICENSES.txt")) {
|
||||
name = fileName;
|
||||
} else {
|
||||
name = "WEB-INF/" + fileName;
|
||||
}
|
||||
|
||||
final InputStream in = open(name);
|
||||
if (in == null) {
|
||||
System.err.println("error: no such file " + fileName);
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
final byte[] buf = new byte[4096];
|
||||
int n;
|
||||
while ((n = in.read(buf)) >= 0) {
|
||||
System.out.write(buf, 0, n);
|
||||
}
|
||||
} finally {
|
||||
System.out.flush();
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private InputStream open(String name) {
|
||||
return GerritLauncher.class.getClassLoader().getResourceAsStream(name);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.pgm;
|
||||
|
||||
import com.google.gerrit.main.GerritLauncher;
|
||||
import com.google.gerrit.pgm.util.AbstractProgram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
/** List the files available in our archive. */
|
||||
public class Ls extends AbstractProgram {
|
||||
@Override
|
||||
public int run() throws IOException {
|
||||
final ZipFile zf = new ZipFile(GerritLauncher.getDistributionArchive());
|
||||
try {
|
||||
final Enumeration<? extends ZipEntry> e = zf.entries();
|
||||
while (e.hasMoreElements()) {
|
||||
final ZipEntry ze = e.nextElement();
|
||||
String name = ze.getName();
|
||||
boolean show = false;
|
||||
show |= name.startsWith("WEB-INF/");
|
||||
show |= name.equals("LICENSES.txt");
|
||||
|
||||
show &= !ze.isDirectory();
|
||||
show &= !name.startsWith("WEB-INF/classes/");
|
||||
show &= !name.startsWith("WEB-INF/lib/");
|
||||
show &= !name.equals("WEB-INF/web.xml");
|
||||
if (show) {
|
||||
if (name.startsWith("WEB-INF/")) {
|
||||
name = name.substring("WEB-INF/".length());
|
||||
}
|
||||
System.out.println(name);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
zf.close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue