|
1 | 1 | package io.github.easy4j.opencli.capability; |
2 | 2 |
|
3 | 3 | import io.github.easy4j.opencli.OpenCliExecutionTarget; |
| 4 | +import io.github.easy4j.opencli.OpenCliInvocationContext; |
4 | 5 | import io.github.easy4j.opencli.OpenCliProperties; |
5 | 6 | import io.github.easy4j.opencli.capability.OpenCliCapabilitySnapshot.Status; |
6 | 7 | import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason; |
|
10 | 11 | import io.github.easy4j.opencli.exception.OpenCliTimeoutException; |
11 | 12 | import io.github.easy4j.opencli.parser.OpenCliHelpParser; |
12 | 13 | import io.github.easy4j.opencli.parser.OpenCliHelpResult; |
| 14 | +import io.github.easy4j.opencli.parser.OpenCliHelpSchema.Argument; |
13 | 15 | import io.github.easy4j.opencli.util.OpenCliStrings; |
14 | 16 | import java.io.IOException; |
15 | 17 | import java.nio.ByteBuffer; |
@@ -89,22 +91,63 @@ public OpenCliCapabilitySnapshot refresh(OpenCliHelpScope scope) { |
89 | 91 | */ |
90 | 92 | public OpenCliResult invoke(OpenCliHelpScope scope, String command, String session, |
91 | 93 | List<String> positionals, Map<String, String> options) { |
| 94 | + return invoke(scope, command, session, positionals, options, null); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Executes a local leaf with immutable invocation overrides. |
| 99 | + * @param scope verified query containing the leaf |
| 100 | + * @param command exact leaf display path |
| 101 | + * @param session explicit browser session, or null |
| 102 | + * @param positionals literal arguments in schema order, or null |
| 103 | + * @param options declared flags and values, or null |
| 104 | + * @param context invocation overrides, or null |
| 105 | + * @return local execution result |
| 106 | + */ |
| 107 | + public OpenCliResult invoke(OpenCliHelpScope scope, String command, String session, |
| 108 | + List<String> positionals, Map<String, String> options, OpenCliInvocationContext context) { |
92 | 109 | OpenCliProperties configured = executor.getProperties(); |
93 | 110 | if (configured.getExecutionTarget() != OpenCliExecutionTarget.LOCAL_PROCESS) { |
94 | 111 | throw new UnsupportedOperationException("Discovered command execution requires a local process"); |
95 | 112 | } |
96 | 113 | OpenCliProperties local = configured.copyForLocalCliExecution(); |
| 114 | + Map<String, String> effectiveOptions = |
| 115 | + OpenCliInvocationContext.mergeOptions(local.getInvocationDefaults(), context, options); |
97 | 116 | OpenCliCapabilitySnapshot snapshot = refresh(scope, local); |
98 | 117 | if (snapshot.getStatus() == Status.AVAILABLE && !VERIFIED_HELP_VERSION.equals(snapshot.getObservedVersion())) { |
99 | 118 | throw new UnsupportedOperationException("Discovered command execution requires the verified help version"); |
100 | 119 | } |
101 | | - List<String> argv = OpenCliCapabilityCommand.bind(snapshot, command, session, positionals, options); |
| 120 | + List<Argument> globals = verifiedAdapterGlobals(snapshot, local, effectiveOptions); |
| 121 | + List<String> argv = OpenCliCapabilityCommand.bind(snapshot, command, session, positionals, effectiveOptions, globals); |
102 | 122 | local.setExecutable(snapshot.getExecutable()); |
103 | 123 | try (OpenCliExecutor invocation = new OpenCliExecutor(local, executor.getProcessRuntime())) { |
104 | 124 | return invocation.invokeLocalOnly(argv); |
105 | 125 | } |
106 | 126 | } |
107 | 127 |
|
| 128 | + private List<Argument> verifiedAdapterGlobals(OpenCliCapabilitySnapshot snapshot, OpenCliProperties local, |
| 129 | + Map<String, String> options) { |
| 130 | + if (snapshot.getStatus() != Status.AVAILABLE || snapshot.getHelpScope() != OpenCliHelpScope.SITE_ZHIHU_HOT |
| 131 | + || !options.containsKey("--profile") || !Boolean.TRUE.equals(snapshot.getHelp().getSchema().getBrowser())) { |
| 132 | + return Collections.emptyList(); |
| 133 | + } |
| 134 | + if (snapshot.getHelp().getSchema().getGlobalOptions().stream().anyMatch(OpenCliCapabilityDiscovery::isProfileFlag)) { |
| 135 | + return Collections.emptyList(); |
| 136 | + } |
| 137 | + OpenCliCapabilitySnapshot metadata = refresh(OpenCliHelpScope.META_PROFILE, local); |
| 138 | + if (metadata.getStatus() != Status.AVAILABLE) { |
| 139 | + throw new IllegalStateException("Current global profile help is unavailable"); |
| 140 | + } |
| 141 | + Argument profile = metadata.getHelp().getSchema().getGlobalOptions().stream() |
| 142 | + .filter(OpenCliCapabilityDiscovery::isProfileFlag) |
| 143 | + .findFirst().orElseThrow(() -> new IllegalStateException("Current global profile flag is unverified")); |
| 144 | + return Collections.singletonList(profile); |
| 145 | + } |
| 146 | + |
| 147 | + private static boolean isProfileFlag(Argument argument) { |
| 148 | + return "profile".equals(argument.getName()) && "--profile <name>".equals(argument.getFlags()); |
| 149 | + } |
| 150 | + |
108 | 151 | private OpenCliCapabilitySnapshot refresh(OpenCliHelpScope scope, OpenCliProperties configured) { |
109 | 152 | Objects.requireNonNull(scope, "scope"); |
110 | 153 | long attempt; |
|
0 commit comments