All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.ext.shell.command.impl.CommandBuilderImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR1
Show newest version
/*
 * Copyright 2015 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 *
 *
 * Copyright (c) 2015 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 *
 */

package io.vertx.ext.shell.command.impl;

import io.vertx.core.Context;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.cli.CLI;
import io.vertx.ext.shell.cli.CliToken;
import io.vertx.ext.shell.cli.Completion;
import io.vertx.ext.shell.command.Command;
import io.vertx.ext.shell.command.CommandBuilder;
import io.vertx.ext.shell.command.CommandProcess;
import io.vertx.ext.shell.system.Process;

import java.util.Collections;
import java.util.List;

/**
 * @author Julien Viet
 */
public class CommandBuilderImpl implements CommandBuilder {

  final String name;
  final CLI cli;
  public Handler processHandler;
  public Handler completeHandler;

  public CommandBuilderImpl(String name, CLI cli) {
    this.name = name;
    this.cli = cli;
  }

  @Override
  public CommandBuilderImpl processHandler(Handler handler) {
    processHandler = handler;
    return this;
  }

  @Override
  public CommandBuilderImpl completionHandler(Handler handler) {
    completeHandler = handler;
    return this;
  }

  @Override
  public Command build(Vertx vertx) {
    Context context = vertx.getOrCreateContext();
    return new Command() {
      @Override
      public String name() {
        return name;
      }
      @Override
      public CLI cli() {
        return cli;
      }

      @Override
      public Process createProcess(List args) {
        return new ProcessImpl(vertx, context, this, args, processHandler);
      }

      @Override
      public void complete(Completion completion) {
        if (completeHandler != null) {
          context.runOnContext(v -> {
            try {
              completeHandler.handle(completion);
            } catch (Throwable t) {
              completion.complete(Collections.emptyList());
              throw t;
            }
          });
        } else {
          Command.super.complete(completion);
        }
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy