Skip to content

Commit d754183

Browse files
committed
chore: init logging by leveraging picocli lifecycle
and provide output as field on root command (can easily be retrieved by steps in case they need it).
1 parent 5747696 commit d754183

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

app/src/main/java/com/diffplug/spotless/cli/SpotlessAction.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
import org.jetbrains.annotations.NotNull;
1919

2020
import com.diffplug.spotless.cli.execution.FormatterStepsSupplier;
21-
import com.diffplug.spotless.cli.logging.output.Output;
2221

2322
public interface SpotlessAction extends SpotlessCommand {
24-
@NotNull Integer executeSpotlessAction(@NotNull Output output, @NotNull FormatterStepsSupplier formatterSteps);
25-
26-
@NotNull Output setupLogging();
23+
@NotNull Integer executeSpotlessAction(@NotNull FormatterStepsSupplier formatterSteps);
2724
}

app/src/main/java/com/diffplug/spotless/cli/SpotlessCLI.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public class SpotlessCLI implements SpotlessAction, SpotlessCommand, SpotlessAct
9696
@CommandLine.Spec
9797
CommandLine.Model.CommandSpec spec; // injected by picocli
9898

99+
private @NotNull Output output;
100+
99101
@CommandLine.Option(
100102
names = {"--mode", "-m"},
101103
defaultValue = "APPLY",
@@ -196,8 +198,20 @@ LoggingConfigurer.CLIOutputLevel toCliOutputLevel() {
196198
description = "The log file to write the output to.")
197199
File logFile;
198200

199-
@Override
200-
public @NotNull Output setupLogging() {
201+
@CommandLine.Option(
202+
names = {"--zzz"},
203+
hidden = true,
204+
defaultValue = "true",
205+
description = "Just a hook to be able to initialize logging.")
206+
void setFinal(boolean finalFlag) {
207+
// this is a hack to make sure that the logging is initialized before any processing occurs outside of parsing
208+
// the command line.
209+
// Maybe there is a picocli hook I've missed for this?
210+
this.output = setupLogging(spec, loggingLevelOptions, logFile);
211+
}
212+
213+
private static @NotNull Output setupLogging(
214+
CommandLine.Model.CommandSpec spec, LoggingLevelOptions loggingLevelOptions, File logFile) {
201215
CommandLine commandLine = spec.commandLine();
202216
LoggingConfigurer.CLIOutputLevel outputLevel = loggingLevelOptions != null
203217
? loggingLevelOptions.toCliOutputLevel()
@@ -221,9 +235,12 @@ private static void logMetaStatements() {
221235
nonSpotlessLogger.debug("Meta: non-spotless loggers on level debug enabled.");
222236
}
223237

238+
public @NotNull Output output() {
239+
return this.output;
240+
}
241+
224242
@Override
225-
public @NotNull Integer executeSpotlessAction(
226-
@NotNull Output output, @NotNull FormatterStepsSupplier formatterSteps) {
243+
public @NotNull Integer executeSpotlessAction(@NotNull FormatterStepsSupplier formatterSteps) {
227244
Objects.requireNonNull(output);
228245
Objects.requireNonNull(formatterSteps);
229246
validateTargets();
@@ -248,7 +265,7 @@ private static void logMetaStatements() {
248265
.toList();
249266
ResultType resultType = stepResults.stream()
250267
.map(future -> ThrowingEx.get(future::get))
251-
.map(result -> this.handleResult(output, result))
268+
.map(this::handleResult)
252269
.reduce(ResultType.CLEAN, ResultType::combineWith);
253270
return spotlessMode.translateResultTypeToExitCode(resultType);
254271
}
@@ -270,7 +287,7 @@ private void validateTargets() {
270287
}
271288
}
272289

273-
private ResultType handleResult(Output output, Result result) {
290+
private ResultType handleResult(Result result) {
274291
if (result.lintState().isClean()) {
275292
LOGGER.debug("File is clean: {}", result.target().toFile());
276293
return ResultType.CLEAN;

app/src/main/java/com/diffplug/spotless/cli/execution/SpotlessExecutionStrategy.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.cli.execution;
1717

18-
import com.diffplug.spotless.cli.SpotlessAction;
1918
import com.diffplug.spotless.cli.core.SpotlessActionContext;
2019
import com.diffplug.spotless.cli.core.SpotlessCommandLineStream;
21-
import com.diffplug.spotless.cli.logging.output.Output;
2220

2321
import picocli.CommandLine;
2422

@@ -35,13 +33,6 @@ public int execute(CommandLine.ParseResult parseResult) throws CommandLine.Execu
3533
}
3634

3735
private Integer runSpotlessActions(SpotlessCommandLineStream commandLineStream) {
38-
// 0. setup logging
39-
Output output = commandLineStream
40-
.actions()
41-
.findFirst()
42-
.map(SpotlessAction::setupLogging)
43-
.orElseThrow();
44-
4536
// 1. prepare context
4637
SpotlessActionContext context = provideSpotlessActionContext(commandLineStream);
4738

@@ -51,7 +42,7 @@ private Integer runSpotlessActions(SpotlessCommandLineStream commandLineStream)
5142
stepsSupplierFactory.createFormatterStepsSupplier(commandLineStream, context);
5243

5344
// 3. run spotless steps
54-
return executeSpotlessAction(output, commandLineStream, stepsSupplier);
45+
return executeSpotlessAction(commandLineStream, stepsSupplier);
5546
}
5647

5748
private SpotlessActionContext provideSpotlessActionContext(SpotlessCommandLineStream commandLineStream) {
@@ -63,11 +54,11 @@ private SpotlessActionContext provideSpotlessActionContext(SpotlessCommandLineSt
6354
}
6455

6556
private Integer executeSpotlessAction(
66-
Output output, SpotlessCommandLineStream commandLineStream, FormatterStepsSupplier stepsSupplier) {
57+
SpotlessCommandLineStream commandLineStream, FormatterStepsSupplier stepsSupplier) {
6758
return commandLineStream
6859
.actions()
6960
.findFirst()
70-
.map(spotlessAction -> spotlessAction.executeSpotlessAction(output, stepsSupplier))
61+
.map(spotlessAction -> spotlessAction.executeSpotlessAction(stepsSupplier))
7162
.orElse(-1);
7263
}
7364
}

app/src/test/java/com/diffplug/spotless/cli/core/ChecksumCalculatorTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.diffplug.spotless.cli.SpotlessAction;
3030
import com.diffplug.spotless.cli.SpotlessActionContextProvider;
3131
import com.diffplug.spotless.cli.execution.FormatterStepsSupplier;
32-
import com.diffplug.spotless.cli.logging.output.Output;
3332
import com.diffplug.spotless.cli.steps.SpotlessCLIFormatterStep;
3433
import com.diffplug.spotless.cli.steps.SpotlessFormatterStep;
3534

@@ -289,15 +288,9 @@ static class Action implements SpotlessAction {
289288
Path baseDir;
290289

291290
@Override
292-
public @NotNull Integer executeSpotlessAction(
293-
@NotNull Output output, @NotNull FormatterStepsSupplier formatterSteps) {
291+
public @NotNull Integer executeSpotlessAction(@NotNull FormatterStepsSupplier formatterSteps) {
294292
return 0;
295293
}
296-
297-
@Override
298-
public @NotNull Output setupLogging() {
299-
return new Output();
300-
}
301294
}
302295

303296
private static SpotlessCommandLineStream commandLine(SpotlessAction action, SpotlessFormatterStep... steps) {

0 commit comments

Comments
 (0)