Skip to content

Commit 8eb62e7

Browse files
committed
refactor: extract logging config into mixin
1 parent fc06db9 commit 8eb62e7

File tree

3 files changed

+77
-45
lines changed

3 files changed

+77
-45
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ Runs spotless
126126
-p, --parallelity=N The number of parallel formatter threads to run.
127127
(default: #cores * 0.5)
128128
-q, --quiet Disable as much output as possible.
129+
-q, --quiet Disable as much output as possible.
129130
-t, --target=<targets> The target files to format.
131+
-v Enable verbose output. Multiple -v options
132+
increase the verbosity (max 5).
130133
-v Enable verbose output. Multiple -v options
131134
increase the verbosity (max 5).
132135
-V, --version Print version information and exit.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.cli;
17+
18+
import java.io.File;
19+
20+
import com.diffplug.spotless.cli.logging.output.LoggingConfigurer;
21+
22+
import picocli.CommandLine;
23+
24+
class LoggingOptions {
25+
26+
@CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
27+
LoggingLevelOptions loggingLevelOptions;
28+
29+
static class LoggingLevelOptions {
30+
@CommandLine.Spec(CommandLine.Spec.Target.MIXEE)
31+
CommandLine.Model.CommandSpec spec;
32+
33+
private boolean[] verbosity;
34+
35+
@CommandLine.Option(
36+
names = {"-v"},
37+
description = "Enable verbose output. Multiple -v options increase the verbosity (max 5).",
38+
arity = "0")
39+
public void setVerbose(boolean[] verbosity) {
40+
if (verbosity.length > 5) {
41+
throw new CommandLine.ParameterException(
42+
spec.commandLine(), "Error: --verbose can be used at most 5 times");
43+
}
44+
this.verbosity = verbosity;
45+
}
46+
47+
@CommandLine.Option(
48+
names = {"--quiet", "-q"},
49+
description = "Disable as much output as possible.",
50+
arity = "0")
51+
boolean quiet;
52+
53+
LoggingConfigurer.CLIOutputLevel toCliOutputLevel() {
54+
if (quiet) {
55+
return LoggingConfigurer.CLIOutputLevel.QUIET;
56+
}
57+
if (verbosity == null) {
58+
return LoggingConfigurer.CLIOutputLevel.DEFAULT;
59+
}
60+
int verbosityCount = this.verbosity.length;
61+
return LoggingConfigurer.CLIOutputLevel.verbosity(verbosityCount);
62+
}
63+
}
64+
65+
@CommandLine.Option(
66+
names = {"--log-file"},
67+
description = "The log file to write the output to.")
68+
File logFile;
69+
}

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

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.cli;
1717

18-
import java.io.File;
1918
import java.nio.charset.Charset;
2019
import java.nio.file.Path;
2120
import java.util.List;
@@ -119,55 +118,16 @@ public void setParallelity(int parallelity) {
119118
this.parallelity = parallelity;
120119
}
121120

122-
@CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
123-
LoggingLevelOptions loggingLevelOptions;
124-
125-
class LoggingLevelOptions {
126-
127-
private boolean[] verbosity;
128-
129-
@CommandLine.Option(
130-
names = {"-v"},
131-
description = "Enable verbose output. Multiple -v options increase the verbosity (max 5).",
132-
arity = "0")
133-
public void setVerbose(boolean[] verbosity) {
134-
if (verbosity.length > 5) {
135-
throw new CommandLine.ParameterException(
136-
spec.commandLine(), "Error: --verbose can be used at most 5 times");
137-
}
138-
this.verbosity = verbosity;
139-
}
140-
141-
@CommandLine.Option(
142-
names = {"--quiet", "-q"},
143-
description = "Disable as much output as possible.",
144-
arity = "0")
145-
boolean quiet;
146-
147-
LoggingConfigurer.CLIOutputLevel toCliOutputLevel() {
148-
if (quiet) {
149-
return LoggingConfigurer.CLIOutputLevel.QUIET;
150-
}
151-
if (verbosity == null) {
152-
return LoggingConfigurer.CLIOutputLevel.DEFAULT;
153-
}
154-
int verbosityCount = this.verbosity.length;
155-
return LoggingConfigurer.CLIOutputLevel.verbosity(verbosityCount);
156-
}
157-
}
158-
159-
@CommandLine.Option(
160-
names = {"--log-file"},
161-
description = "The log file to write the output to.")
162-
File logFile;
121+
@CommandLine.Mixin
122+
LoggingOptions loggingOptions;
163123

164124
@Override
165125
public void setupLogging() {
166126
LoggingConfigurer.configureLogging(
167-
loggingLevelOptions != null
168-
? loggingLevelOptions.toCliOutputLevel()
127+
loggingOptions.loggingLevelOptions != null
128+
? loggingOptions.loggingLevelOptions.toCliOutputLevel()
169129
: LoggingConfigurer.CLIOutputLevel.DEFAULT,
170-
logFile);
130+
loggingOptions.logFile);
171131
}
172132

173133
@Override

0 commit comments

Comments
 (0)