
br.com.objectos.rio.AbstractRioCommand Maven / Gradle / Ivy
The newest version!
/*
* AbstractRioCommand.java criado em 05/01/2014
*
* Propriedade de Objectos Fábrica de Software LTDA.
* Reprodução parcial ou total proibida.
*/
package br.com.objectos.rio;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import br.com.objectos.rio.core.os.Chmod;
import br.com.objectos.rio.core.os.ChmodAt;
import br.com.objectos.rio.core.os.Chown;
import br.com.objectos.rio.core.os.ChownAt;
import br.com.objectos.rio.core.os.Procs;
import br.com.objectos.rio.core.os.Tar;
import br.com.objectos.rio.core.os.TarAt;
import br.com.objectos.rio.core.os.UmountPoint;
import br.com.objectos.rio.core.os.Untar;
import br.com.objectos.rio.core.os.UntarFile;
import br.com.objectos.way.cli.Args;
import br.com.objectos.way.cli.Command;
import br.com.objectos.way.core.io.Directory;
import br.com.objectos.way.etc.WayEtc;
import br.com.objectos.way.etc.WayEtcResources;
import br.com.objectos.way.ssh.ScpUpload;
import br.com.objectos.way.ssh.SshBuilder;
import br.com.objectos.way.ssh.WaySSH;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.io.Files;
import com.google.common.io.Resources;
/**
* @author [email protected] (Marcio Endo)
*/
public abstract class AbstractRioCommand implements Command {
private O options;
private RioMessages messages;
@Override
public final void execute(Args args) {
options = newOptions();
args.parse(options);
initMessages();
executeCommand(options);
}
protected abstract String getCommandName();
protected abstract O newOptions();
protected abstract void executeCommand(O options);
// io stuff...
protected void copy(File from, File to) {
try {
Files.copy(from, to);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
protected ChmodAt chmod(Directory directory) {
return Chmod.at(directory);
}
protected ChownAt chown(Directory directory) {
return Chown.at(directory);
}
protected RioDownload download(HttpServer server) {
return new RioDownload(server);
}
protected RioInstall install(Directory source) {
return new RioInstall(source);
}
protected void mk2fs(String device) {
Procs.newCommand()
.add("mkfs.ext2")
.add(device)
.exec();
}
protected void mk4fs(String device) {
Procs.newCommand()
.add("mkfs.ext4")
.add(device)
.exec();
}
protected void mkswap(String device) {
Procs.newCommand()
.add("mkswap")
.add(device)
.exec();
}
protected RioMount mount(String source) {
return new RioMount(source);
}
protected WayEtcResources resourcesAt(String baseDir) {
return WayEtc.resourcesAt(baseDir);
}
protected List resourceToLines(String resourceName) {
try {
URL url = Resources.getResource(AbstractRioCommand.class, resourceName);
return Resources.readLines(url, Charsets.UTF_8);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
protected String resourceToPrintf(String resourceName) {
return resourceToString(resourceName)
.replace("\"", "\\\"")
.replace("$", "\\$");
}
protected String resourceToString(String resourceName) {
try {
URL url = Resources.getResource(AbstractRioCommand.class, resourceName);
return Resources.toString(url, Charsets.UTF_8);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
protected void swapon(String device) {
Procs.newCommand()
.add("swapon")
.add(device)
.exec();
}
protected ScpUpload scpFile(File file) {
return WaySSH.scp().file(file);
}
protected SshBuilder.HostBuider ssh(String host) {
return WaySSH.ssh()
.toHost(host);
}
protected TarAt tar(Directory dir) {
return Tar.changeTo(dir);
}
protected void umount(Directory target) {
UmountPoint.at(target).exec();
}
protected UntarFile untar(File file) {
return Untar.file(file);
}
// messages stuff...
protected void initMessages() {
String commandName = getCommandName();
messages = new RioMessages(commandName);
}
protected void infoAction(String actionName) {
messages.infoAction(actionName);
}
protected void info() {
messages.info();
}
protected void info(String info) {
messages.info(info);
}
protected void info(String format, Object... args) {
messages.info(format, args);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy