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

Use command arguments of type Map to support environment variables with dynamic names #134

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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 @@ -6,13 +6,15 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.g2forge.alexandria.annotations.note.Note;
import com.g2forge.alexandria.annotations.note.NoteType;
import com.g2forge.alexandria.command.invocation.CommandInvocation;
import com.g2forge.alexandria.command.invocation.environment.SystemEnvironment;
import com.g2forge.alexandria.command.invocation.environment.modified.EnvironmentValue;
import com.g2forge.alexandria.command.invocation.environment.modified.IEnvironmentModifier;
import com.g2forge.alexandria.command.invocation.environment.modified.ModifiedEnvironment;
import com.g2forge.alexandria.command.invocation.format.ICommandFormat;
Expand Down Expand Up @@ -135,14 +137,30 @@ public String modify(String parent) {
};
builder.add(ArgumentContext.class, Boolean.class, bool);
builder.add(ArgumentContext.class, Boolean.TYPE, bool);
builder.add(ArgumentContext.class, Map.class, (c, v) -> {
final Environment environment = c.getArgument().getMetadata().get(Environment.class);
if ((environment == null) || (environment.value() != null)) throw new IllegalArgumentException("Map arguments must be environemt with \"null\" name!");
final ModifiedEnvironment.ModifiedEnvironmentBuilder e = c.getEnvironment();
@SuppressWarnings("unchecked")
final Map<String, ?> map = (Map<String, ?>) v;
for (Map.Entry<String, ?> entry : map.entrySet()) {
final String key = entry.getKey();
final Object value = entry.getValue();
final IEnvironmentModifier modifier;
if (value instanceof IEnvironmentModifier) modifier = (IEnvironmentModifier) value;
else if (value instanceof String) modifier = new EnvironmentValue((String) value);
else throw new IllegalArgumentException("Arguments of type \"" + value.getClass() + "\" are not supported!");
e.modifier(key, modifier);
}
});
builder.fallback((c, v) -> {
if (v == null) {
final ISubject subject = c.getArgument().getMetadata();
if (subject.isPresent(Working.class)) return;
if (subject.isPresent(Environment.class)) return;
if (subject.isPresent(EnvPath.class)) return;
HDumbCommandConverter.set(c, c.getArgument(), null);
} else throw new IllegalArgumentException(String.format("Parameter %1$s cannot be converted to a command line argument because the type of \"2$s\" (%3$s) is unknown. Please consider implementing %4$s.", c.getArgument().getName(), v, v.getClass(), IArgumentRenderer.class.getSimpleName()));
} else throw new IllegalArgumentException(String.format("Parameter %1$s cannot be converted to a command line argument because the type of \"%2$s\" (%3$s) is unknown. Please consider implementing %4$s.", c.getArgument().getName(), v, v.getClass(), IArgumentRenderer.class.getSimpleName()));
});
}).build();

Expand Down
Loading