gitignore.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // GitignoresService provides access to the gitignore related functions in the
  11. // GitHub API.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/gitignore/
  14. type GitignoresService service
  15. // Gitignore represents a .gitignore file as returned by the GitHub API.
  16. type Gitignore struct {
  17. Name *string `json:"name,omitempty"`
  18. Source *string `json:"source,omitempty"`
  19. }
  20. func (g Gitignore) String() string {
  21. return Stringify(g)
  22. }
  23. // List all available Gitignore templates.
  24. //
  25. // GitHub API docs: https://developer.github.com/v3/gitignore/#listing-available-templates
  26. func (s GitignoresService) List(ctx context.Context) ([]string, *Response, error) {
  27. req, err := s.client.NewRequest("GET", "gitignore/templates", nil)
  28. if err != nil {
  29. return nil, nil, err
  30. }
  31. var availableTemplates []string
  32. resp, err := s.client.Do(ctx, req, &availableTemplates)
  33. if err != nil {
  34. return nil, resp, err
  35. }
  36. return availableTemplates, resp, nil
  37. }
  38. // Get a Gitignore by name.
  39. //
  40. // GitHub API docs: https://developer.github.com/v3/gitignore/#get-a-single-template
  41. func (s GitignoresService) Get(ctx context.Context, name string) (*Gitignore, *Response, error) {
  42. u := fmt.Sprintf("gitignore/templates/%v", name)
  43. req, err := s.client.NewRequest("GET", u, nil)
  44. if err != nil {
  45. return nil, nil, err
  46. }
  47. gitignore := new(Gitignore)
  48. resp, err := s.client.Do(ctx, req, gitignore)
  49. if err != nil {
  50. return nil, resp, err
  51. }
  52. return gitignore, resp, nil
  53. }