|
18 | 18 | */ |
19 | 19 | package org.apache.maven.cling.invoker.mvnenc.goals; |
20 | 20 |
|
21 | | -import java.io.IOError; |
22 | | -import java.io.InterruptedIOException; |
23 | | -import java.util.HashMap; |
24 | 21 | import java.util.List; |
25 | 22 | import java.util.Map; |
26 | 23 | import java.util.Objects; |
|
42 | 39 | import org.jline.consoleui.prompt.PromptResultItemIF; |
43 | 40 | import org.jline.consoleui.prompt.builder.ListPromptBuilder; |
44 | 41 | import org.jline.consoleui.prompt.builder.PromptBuilder; |
45 | | -import org.jline.reader.UserInterruptException; |
46 | 42 | import org.jline.utils.Colors; |
47 | 43 | import org.jline.utils.OSUtils; |
48 | 44 |
|
@@ -94,122 +90,111 @@ public int doExecute(EncryptContext context) throws Exception { |
94 | 90 | config.setDefaultDispatcher(null); |
95 | 91 | config.getConfigurations().clear(); |
96 | 92 |
|
97 | | - try (ConsolePrompt prompt = new ConsolePrompt(context.reader, context.terminal, promptConfig)) { |
98 | | - Map<String, PromptResultItemIF> dispatcherResult = new HashMap<>(); |
99 | | - Map<String, PromptResultItemIF> dispatcherConfigResult = new HashMap<>(); |
100 | | - Map<String, PromptResultItemIF> confirmChoice = new HashMap<>(); |
| 93 | + Map<String, PromptResultItemIF> dispatcherResult; |
| 94 | + Map<String, PromptResultItemIF> dispatcherConfigResult; |
| 95 | + Map<String, PromptResultItemIF> confirmChoice; |
| 96 | + ConsolePrompt prompt = new ConsolePrompt(context.reader, context.terminal, promptConfig); |
101 | 97 |
|
102 | | - prompt.prompt( |
103 | | - context.header, dispatcherPrompt(prompt.getPromptBuilder()).build(), dispatcherResult); |
104 | | - if (dispatcherResult.isEmpty()) { |
105 | | - throw new InterruptedException(); |
106 | | - } |
107 | | - if (NONE.equals(dispatcherResult.get("defaultDispatcher").getResult())) { |
108 | | - context.terminal |
109 | | - .writer() |
110 | | - .println(messageBuilderFactory |
111 | | - .builder() |
112 | | - .warning( |
113 | | - "Maven4 SecDispatcher disabled; Maven3 fallback may still work, use `mvnenc diag` to check") |
114 | | - .build()); |
115 | | - } else { |
116 | | - config.setDefaultDispatcher( |
117 | | - dispatcherResult.get("defaultDispatcher").getResult()); |
| 98 | + dispatcherResult = prompt.prompt( |
| 99 | + context.header, dispatcherPrompt(prompt.getPromptBuilder()).build()); |
| 100 | + if (dispatcherResult.isEmpty()) { |
| 101 | + throw new InterruptedException(); |
| 102 | + } |
| 103 | + if (NONE.equals(dispatcherResult.get("defaultDispatcher").getResult())) { |
| 104 | + context.terminal |
| 105 | + .writer() |
| 106 | + .println(messageBuilderFactory |
| 107 | + .builder() |
| 108 | + .warning( |
| 109 | + "Maven4 SecDispatcher disabled; Maven3 fallback may still work, use `mvnenc diag` to check") |
| 110 | + .build()); |
| 111 | + } else { |
| 112 | + config.setDefaultDispatcher( |
| 113 | + dispatcherResult.get("defaultDispatcher").getResult()); |
118 | 114 |
|
119 | | - DispatcherMeta meta = secDispatcher.availableDispatchers().stream() |
120 | | - .filter(d -> Objects.equals(config.getDefaultDispatcher(), d.name())) |
121 | | - .findFirst() |
122 | | - .orElseThrow(); |
123 | | - if (!meta.fields().isEmpty()) { |
124 | | - prompt.prompt( |
125 | | - context.header, |
126 | | - configureDispatcher(context, meta, prompt.getPromptBuilder()) |
127 | | - .build(), |
128 | | - dispatcherConfigResult); |
129 | | - if (dispatcherConfigResult.isEmpty()) { |
130 | | - throw new InterruptedException(); |
131 | | - } |
| 115 | + DispatcherMeta meta = secDispatcher.availableDispatchers().stream() |
| 116 | + .filter(d -> Objects.equals(config.getDefaultDispatcher(), d.name())) |
| 117 | + .findFirst() |
| 118 | + .orElseThrow(); |
| 119 | + if (!meta.fields().isEmpty()) { |
| 120 | + dispatcherConfigResult = prompt.prompt( |
| 121 | + context.header, |
| 122 | + configureDispatcher(context, meta, prompt.getPromptBuilder()) |
| 123 | + .build()); |
| 124 | + if (dispatcherConfigResult.isEmpty()) { |
| 125 | + throw new InterruptedException(); |
| 126 | + } |
132 | 127 |
|
133 | | - List<Map.Entry<String, PromptResultItemIF>> editables = dispatcherConfigResult.entrySet().stream() |
134 | | - .filter(e -> e.getValue().getResult().contains("$")) |
135 | | - .toList(); |
136 | | - if (!editables.isEmpty()) { |
137 | | - context.addInHeader(""); |
138 | | - context.addInHeader("Please customize the editable value:"); |
139 | | - Map<String, PromptResultItemIF> editMap = new HashMap<>(editables.size()); |
140 | | - for (Map.Entry<String, PromptResultItemIF> editable : editables) { |
141 | | - String template = editable.getValue().getResult(); |
142 | | - prompt.prompt( |
143 | | - context.header, |
144 | | - prompt.getPromptBuilder() |
145 | | - .createInputPrompt() |
146 | | - .name("edit") |
147 | | - .message(template) |
148 | | - .addPrompt() |
149 | | - .build(), |
150 | | - editMap); |
151 | | - if (editMap.isEmpty()) { |
152 | | - throw new InterruptedException(); |
153 | | - } |
154 | | - dispatcherConfigResult.put(editable.getKey(), editMap.get("edit")); |
| 128 | + List<Map.Entry<String, PromptResultItemIF>> editables = dispatcherConfigResult.entrySet().stream() |
| 129 | + .filter(e -> e.getValue().getResult().contains("$")) |
| 130 | + .toList(); |
| 131 | + if (!editables.isEmpty()) { |
| 132 | + context.addInHeader(""); |
| 133 | + context.addInHeader("Please customize the editable value:"); |
| 134 | + Map<String, PromptResultItemIF> editMap; |
| 135 | + for (Map.Entry<String, PromptResultItemIF> editable : editables) { |
| 136 | + String template = editable.getValue().getResult(); |
| 137 | + editMap = prompt.prompt( |
| 138 | + context.header, |
| 139 | + prompt.getPromptBuilder() |
| 140 | + .createInputPrompt() |
| 141 | + .name("edit") |
| 142 | + .message(template) |
| 143 | + .addPrompt() |
| 144 | + .build()); |
| 145 | + if (editMap.isEmpty()) { |
| 146 | + throw new InterruptedException(); |
155 | 147 | } |
156 | | - } |
157 | | - |
158 | | - Config dispatcherConfig = new Config(); |
159 | | - dispatcherConfig.setName(meta.name()); |
160 | | - for (DispatcherMeta.Field field : meta.fields()) { |
161 | | - ConfigProperty property = new ConfigProperty(); |
162 | | - property.setName(field.getKey()); |
163 | | - property.setValue( |
164 | | - dispatcherConfigResult.get(field.getKey()).getResult()); |
165 | | - dispatcherConfig.addProperty(property); |
166 | | - } |
167 | | - if (!dispatcherConfig.getProperties().isEmpty()) { |
168 | | - config.addConfiguration(dispatcherConfig); |
| 148 | + dispatcherConfigResult.put(editable.getKey(), editMap.get("edit")); |
169 | 149 | } |
170 | 150 | } |
171 | | - } |
172 | 151 |
|
173 | | - if (yes) { |
174 | | - secDispatcher.writeConfiguration(config); |
175 | | - } else { |
176 | | - context.addInHeader(""); |
177 | | - context.addInHeader("Values set:"); |
178 | | - context.addInHeader("defaultDispatcher=" + config.getDefaultDispatcher()); |
179 | | - for (Config c : config.getConfigurations()) { |
180 | | - context.addInHeader(" dispatcherName=" + c.getName()); |
181 | | - for (ConfigProperty cp : c.getProperties()) { |
182 | | - context.addInHeader(" " + cp.getName() + "=" + cp.getValue()); |
183 | | - } |
| 152 | + Config dispatcherConfig = new Config(); |
| 153 | + dispatcherConfig.setName(meta.name()); |
| 154 | + for (DispatcherMeta.Field field : meta.fields()) { |
| 155 | + ConfigProperty property = new ConfigProperty(); |
| 156 | + property.setName(field.getKey()); |
| 157 | + property.setValue(dispatcherConfigResult.get(field.getKey()).getResult()); |
| 158 | + dispatcherConfig.addProperty(property); |
| 159 | + } |
| 160 | + if (!dispatcherConfig.getProperties().isEmpty()) { |
| 161 | + config.addConfiguration(dispatcherConfig); |
184 | 162 | } |
| 163 | + } |
| 164 | + } |
185 | 165 |
|
186 | | - prompt.prompt( |
187 | | - context.header, confirmPrompt(prompt.getPromptBuilder()).build(), confirmChoice); |
188 | | - ConfirmResult confirm = (ConfirmResult) confirmChoice.get("confirm"); |
189 | | - if (confirm.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) { |
190 | | - context.terminal |
191 | | - .writer() |
192 | | - .println(messageBuilderFactory |
193 | | - .builder() |
194 | | - .info("Writing out the configuration...") |
195 | | - .build()); |
196 | | - secDispatcher.writeConfiguration(config); |
197 | | - } else { |
198 | | - context.terminal |
199 | | - .writer() |
200 | | - .println(messageBuilderFactory |
201 | | - .builder() |
202 | | - .warning("Values not accepted; not saving configuration.") |
203 | | - .build()); |
204 | | - return CANCELED; |
| 166 | + if (yes) { |
| 167 | + secDispatcher.writeConfiguration(config); |
| 168 | + } else { |
| 169 | + context.addInHeader(""); |
| 170 | + context.addInHeader("Values set:"); |
| 171 | + context.addInHeader("defaultDispatcher=" + config.getDefaultDispatcher()); |
| 172 | + for (Config c : config.getConfigurations()) { |
| 173 | + context.addInHeader(" dispatcherName=" + c.getName()); |
| 174 | + for (ConfigProperty cp : c.getProperties()) { |
| 175 | + context.addInHeader(" " + cp.getName() + "=" + cp.getValue()); |
205 | 176 | } |
206 | 177 | } |
207 | | - } catch (IOError e) { |
208 | | - // TODO: this should be handled properly in jline3! |
209 | | - if (e.getCause() instanceof InterruptedIOException) { |
210 | | - throw new UserInterruptException(e.getCause()); |
| 178 | + |
| 179 | + confirmChoice = prompt.prompt( |
| 180 | + context.header, confirmPrompt(prompt.getPromptBuilder()).build()); |
| 181 | + ConfirmResult confirm = (ConfirmResult) confirmChoice.get("confirm"); |
| 182 | + if (confirm.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) { |
| 183 | + context.terminal |
| 184 | + .writer() |
| 185 | + .println(messageBuilderFactory |
| 186 | + .builder() |
| 187 | + .info("Writing out the configuration...") |
| 188 | + .build()); |
| 189 | + secDispatcher.writeConfiguration(config); |
211 | 190 | } else { |
212 | | - throw e; |
| 191 | + context.terminal |
| 192 | + .writer() |
| 193 | + .println(messageBuilderFactory |
| 194 | + .builder() |
| 195 | + .warning("Values not accepted; not saving configuration.") |
| 196 | + .build()); |
| 197 | + return CANCELED; |
213 | 198 | } |
214 | 199 | } |
215 | 200 |
|
|
0 commit comments