Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created July 10, 2020 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxandersen/77acbb28ca118f92eb667708a871182b to your computer and use it in GitHub Desktop.
Save maxandersen/77acbb28ca118f92eb667708a871182b to your computer and use it in GitHub Desktop.
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.2.0
//DEPS org.kohsuke:github-api:1.101
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.extras.OkHttpConnector;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;
@Command(name = "delrepo", mixinStandardHelpOptions = true, version = "delrepo 0.1",
description = "delrepo made with jbang.\n\nBE CAREFUL: Once you use -f this script WILL DELETE ANY MATCHING REPOSITORY! No undo!")
class delrepo implements Callable<Integer> {
@Option(names = { "--token"}, defaultValue = "${GITHUB_TOKEN}")
private String token;
@Option(names={"-f", "--force"}, description = "Actually delete repository matching the pattern")
boolean force;
@Parameters(index="0")
private String repopattern;
public static void main(String... args) {
int exitCode = new CommandLine(new delrepo()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception {
if(force) {
System.out.println("Finding repos to forcifully delete matching " + repopattern);
} else {
System.out.println("Finding repos matching " + repopattern);
System.out.println("Use -f to actually forcefully delete.");
}
Pattern pattern = Pattern.compile(repopattern);
GitHub github = GitHub.connectUsingOAuth(token);
var ghRepo = github.getUser("maxandersen").listRepositories().iterator();
ghRepo.forEachRemaining(repo -> {
if (repo.getName().matches(repopattern)) {
System.out.println(repo.getHtmlUrl());
if(force) {
try {
repo.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment