-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·113 lines (99 loc) · 2.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env node
const axios = require("axios");
const { program } = require("commander");
const readline = require("readline");
const BaseURL = "https://api.github.com";
async function fetchUSER(user) {
try {
const userResponse = await axios.get(`${BaseURL}/users/${user}`);
console.log(userResponse.data);
const reposResponse = await axios.get(`${userResponse.data.repos_url}`);
console.log("\nRepositories:");
reposResponse.data.forEach((repo, index) => {
console.log(`${index + 1}. ${repo.name} - ${repo.html_url}`);
});
} catch (error) {
console.error(
"Error fetching user or repos:",
error.response ? error.response.data.message : error.message
);
}
}
async function fetchCommits(username, repo) {
try {
const response = await axios.get(
`${BaseURL}/repos/${username}/${repo}/commits`
);
response.data.forEach((commit) => {
console.log(
`Commit: ${commit.commit.message} \nAuthor: ${commit.commit.author.name}\n`
);
});
} catch (error) {
console.error(
"Error fetching commits:",
error.response ? error.response.data.message : error.message
);
}
}
function showMenu() {
console.log("\nAvailable Commands:");
console.log(" 1. fetch-username <username> - Fetch GitHub user info");
console.log(" 2. get-commits <username> <repo> - Fetch repository commits");
console.log(" 3. exit - Exit the CLI tool\n");
}
program.version("1.0.0").description("Github CLI for fetching GitHub commits");
program
.command("fetch-username <username>")
.description("Fetch a user info by username")
.action((username) => {
fetchUSER(username);
});
program
.command("get-commits <username> <repo>")
.description("Fetch commits from a repo")
.action((username, repo) => {
fetchCommits(username, repo);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "GithubCLI> ",
});
function handleUserInput(line) {
const args = line.trim().split(" ");
const command = args[0];
switch (command) {
case "fetch-username":
if (args.length === 2) {
fetchUSER(args[1]);
} else {
console.log("Usage: fetch-username <username>");
}
break;
case "get-commits":
if (args.length === 3) {
fetchCommits(args[1], args[2]);
} else {
console.log("Usage: get-commits <username> <repo>");
}
break;
case "exit":
rl.close();
console.log("Exiting...");
break;
default:
console.log("Invalid command");
showMenu();
}
}
console.log("Welcome to Github CLI");
showMenu();
rl.prompt();
rl.on("line", (line) => {
handleUserInput(line);
rl.prompt();
}).on("close", () => {
console.log("Goodbye!");
process.exit(0);
});