Search in sources :

Example 1 with CliHandler

use of act.handler.CliHandler in project actframework by actframework.

the class Help method list.

private void list(String search, String label, String fmt, List<String> commands, CliDispatcher dispatcher, CliContext context) {
    List<String> lines = new ArrayList<>();
    boolean noSearch = S.blank(search);
    if (noSearch) {
        lines.add(label.toUpperCase());
        lines.add("");
    }
    for (String cmd : commands) {
        CliHandler handler = dispatcher.handler(cmd);
        T2<String, String> commandLine = handler.commandLine();
        if (noSearch || commandLine._1.contains(search)) {
            lines.add(S.fmt(fmt, cmd, commandLine._2));
        }
    }
    context.println(Ansi.ansi().render(S.join("\n", lines)).toString());
}
Also used : ArrayList(java.util.ArrayList) CliHandler(act.handler.CliHandler)

Example 2 with CliHandler

use of act.handler.CliHandler in project actframework by actframework.

the class CliDispatcher method commands.

/**
 * Returns all commands in alphabetic order
 *
 * @return the list of commands
 */
public List<String> commands(boolean sys, boolean app) {
    C.List<String> list = C.newList();
    Act.Mode mode = Act.mode();
    boolean all = !sys && !app;
    for (String s : registry.keySet()) {
        boolean isSysCmd = s.startsWith("act.");
        if (isSysCmd && !sys && !all) {
            continue;
        }
        if (!isSysCmd && !app && !all) {
            continue;
        }
        CliHandler h = registry.get(s);
        if (h.appliedIn(mode)) {
            list.add(s);
        }
    }
    return list.sorted(new Osgl.Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            boolean b1 = (o1.startsWith("act."));
            boolean b2 = (o2.startsWith("act."));
            if (b1 & !b2) {
                return -1;
            }
            if (!b1 & b2) {
                return 1;
            }
            return o1.compareTo(o2);
        }
    });
}
Also used : C(org.osgl.util.C) Act(act.Act) CliHandler(act.handler.CliHandler) Osgl(org.osgl.Osgl)

Example 3 with CliHandler

use of act.handler.CliHandler in project actframework by actframework.

the class CommanderByteCodeScannerTest method instanceWithReturnValue.

@Test
public void instanceWithReturnValue() {
    scan(InstanceWithReturnType.class);
    CliHandler handler = dispatcher.handler("user.list");
    assertNotNull(handler);
    CommanderClassMetaInfo classMetaInfo = infoSrc.commanderMetaInfo(InstanceWithReturnType.class.getName());
    CommandMethodMetaInfo methodMetaInfo = classMetaInfo.command("user.list");
    eq("getUserList", methodMetaInfo.methodName());
    eq(InstanceWithReturnType.class.getName() + ".getUserList", methodMetaInfo.fullName());
    no(methodMetaInfo.isStatic());
    PropertySpec.MetaInfo dataView = methodMetaInfo.propertySpec();
    ceq(C.list("fn", "ln"), dataView.outputFields());
    eq(Type.getType(List.class), methodMetaInfo.returnType());
    C.List<CommandParamMetaInfo> params = methodMetaInfo.params();
    eq(4, params.size());
    CommandParamMetaInfo op = params.get(0);
    eq("id", op.name());
    ParamOptionAnnoInfo anno = op.optionInfo();
    eq("-i", anno.lead1());
    eq("--id", anno.lead2());
    eq("group1", anno.group());
    assertNull(anno.defVal());
    op = params.get(1);
    eq("b", op.name());
    assertNull(op.optionInfo());
    op = params.get(2);
    eq("limit", op.name());
    anno = op.optionInfo();
    eq("-l", anno.lead1());
    eq("--limit", anno.lead2());
    assertNull(anno.group());
    eq("-1", anno.defVal());
    op = params.get(3);
    eq("l", op.name());
    assertNull(op.optionInfo());
}
Also used : PropertySpec(act.util.PropertySpec) C(org.osgl.util.C) List(java.util.List) CliHandler(act.handler.CliHandler) InstanceWithReturnType(testapp.cli.InstanceWithReturnType) Test(org.junit.Test)

Example 4 with CliHandler

use of act.handler.CliHandler in project actframework by actframework.

the class CliOverHttp method handler.

private CliHandler handler(String cmd) {
    CliHandler handler = dispatcher.handler(cmd);
    notFoundIfNull(handler);
    return handler;
}
Also used : CliHandler(act.handler.CliHandler)

Example 5 with CliHandler

use of act.handler.CliHandler in project actframework by actframework.

the class CliOverHttp method run.

@PostAction("cmd")
public Result run(String cmd, ActionContext context) throws IOException {
    CliHandler handler = handler(cmd);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    CliContext cliContext = new CliOverHttpContext(context, os);
    handler.handle(cliContext);
    cliContext.flush();
    String txt = new String(os.toByteArray(), "UTF-8");
    txt = txt.replace("^J", "\n");
    return text(txt);
}
Also used : CliOverHttpContext(act.cli.CliOverHttpContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CliHandler(act.handler.CliHandler) CliContext(act.cli.CliContext) PostAction(org.osgl.mvc.annotation.PostAction)

Aggregations

CliHandler (act.handler.CliHandler)7 Act (act.Act)2 ArrayList (java.util.ArrayList)2 C (org.osgl.util.C)2 CliContext (act.cli.CliContext)1 CliDispatcher (act.cli.CliDispatcher)1 CliOverHttpContext (act.cli.CliOverHttpContext)1 PropertySpec (act.util.PropertySpec)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 List (java.util.List)1 Test (org.junit.Test)1 Osgl (org.osgl.Osgl)1 T2 (org.osgl.Osgl.T2)1 PostAction (org.osgl.mvc.annotation.PostAction)1 InstanceWithReturnType (testapp.cli.InstanceWithReturnType)1