/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.rio;
import static com.google.common.collect.Lists.newArrayList;
import java.util.List;
import br.com.objectos.rio.core.os.Chroot;
import br.com.objectos.rio.core.os.ChrootMount;
import br.com.objectos.rio.core.os.ChrootUmount;
import br.com.objectos.way.base.io.Directory;
import br.com.objectos.way.base.io.Stdout;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class GentooChrootBuilder {
private final Directory mountDir;
private final List<String> commands = newArrayList();
public GentooChrootBuilder(Directory mountDir) {
this.mountDir = mountDir;
}
public GentooChrootBuilder add(String command) {
commands.add(command);
return this;
}
public GentooChrootBuilder add(String template, Object... args) {
return add(String.format(template, args));
}
public GentooChrootBuilder addAll(List<String> commands) {
this.commands.addAll(commands);
return this;
}
public AddAllIf addAllIf(List<String> commands) {
return new AddAllIf(commands);
}
public void exec() {
ChrootMount mount = ChrootMount.at(mountDir).mount();
try {
Chroot chroot = Chroot.at(mountDir).addAll(commands).exec();
Stdout.print(chroot);
} finally {
ChrootUmount umount = mount.umount();
Stdout.print(umount);
}
}
public class AddAllIf {
private final List<String> _cmds;
public AddAllIf(List<String> cmds) {
this._cmds = cmds;
}
public GentooChrootBuilder isNotNull(Object val) {
return isTrue(val != null);
}
public GentooChrootBuilder isTrue(boolean val) {
if (val) {
commands.addAll(_cmds);
}
return GentooChrootBuilder.this;
}
}
}