Skip to content

KEINOS/go-gisty

Repository files navigation

go1.18+ Go Reference

go-gisty

go-gisty is a simple and easy-to-use Go package for managing GitHub Gists.

Usage

go get "github.com/KEINOS/go-gisty"
// This package requires a GitHub Personal Access Token (with gist scope)
// to be set in the GITHUB_TOKEN/GH_TOKEN environment variable.
import "github.com/KEINOS/go-gisty/gisty"
  • CRUD
    • Gisty.Create() ..... Create a new gist with specified files to GitHub.
    • Gisty.Read() ....... Get a content of a gist from GitHub.
    • Gisty.Update() ..... Syncs the local changes to the gist on GitHub.
    • Gisty.Delete() ..... Delete a specified gist from GitHub.
  • Gisty.Clone() ........ Clone a specified gist in GitHub to local.
  • Gisty.List() ......... Get the list of gists in the GitHub account.
  • Gisty.Stargazer() .... Get number of stars of a specified gist in GitHub.
  • Gisty.Comments() ..... Get comments of a specified gist in GitHub.

Note : This package is a spin-off of the gist subcommand from the GitHub CLI and is intended to provide a similar functionality as the gh gist command in Go applications.

Conversely, if you just want to create a single command that perform gist operations, it is recommended to create an alias for the gh gist command in your shell configuration, instead of re-inventing the wheel like I did. Also, if you are a vim user and want to handle gist through vim, you should consider using the vim-gist plugin.

func Example() {
  // Create a new Gisty instance.
  obj := gisty.NewGisty()

  // The below line is equivalent to:
  //   gh gist list --public --limit 10
  args := gisty.ListArgs{
    Limit:      10,
    OnlyPublic: true,  // If both OnlyPublic and OnlySecret are true,
    OnlySecret: false, // OnlySecret is prior.
  }

  items, err := obj.List(args)
  if err != nil {
    log.Fatal(err)
  }

  // Print the fields of the first item
  firstItem := items[0]

  fmt.Println("GistID:", firstItem.GistID)
  fmt.Println("Description:", firstItem.Description)
  fmt.Println("Number of files:", firstItem.Files)
  fmt.Println("Is public gist:", firstItem.IsPublic)
  fmt.Println("Updated at:", firstItem.UpdatedAt.String())
  // Output:
  // GistID: e915aa8c01dd438e3ffd79b05f15a4ff
  // Description: Title of gist item 1
  // Number of files: 1
  // Is public gist: true
  // Updated at: 2022-04-18 03:04:38 +0000 UTC
}
func main() {
  // Default gist ID if not specified.
  gistID := "5b10b34f87955dfc86d310cd623a61d1"

  if len(os.Args) > 1 {
    gistID = gisty.SanitizeGistID(os.Args[1])
  }

  // Instantiate a new Gisty object.
  obj := gisty.NewGisty()

  // Get the number of stars of the gist.
  count, err := obj.Stargazer(gistID)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Printf("ID: %s, STARS: %v\n", gistID, count)
}

Statuses

UnitTests PlatformTests GolangCI

CodeQL-Analysis Vulnerability codecov Go Report Card

Contribute

Go Reference go1.18+

  • Branch to PR:
    • main
      • Any PRs for the betterment of the package is welcome.
      • Please draft a PR before you start implementing the feature. So the others can comment on the idea or avoid duplication of work.
  • Issues/bug reports:
    • Opened Issues
    • GitHub Issues
      • Reproducible sample code is required to help us to fix the issue.
  • Help wanted:

TODOs

  • Retrieve comments in a gist [#2]
  • Simple method to fetch a file in a gist [#3]

License