001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.tools.command;
020
021import java.time.Duration;
022
023import org.apache.commons.cli.CommandLine;
024import org.apache.commons.cli.Option;
025import org.apache.commons.cli.Options;
026import org.nuxeo.lib.stream.computation.Record;
027import org.nuxeo.lib.stream.log.LogManager;
028import org.nuxeo.lib.stream.log.LogRecord;
029import org.nuxeo.lib.stream.log.LogTailer;
030import org.nuxeo.lib.stream.tools.renderer.Renderer;
031
032/**
033 * @since 9.3
034 */
035public class CatCommand extends Command {
036
037    protected static final String NAME = "cat";
038
039    @Override
040    public String name() {
041        return NAME;
042    }
043
044    @Override
045    public void updateOptions(Options options) {
046        options.addOption(
047                Option.builder("n").longOpt("lines").desc("Render the first N records").hasArg().argName("N").build());
048        options.addOption(Option.builder("l")
049                                .longOpt("log-name")
050                                .desc("Log name")
051                                .required()
052                                .hasArg()
053                                .argName("LOG_NAME")
054                                .build());
055        options.addOption(
056                Option.builder("g").longOpt("group").desc("Consumer group").hasArg().argName("GROUP").build());
057        options.addOption(
058                Option.builder().longOpt("render").desc("Output rendering").hasArg().argName("FORMAT").build());
059
060    }
061
062    @Override
063    public boolean run(LogManager manager, CommandLine cmd) throws InterruptedException {
064        int limit = Integer.parseInt(cmd.getOptionValue("lines", "-1"));
065        String name = cmd.getOptionValue("log-name");
066        String render = cmd.getOptionValue("render", "default");
067        String group = cmd.getOptionValue("group", "tools");
068        cat(manager, name, group, limit, getRecordRenderer(render));
069        return true;
070    }
071
072    protected void cat(LogManager manager, String name, String group, int limit, Renderer render)
073            throws InterruptedException {
074        render.header();
075        try (LogTailer<Record> tailer = manager.createTailer(group, name)) {
076            int count = 0;
077            do {
078                LogRecord<Record> record = tailer.read(Duration.ofMillis(1000));
079                if (record == null) {
080                    break;
081                }
082                count++;
083                render.accept(record);
084            } while (limit < 0 || (count < limit));
085        }
086        render.footer();
087    }
088}