001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.cli.processor.command;
007
008import gov.nist.secauto.metaschema.core.util.ObjectUtils;
009import gov.nist.secauto.metaschema.core.util.UriUtils;
010
011import java.net.URI;
012import java.net.URISyntaxException;
013import java.nio.file.Path;
014import java.nio.file.Paths;
015import java.util.Collection;
016import java.util.Collections;
017
018import edu.umd.cs.findbugs.annotations.NonNull;
019import nl.talsmasoftware.lazy4j.Lazy;
020
021public abstract class AbstractTerminalCommand implements ICommand {
022  private static Lazy<Path> currentWorkingDirectory = Lazy.lazy(() -> Paths.get("").toAbsolutePath());
023
024  @SuppressWarnings("null")
025  @Override
026  public Collection<ICommand> getSubCommands() {
027    return Collections.emptyList();
028  }
029
030  @Override
031  public boolean isSubCommandRequired() {
032    return false;
033  }
034
035  @NonNull
036  protected static Path getCurrentWorkingDirectory() {
037    return ObjectUtils.notNull(currentWorkingDirectory.get());
038  }
039
040  @NonNull
041  protected static Path resolveAgainstCWD(@NonNull Path path) {
042    return ObjectUtils.notNull(getCurrentWorkingDirectory().resolve(path).normalize());
043  }
044
045  @NonNull
046  protected static URI resolveAgainstCWD(@NonNull URI uri) {
047    return ObjectUtils.notNull(getCurrentWorkingDirectory().toUri().resolve(uri.normalize()));
048  }
049
050  @NonNull
051  protected static URI resolveAgainstCWD(@NonNull String uri) throws URISyntaxException {
052    return UriUtils.toUri(uri, ObjectUtils.notNull(getCurrentWorkingDirectory().toUri()));
053  }
054}