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

org.testingisdocumenting.webtau.cli.CliDocumentation Maven / Gradle / Ivy

There is a newer version: 2.4
Show newest version
/*
 * Copyright 2020 webtau maintainers
 * Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
 *
 * 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 org.testingisdocumenting.webtau.cli;

import org.testingisdocumenting.webtau.documentation.DocumentationArtifacts;
import org.testingisdocumenting.webtau.documentation.DocumentationArtifactsLocation;
import org.testingisdocumenting.webtau.utils.FileUtils;

import java.nio.file.Path;
import java.util.List;

public class CliDocumentation {
    public void capture(String artifactName) {
        Capture capture = new Capture(artifactName);
        capture.capture();
    }

    private static class Capture {
        private final Path path;
        private final CliDocumentationArtifact documentationArtifact;

        public Capture(String artifactName) {
            DocumentationArtifacts.registerName(artifactName);

            this.path = DocumentationArtifactsLocation.resolve(artifactName);

            this.documentationArtifact = Cli.cli.getLastDocumentationArtifact();
            if (this.documentationArtifact == null) {
                throw new IllegalStateException("no cli calls were made yet");
            }
        }

        public void capture() {
            captureCommand();
            captureOut();
            captureErr();
            captureOutMatchedLines();
            captureErrMatchedLines();
            captureExitCode();
        }

        private void captureCommand() {
            FileUtils.writeTextContent(path.resolve("command.txt"), documentationArtifact.getFullCommand());

        }

        private void captureOut() {
            capture("out.txt", documentationArtifact.getOutput());
        }

        private void captureErr() {
            capture("err.txt", documentationArtifact.getError());
        }

        private void capture(String fileName, CliOutput output) {
            String out = output.get();
            if (!out.isEmpty()) {
                FileUtils.writeTextContent(path.resolve(fileName), out);
            }
        }

        private void captureOutMatchedLines() {
            captureMatched("out.matched.txt", documentationArtifact.getOutput());
        }

        private void captureErrMatchedLines() {
            captureMatched("err.matched.txt", documentationArtifact.getError());
        }

        private void captureMatched(String fileName, CliOutput output) {
            List lines = output.extractMatchedLines();
            if (!lines.isEmpty()) {
                FileUtils.writeTextContent(path.resolve(fileName), String.join("\n", lines));
            }
        }

        private void captureExitCode() {
            if (documentationArtifact.getExitCode() == null) {
                return;
            }

            FileUtils.writeTextContent(path.resolve("exitcode.txt"),
                    String.valueOf(documentationArtifact.getExitCode().get()));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy