Gh user info
GitHub User Info Fetcher: A Fish Shell Function for Quick Access
Problem
When working with GitHub, you often need quick access to a user's profile information, including their repositories, bio, and other relevant details. The GitHub web interface provides this information, but it can be cumbersome to navigate through multiple pages to get the data you need. Moreover, when working in a terminal environment, it’s much more convenient to have this information displayed right in your shell.
Idea and Possible Use Cases
The idea is to create a simple and efficient way to retrieve GitHub user information directly from the command line without needing to open a browser. This could be useful in a variety of situations:
- Developers: Quickly checking the profile of a collaborator or a potential contributor.
- Hiring Managers: Reviewing the GitHub profiles of job candidates.
- Open Source Contributors: Finding and exploring repositories of other developers.
The function we’re about to discuss accomplishes this by fetching and displaying the user information using the GitHub API, all from the comfort of your Fish shell.
Proposed Solution
The solution is a Fish shell function named gh_user_info
that takes a GitHub username as an argument, retrieves the user’s profile and repository information using the GitHub API, and prints it to the terminal. The function is designed to be easy to use, and since the GitHub API doesn’t require an access token for public data, no special setup or authentication is needed.
How to Do It (In Theory)
Here’s a high-level overview of how this solution works:
- User Input: The function accepts a GitHub username as an argument.
- API Requests: It uses Python to send HTTP GET requests to the GitHub API.
- Data Parsing: The API responses are parsed to extract relevant information such as username, bio, number of repositories, and more.
- Output: The parsed information is printed to the terminal in a readable format, including some ASCII art for a stylish touch.
This approach leverages the powerful combination of Fish shell scripting and Python, making it both flexible and easy to extend.
The Function
Here is the gh_user_info
function, complete with inline comments for clarity:
function gh_user_info
# Check if the required arguments are provided
if test (count $argv) -lt 1
echo "Usage: gh_user_info <username>"
return 1
end
set username $argv[1]
# Fetch user info from GitHub API and print it using Python
python3 -c "
import pyfiglet
import requests
import json
def print_ascii_title(title):
# Generate and print ASCII art for the title using pyfiglet
ascii_art = pyfiglet.figlet_format(title, font='slant')
print(ascii_art)
def print_user_info(username):
profile_url = f'https://api.github.com/users/{username}'
try:
# Send a GET request to fetch the user's profile data
response = requests.get(profile_url)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx, 5xx)
except requests.exceptions.RequestException as e:
# Print an error message if the request fails
print(f'Error: Unable to fetch user info for {username}. {str(e)}')
return
# Parse the JSON response into a Python dictionary
user_data = response.json()
# Print relevant user information
print(f'Username: {user_data.get(\"login\", \"N/A\")}')
print(f'Name: {user_data.get(\"name\", \"N/A\")}')
print(f'Location: {user_data.get(\"location\", \"N/A\")}')
print(f'Bio: {user_data.get(\"bio\", \"N/A\")}')
print(f'Public Repos: {user_data.get(\"public_repos\", \"N/A\")}')
print(f'Followers: {user_data.get(\"followers\", \"N/A\")}')
print(f'Following: {user_data.get(\"following\", \"N/A\")}')
print(f'Profile URL: {user_data.get(\"html_url\", \"N/A\")}')
# Fetch and print the user's repositories
print_repos(username)
def print_repos(username):
repos_url = f'https://api.github.com/users/{username}/repos'
try:
# Send a GET request to fetch the user's repositories
response = requests.get(repos_url)
response.raise_for_status()
except requests.exceptions.RequestException as e:
# Print an error message if the request fails
print(f'Error: Unable to fetch repositories for {username}. {str(e)}')
return
repos = response.json()
if repos:
# Print a list of the user's public repositories
print('\nPublic Repositories:')
for repo in repos:
name = repo.get('name', 'N/A')
description = repo.get('description', 'No description provided')
url = repo.get('html_url', 'N/A')
print(f' - {name}: {description}')
print(f' {url}\n')
else:
print('No public repositories available.')
# Generate and print the ASCII title 'User Info'
print_ascii_title('User Info')
# Fetch and print the GitHub user information
print_user_info('$username')
"
end
Step by Step Explanation of the Function
Let's break down how the gh_user_info
function works:
- Argument Check:
- The function starts by checking if the user provided a GitHub username.
-
If no username is provided, it displays a usage message and exits.
-
Setting the Username:
-
The username is extracted from the command-line arguments and stored in the
username
variable. -
Python Script Execution:
- The core of the function is a Python script embedded within the Fish shell function.
-
The script first imports necessary libraries (
pyfiglet
for ASCII art andrequests
for making HTTP requests). -
Generating ASCII Art:
-
The
print_ascii_title
function generates an ASCII art title using thepyfiglet
library and prints it. -
Fetching User Info:
- The
print_user_info
function sends a GET request to the GitHub API to fetch the user's profile data. - It handles errors gracefully, printing a message if the request fails.
-
If the request is successful, it extracts and prints key information such as the username, name, location, bio, public repositories, followers, and the profile URL.
-
Fetching Repositories:
- The
print_repos
function sends a GET request to fetch the user's repositories. -
It also handles errors and prints a list of the user's public repositories, including their names, descriptions, and URLs.
-
Output:
- The function prints all the gathered information in a user-friendly format, making it easy to see the most important details at a glance.
Conclusion
The gh_user_info
function is a practical tool for quickly accessing GitHub user information directly from the command line. It leverages the power of Fish shell and Python, making it both flexible and easy to use. The function does not require any special tokens or authentication since it only accesses public data available through the GitHub API.
This solution is especially useful for developers, hiring managers, or anyone who frequently interacts with GitHub. With this function, you can get a comprehensive overview of a user's GitHub profile with a single command, saving you time and effort.
Try it out, and you'll find it to be a handy addition to your Fish shell toolbox!