I haven’t written code every day for many years but every now and then, a project emerges that requires a little bit of coding. This tends to happen about once a year. In the past, the cost of revving up the coding engine could be pretty high:
- I would have to re-learn the basics of Git. I keep a private repository of my few coding projects on Github but don’t code often enough for even basic Git commands to be committed to memory.
- I like working on the command line so inevitably I would have a new computer and have to generate new ssh keys so I wouldn’t have to enter a password when I’m working with the remote Github repository. (I have been using password-less ssh for close to 30 years now and I have never been able to remember the process!)
- If I’m working with a 3rd party API, I need to read through the documentation to figure out how to do whatever it is that I want to do. Sometimes there is sample code for the language I’m using and sometimes there isn’t.
- For some projects, credentials are required. I try to handle those responsibly so I have to re-learn Amazon Secrets Manager. . . and that means I have to re-learn the interface for whatever language I’m using to Amazon Secrets Manager.
- With all that in place, I can now focus on the task at hand.
None of the above is “hard” in a technical sense but it’s all kind of a pain if you don’t code frequently and your environment hasn’t been tended to for a while. My life is also busy so all the grunt work of setting up and maintaining a programming environment is very far down my list of important things to do. For all of the above, I usually figure it all out by using Google but that usually involves digging through documentation (sometimes contradictory or just out-of-date). In the past, I’ve usually reached a point of fatigue with the whole process at this point and then question whether I should be coding again at all.
I have a project in mind that I won’t describe in great detail here but the gist of it is that I want to take bookmarks from Pinboard and make a set of web pages with the data. I decided to use ChatGPT to rev up the coding engine this time and it was so much easier and more immediately productive than other times I’ve been down this path. I’m including my ChatGPT prompts and responses below so you can see what I mean.
It remains to be seen just how deeply ChatGPT penetrates knowledge work and I’m skeptical in many ways — but for this kind of work, it feels pretty magical, especially for a rusty programmer who just wants to get something done.
ChatGPT prompt: Describe basic git commands typically used when working with a remote repository on Github
Here are some basic git commands that are typically used when working with a remote repository on Github:
git clone
: This command is used to create a local copy of a remote repository. For example, if you want to clone a repository from Github, you can run the command git clone <repository-url>
.
git add
: This command is used to stage changes made to a local repository before committing them. For example, if you have made changes to a file in your local repository, you can stage those changes by running the command git add <file-name>
.
git commit
: This command is used to commit staged changes to a local repository. For example, if you have staged changes to a file in your local repository, you can commit those changes by running the command git commit -m "commit message"
.
git push
: This command is used to push committed changes from a local repository to a remote repository. For example, if you have committed changes to a file in your local repository and want to push those changes to Github, you can run the command git push
.
git pull
: This command is used to fetch and merge changes from a remote repository to a local repository. For example, if someone else has made changes to a file in the remote repository, you can fetch and merge those changes into your local repository by running the command git pull
.
git branch
: This command is used to list, create, or delete branches in a local repository. For example, if you want to list all the branches in your local repository, you can run the command git branch
.
git merge
: This command is used to merge changes from one branch to another in a local repository. For example, if you want to merge changes from the feature-branch
to the master
branch in your local repository, you can run the command git merge feature-branch
.
These are just some of the basic git commands that are commonly used when working with a remote repository on Github. There are many other git commands that can be used to perform more advanced tasks, such as resolving conflicts, rebasing branches, and more.
ChatGPT prompt: using the command line, how do I set up a git client to work with a remote repository including setup of keys so I can work without a password
To set up a Git client to work with a remote repository and configure SSH keys for passwordless access, you can follow these steps:
- Install Git on your machine, if it is not already installed.
- Generate an SSH key pair using the
ssh-keygen
command: ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This command will generate a new SSH key pair using RSA encryption and a bit length of 4096. Replace “your_email@example.com” with your email address.
- Add the public key to your Git hosting provider (e.g. GitHub, GitLab, Bitbucket, etc.) by following their instructions for adding an SSH key.For example, to add a key to a GitHub account, navigate to the “Settings” page, click on “SSH and GPG keys”, and then click on “New SSH key”. Paste the contents of the public key (usually found in
~/.ssh/id_rsa.pub
) into the “Key” field.
- Test the SSH connection by running the following command:
ssh -T git@github.com
Replace github.com
with the hostname of your Git hosting provider. You should see a message similar to the following: Hi username! You've successfully authenticated, but GitHub does not provide shell access.
If you see an error message instead, double-check that you’ve added the public key to your Git hosting provider correctly.
- Configure Git to use the SSH key for passwordless access by running the following commands:
git config --global user.name "Your Name" git config --global user.email "your_email@example.com" git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa"
Replace “Your Name” and “your_email@example.com” with your name and email address, respectively. The last command tells Git to use the SSH key for all SSH connections.
- Clone the remote repository to your local machine using the SSH URL:
git clone git@github.com:username/repo.git
Replace username
with your Git username and repo
with the name of the repository you want to clone.
That’s it! You should now be able to use Git to push and pull changes to and from the remote repository without entering a password each time.
ChatGPT prompt: write a sample client in PHP that uses the Pinboard v2 API to fetch a list of all bookmarks
(Note: my first prompt simply asked about the Pinboard API and I got a v1 sample. I prompted again and asked for v2)
Sure! Here’s an example PHP client that uses the Pinboard v2 API to fetch a list of all bookmarks:
<?php
// Replace with your own Pinboard API credentials
$username = "your_username";
$apikey = "your_apikey";
// Set the URL for the API request
$url = "https://api.pinboard.in/v2/posts/all?auth_token={$username}:{$apikey}&format=json";
// Set up a cURL session to send the API request
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
// Decode the JSON response
$bookmarks = json_decode($response, true);
// Loop through the bookmarks and output their titles and URLs
foreach ($bookmarks['posts'] as $bookmark) {
echo $bookmark['description'] . "\n";
echo $bookmark['href'] . "\n\n";
}
?>
Make sure to replace your_username
and your_apikey
with your own Pinboard API credentials. This client uses cURL to send the API request and fetch the JSON response. It then uses json_decode()
with the true
parameter to parse the JSON into a PHP associative array, and loops through the array to output the titles and URLs of each bookmark.
Note that the Pinboard v2 API requires authentication using a username and API token, unlike the v1 API which uses a username and password.
ChatGPT prompt: write php code for using Amazon Secrets Manager
(Note: I also asked ChatGPT the same question but without using Composer)
To use Amazon Secrets Manager in PHP, you will need to install the AWS SDK for PHP. You can do this by running the following command:
codecomposer require aws/aws-sdk-php
Once you have installed the AWS SDK for PHP, you can use the following code to access a secret stored in Amazon Secrets Manager:
<?php
require 'vendor/autoload.php';
use Aws\SecretsManager\SecretsManagerClient;
$secretsManager = new SecretsManagerClient([
'version' => 'latest',
'region' => 'us-east-1' // replace with the region where your secret is stored
]);
$secretName = 'mySecret';
$result = $secretsManager->getSecretValue([
'SecretId' => $secretName
]);
$secret = $result['SecretString'];
echo $secret;
?>
In this code, we first load the AWS SDK for PHP using the autoload.php
file. Then, we create a new SecretsManagerClient
object and provide it with the region where our secret is stored. We also provide the name of the secret we want to access.
Next, we call the getSecretValue
method on the SecretsManagerClient
object, passing in the name of the secret. This method returns a result object, which we can use to access the secret value.
Finally, we retrieve the secret value from the result object and print it to the screen. Note that the secret value is returned as a string, so you may need to parse it into the appropriate format for your application.
You must be logged in to post a comment.