Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oscal-cli resolve --relative-to=URI #88

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.core.util.UriUtils;
import gov.nist.secauto.metaschema.databind.IBindingContext;
import gov.nist.secauto.metaschema.databind.io.DeserializationFeature;
import gov.nist.secauto.metaschema.databind.io.Format;
Expand All @@ -33,6 +34,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
Expand All @@ -48,12 +50,19 @@ public abstract class AbstractResolveCommand
private static final List<ExtraArgument> EXTRA_ARGUMENTS = ObjectUtils.notNull(List.of(
ExtraArgument.newInstance("URI to resolve", true),
ExtraArgument.newInstance("destination file", false)));
private static final Option RELATIVE_TO = Option.builder()
.longOpt("relative-to")
.desc("Generate URI references relative to this resource")
.hasArg()
.build();

@NonNull
private static final List<Option> OPTIONS = ObjectUtils.notNull(
List.of(
MetaschemaCommands.AS_FORMAT_OPTION,
MetaschemaCommands.TO_OPTION,
MetaschemaCommands.OVERWRITE_OPTION));
MetaschemaCommands.OVERWRITE_OPTION,
RELATIVE_TO));

@Override
public String getDescription() {
Expand Down Expand Up @@ -153,10 +162,25 @@ protected void executeCommand(
destination = MetaschemaCommands.handleDestination(ObjectUtils.requireNonNull(extraArgs.get(1)), cmdLine);
}

URI relativeTo;
if (cmdLine.hasOption(RELATIVE_TO)) {
relativeTo = getCurrentWorkingDirectory().toUri().resolve(cmdLine.getOptionValue(RELATIVE_TO));
} else {
relativeTo = document.getDocumentUri();
}

// this is a profile
DynamicContext dynamicContext = new DynamicContext(document.getStaticContext());
dynamicContext.setDocumentLoader(loader);
ProfileResolver resolver = new ProfileResolver(dynamicContext);
ProfileResolver resolver = new ProfileResolver(
dynamicContext,
(uri, src) -> {
try {
return UriUtils.relativize(relativeTo, src.resolve(uri), true);
} catch (URISyntaxException ex) {
throw new IllegalArgumentException(ex);
}
});

IDocumentNodeItem resolvedProfile;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String getName() {

@Override
public String getDescription() {
return "Generate a diagram for the provided Metaschema module";
return "List allowed values constraints for the provided Metaschema module";
}

@SuppressWarnings("null")
Expand Down