Skip to content

Instantly share code, notes, and snippets.

Last active May 30, 2020 20:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxandersen/590b8a0e824faeb3ee7ddfad741ce842 to your computer and use it in GitHub Desktop.
Save maxandersen/590b8a0e824faeb3ee7ddfad741ce842 to your computer and use it in GitHub Desktop.
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.10.2
//DEPS com.fasterxml.jackson.core:jackson-databind:2.10.2
//DEPS info.picocli:picocli:4.1.4
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import static java.lang.System.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import minimatch.Minimatch;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.Help.Ansi;
@Command(description="Prints matching labels for directories found in folder. Useful to check how labeler will work.")
public class checklabeler implements Callable<Integer> {
@Parameters(index="0", description="Path to .github/labeler.yml")
String yamlFile;
@Parameters(index="1", defaultValue = "", description = "Path to dump labels for")
String rootDir;
@Option(names = "--only-dirs", description = "If set, only print for directories", defaultValue = "false")
boolean onlyDirs;
@Option(names= "--only-matches", description = "Print only matches")
boolean onlyMatches;
public static void main(String... args) {
System.exit(new CommandLine(new checklabeler()).execute(args));
private Map<String, List<String>> labelers;
List<String> findLabels(Path p) {
var result = new ArrayList<String>();
var item = labelers.entrySet().stream().filter(e -> match(p, e.getValue()))
if(onlyMatches && item.isEmpty()) {
// noop
} else {
out.println(Ansi.AUTO.string(p + " @|bold,green,underline " + result +"|@"));
return result;
private static boolean match(Path p, List<String> value) {
for (String pattern : value) {
if(Minimatch.minimatch(p.toFile().getPath(), pattern)) {
return true;
return false;
public Integer call() {
var mapper = new ObjectMapper(new YAMLFactory());
try {
labelers = mapper.readValue(new File(yamlFile), Map.class);
Files.walk(Paths.get(rootDir)).filter(x -> onlyDirs?x.toFile().isDirectory():true).forEach(this::findLabels);
} catch (Exception e) {
return -1;
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment