3

I'm trying to remotely update an existing Github gist through Git bash, and I cannot find a way to upload a file's contents without manually declaring the contents inside of the command.

My goal is to take a file I have on my PC, and upload its contents to update a pre-existing Gist (the Gist ID which is mentioned in the latter text).

Here is an example Gist ID in the hope that it helps: 'c7alSToGZjxiMveTa2e6bzVvXJRqS09D'. For the sake of this discussion, the exact name of the file I am trying to upload is 'testingFile.txt', located at the file path 'C:\Users\username\Desktop', with the Gist file display name of 'monkey.txt'.

How would I make the "curl PATCH" command be able to execute such an upload/update procedure? I've tried the following code curl -d '{"files":{"monkey.txt":{"content":"$chung"}}}' -u username:accessToken -X PATCH https://api.github.com/gists/{gist_id}, where $chung was set equal to the file contents of "testingFile.txt". This exact code would just end up uploading the text "$chung" to the gist, instead of the variable's actual contents (which for this example, is ["WelcomeToTheJungle", "AmazonRainforest", "Brazil"]. Any suggestions on how to do this are welcome, cheers!

EDIT: Github's API for this is- https://docs.github.com/en/rest/reference/gists#update-a-gist

Dyte
  • 31
  • 3
  • It doesn’t work because you enclosed the argument for `-d` in single quotes, anything inside single quotes are treated literally, so `$chung` won’t work – hedy Aug 12 '20 at 09:35

2 Answers2

2

You can also use the GitHub CLI gh 2.8.0 (Apr. 2022):

Allow non-interactive gist file replacement

Allows the overwriting of a gist file with a local replacement without any interactivity.

The behavior change is that if the user specifies the gist file to edit using the --filename flag, then skip showing the continuation prompt of Submit/Cancel/Edit.

It follows the same syntax as adding a file:

# adding new file
gh gist edit <gist-id> -a <gist-filename> <local-filename>

# overwriting file
gh gist edit <gist-id> -f <gist-filename> <local-filename>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Try this:

curl -d '{"files":{"monkey.txt":{"content": '"$chung"'}}}' -u username:accessToken -X PATCH https://api.github.com/gists/{gist_id}

As a follow up to my comment I have a solution, you can do this 'literal'"$variable"'literal' To have variables inside strings, or just replace all your single quotes with double quotes, then the $chung won’t be literal.

Reference:

hedy
  • 1,160
  • 5
  • 23