Created
May 29, 2020 14:03
-
-
Save bzdgn/023e9aa701ab1772842c38abf91916eb to your computer and use it in GitHub Desktop.
Github Repository Search Command With JBang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS info.picocli:picocli:4.2.0 | |
import picocli.CommandLine; | |
import picocli.CommandLine.Command; | |
import picocli.CommandLine.Parameters; | |
import java.util.concurrent.Callable; | |
import java.io.InputStreamReader; | |
import java.io.BufferedReader; | |
@Command(name = "repo_search", mixinStandardHelpOptions = true, version = "repo_search 0.1", | |
description = "github repository search by topic made with jbang") | |
class repoSearch implements Callable<Integer> { | |
@Parameters(index = "0", description = "github repository topic", defaultValue = "jbang") | |
private String topic; | |
private String curlCommand = "curl \"https://api.github.com/search/repositories?q=topic:%s\""; | |
public static void main(String... args) { | |
int exitCode = new CommandLine(new repoSearch()).execute(args); | |
System.exit(exitCode); | |
} | |
@Override | |
public Integer call() throws Exception { | |
Runtime rt = Runtime.getRuntime(); | |
String command = String.format(curlCommand, topic); | |
Process process = rt.exec(command); | |
Thread.sleep(100); | |
System.out.println("Printing github repository results for topic: " + topic); | |
System.out.println("----------------------------------------------" + printPadding(topic)); | |
System.out.println((getResultFromProcess(process))); | |
return 0; | |
} | |
private static String getResultFromProcess(Process process) { | |
StringBuilder output = new StringBuilder(); | |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));) { | |
String line; | |
while ((line = reader.readLine()) != null) { | |
output.append(line + "\n"); | |
} | |
return output.toString(); | |
} catch(Exception e) { | |
System.out.println("Unable to retrieve from github"); | |
e.printStackTrace(); | |
} | |
return "Error: could not retrieve the result"; | |
} | |
private static String printPadding(String param) { | |
String padding = ""; | |
for (int i = 0; i < param.length(); i++) { | |
padding += "-"; | |
} | |
return padding; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output;