Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Created May 29, 2020 14:03
Show Gist options
  • Save bzdgn/023e9aa701ab1772842c38abf91916eb to your computer and use it in GitHub Desktop.
Save bzdgn/023e9aa701ab1772842c38abf91916eb to your computer and use it in GitHub Desktop.
Github Repository Search Command With JBang
//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;
}
}
@bzdgn
Copy link
Author

bzdgn commented May 29, 2020

Sample output;

C:\jbang>jbang repoSearch.java jbang
[jbang] Building jar...
Printing github repository results for topic: jbang
---------------------------------------------------
{
  "total_count": 1,
  "incomplete_results": false,
  "items": [
    {
      "id": 241279153,
      "node_id": "MDEwOlJlcG9zaXRvcnkyNDEyNzkxNTM=",
      "name": "jbang-issuelabeler",
      "full_name": "maxandersen/jbang-issuelabeler",
      "private": false,
      "owner": {
        "login": "maxandersen",
        "id": 54129,
        "node_id": "MDQ6VXNlcjU0MTI5",
        "avatar_url": "https://avatars1.githubusercontent.com/u/54129?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/maxandersen",
        "html_url": "https://github.com/maxandersen",
        "followers_url": "https://api.github.com/users/maxandersen/followers",
        "following_url": "https://api.github.com/users/maxandersen/following{/other_user}",
        "gists_url": "https://api.github.com/users/maxandersen/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/maxandersen/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/maxandersen/subscriptions",
        "organizations_url": "https://api.github.com/users/maxandersen/orgs",
        "repos_url": "https://api.github.com/users/maxandersen/repos",
        "events_url": "https://api.github.com/users/maxandersen/events{/privacy}",
        "received_events_url": "https://api.github.com/users/maxandersen/received_events",
        "type": "User",
        "site_admin": false
      },
      "html_url": "https://github.com/maxandersen/jbang-issuelabeler",
      "description": "Simple java script that add tags to incoming issues based on regular or javax.el expression",
      "fork": false,
      "url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler",
      "forks_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/forks",
      "keys_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/keys{/key_id}",
      "collaborators_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/collaborators{/collaborator}",
      "teams_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/teams",
      "hooks_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/hooks",
      "issue_events_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/issues/events{/number}",
      "events_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/events",
      "assignees_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/assignees{/user}",
      "branches_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/branches{/branch}",
      "tags_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/tags",
      "blobs_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/git/blobs{/sha}",
      "git_tags_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/git/tags{/sha}",
      "git_refs_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/git/refs{/sha}",
      "trees_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/git/trees{/sha}",
      "statuses_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/statuses/{sha}",
      "languages_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/languages",
      "stargazers_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/stargazers",
      "contributors_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/contributors",
      "subscribers_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/subscribers",
      "subscription_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/subscription",
      "commits_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/commits{/sha}",
      "git_commits_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/git/commits{/sha}",
      "comments_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/comments{/number}",
      "issue_comment_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/issues/comments{/number}",
      "contents_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/contents/{+path}",
      "compare_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/compare/{base}...{head}",
      "merges_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/merges",
      "archive_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/{archive_format}{/ref}",
      "downloads_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/downloads",
      "issues_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/issues{/number}",
      "pulls_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/pulls{/number}",
      "milestones_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/milestones{/number}",
      "notifications_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/notifications{?since,all,participating}",
      "labels_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/labels{/name}",
      "releases_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/releases{/id}",
      "deployments_url": "https://api.github.com/repos/maxandersen/jbang-issuelabeler/deployments",
      "created_at": "2020-02-18T05:20:00Z",
      "updated_at": "2020-05-15T21:36:33Z",
      "pushed_at": "2020-02-23T22:15:27Z",
      "git_url": "git://github.com/maxandersen/jbang-issuelabeler.git",
      "ssh_url": "[email protected]:maxandersen/jbang-issuelabeler.git",
      "clone_url": "https://github.com/maxandersen/jbang-issuelabeler.git",
      "svn_url": "https://github.com/maxandersen/jbang-issuelabeler",
      "homepage": "",
      "size": 41,
      "stargazers_count": 1,
      "watchers_count": 1,
      "language": "Java",
      "has_issues": true,
      "has_projects": true,
      "has_downloads": true,
      "has_wiki": true,
      "has_pages": false,
      "forks_count": 0,
      "mirror_url": null,
      "archived": false,
      "disabled": false,
      "open_issues_count": 0,
      "license": null,
      "forks": 0,
      "open_issues": 0,
      "watchers": 1,
      "default_branch": "master",
      "score": 1.0
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment